mirror of
https://github.com/EnumeratedDev/enit.git
synced 2026-09-16 02:26:11 +00:00
ectl: fix being able to enable services that do not exist
This commit is contained in:
@@ -190,6 +190,18 @@ func enableDisableService(subcommand string) {
|
||||
verb = "disabled"
|
||||
}
|
||||
|
||||
// Ensure service exists
|
||||
if stage != 0 {
|
||||
if !serviceExists(service) {
|
||||
if printJson {
|
||||
fmt.Printf("{\"error\":\"Service (%s) does not exist\"}\n", verb)
|
||||
} else {
|
||||
fmt.Printf("Service (%s) does not exist\n", verb)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Return if service is already enabled
|
||||
if _, enabledStage := isServiceEnabled(service); enabledStage == stage {
|
||||
if printJson {
|
||||
|
||||
@@ -9,6 +9,30 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func serviceExists(service string) bool {
|
||||
dirEntries, err := os.ReadDir(path.Join(sysconfdir, "esvm/services"))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, entry := range dirEntries {
|
||||
var dataMap map[string]any
|
||||
|
||||
data, err := os.ReadFile(path.Join(sysconfdir, "esvm/services", entry.Name()))
|
||||
|
||||
err = yaml.Unmarshal(data, &dataMap)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if name, ok := dataMap["name"]; ok && name == service {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func isServiceEnabled(service string) (bool, int) {
|
||||
for stage, services := range readEnabledServices() {
|
||||
if slices.Contains(services, service) {
|
||||
|
||||
Reference in New Issue
Block a user