diff --git a/command/commands.go b/command/commands.go index 225846a..df3a322 100644 --- a/command/commands.go +++ b/command/commands.go @@ -1,6 +1,7 @@ package command import ( + "ispctl/command/utils" "ispctl/model" "github.com/txix-open/isp-kit/rc/schema" @@ -23,6 +24,10 @@ type ConfigService interface { UpsertVariables(vars []model.UpsertVariableRequest) error } +type UpdateConfigService interface { + CreateUpdateConfig(stringToChange string, configuration *model.Config) (map[string]any, error) +} + func AllCommands(configService ConfigService, autoComplete AutoComplete) []*cli.Command { status := NewStatus(configService) get := NewGet(configService, autoComplete) @@ -43,3 +48,14 @@ func AllCommands(configService ConfigService, autoComplete AutoComplete) []*cli. variables.Command(), } } + +func CreateUpdateConfig(stringToChange string, configuration *model.Config, service UpdateConfigService) error { + answer, err := service.CreateUpdateConfig(stringToChange, configuration) + if err != nil { + return err + } + if answer != nil { + return utils.PrintAnswer(answer) + } + return nil +} diff --git a/command/delete.go b/command/delete.go index fe0dd0f..23e6ce9 100644 --- a/command/delete.go +++ b/command/delete.go @@ -51,7 +51,7 @@ func (c Delete) action(ctx *cli.Context) error { return err } if pathObject == "" { - return utils.CreateUpdateConfig("", moduleConfiguration, c.service) + return CreateUpdateConfig("", moduleConfiguration, c.service) } jsonObject, err := json.Marshal(moduleConfiguration.Data) @@ -63,5 +63,5 @@ func (c Delete) action(ctx *cli.Context) error { return err } - return utils.CreateUpdateConfig(stringToChange, moduleConfiguration, c.service) + return CreateUpdateConfig(stringToChange, moduleConfiguration, c.service) } diff --git a/command/get.go b/command/get.go index 50ee8b9..23e0ebe 100644 --- a/command/get.go +++ b/command/get.go @@ -52,13 +52,12 @@ func (c Get) action(ctx *cli.Context) error { } if pathObject == "" { - utils.PrintAnswer(data) - } else { - jsonObject, err := json.Marshal(data) - if err != nil { - return err - } - utils.CheckObject(jsonObject, pathObject) + return utils.PrintAnswer(data) } - return nil + + jsonObject, err := json.Marshal(data) + if err != nil { + return err + } + return utils.CheckObject(jsonObject, pathObject) } diff --git a/command/merge.go b/command/merge.go index bbe64aa..ad79e6e 100644 --- a/command/merge.go +++ b/command/merge.go @@ -58,7 +58,6 @@ func (c Merge) action(ctx *cli.Context) error { if err != nil { return errors.WithMessagef(err, "update module config '%s'", moduleName) } - utils.PrintAnswer(result) - return nil + return utils.PrintAnswer(result) } diff --git a/command/schema.go b/command/schema.go index 92def9e..a05c1c7 100644 --- a/command/schema.go +++ b/command/schema.go @@ -55,13 +55,12 @@ func (c Schema) action(ctx *cli.Context) error { schema["schema"] = schemaConfig switch ctx.String(OutPrintFlagName) { case OutPrintJsonValue: - utils.PrintAnswer(schema) + return utils.PrintAnswer(schema) case OutPrintHtmlValue: return c.printHtml(schema) default: return errors.Errorf("invalid flag value, expected [%s] or [%s]", OutPrintJsonValue, OutPrintHtmlValue) } - return nil } func (c Schema) getSchemaConfig(moduleName string) (schema.Schema, error) { diff --git a/command/set.go b/command/set.go index d02d89c..0c1adea 100644 --- a/command/set.go +++ b/command/set.go @@ -58,7 +58,7 @@ func (c Set) action(ctx *cli.Context) error { } if pathObject == "" { - return utils.CreateUpdateConfig(changeObject, config, c.service) + return CreateUpdateConfig(changeObject, config, c.service) } jsonObject, err := json.Marshal(config.Data) if err != nil { @@ -70,5 +70,5 @@ func (c Set) action(ctx *cli.Context) error { if err != nil { return err } - return utils.CreateUpdateConfig(stringToChange, config, c.service) + return CreateUpdateConfig(stringToChange, config, c.service) } diff --git a/command/status.go b/command/status.go index 1b390e1..cfec2e0 100644 --- a/command/status.go +++ b/command/status.go @@ -51,7 +51,7 @@ func (c Status) action(ctx *cli.Context) error { } switch ctx.String(OutPrintFlagName) { case OutPrintJsonValue: - utils.PrintAnswer(arrayOfModules) + return utils.PrintAnswer(arrayOfModules) default: table := tablewriter.NewWriter(os.Stdout) table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false}) diff --git a/command/utils/common.go b/command/utils/common.go index 1bedf53..8f0b32e 100644 --- a/command/utils/common.go +++ b/command/utils/common.go @@ -2,7 +2,7 @@ package utils import ( "fmt" - "io/ioutil" + "io" "os" "strings" @@ -32,7 +32,7 @@ func CheckPath(pathObject string) (string, error) { func CheckChangeObject(changeObject string) (string, error) { if changeObject == "" { - bytes, err := ioutil.ReadAll(os.Stdin) + bytes, err := io.ReadAll(os.Stdin) if err != nil { return "", err } @@ -40,23 +40,15 @@ func CheckChangeObject(changeObject string) (string, error) { } if changeObject == "" { return "", errors.New("expected argument") - } else { - return changeObject, nil } + return changeObject, nil } -func PrintAnswer(data any) { +func PrintAnswer(data any) error { encoder := json.NewEncoder(os.Stdout) encoder.SetEscapeHTML(false) encoder.SetIndent("", " ") - if err := encoder.Encode(data); err != nil { - PrintError(err) - } -} - -func PrintError(err error) { - fmt.Println("ERROR:", err) - os.Exit(-1) + return encoder.Encode(data) } func ParseSetObject(argument string) any { @@ -98,16 +90,15 @@ func ParseSetObject(argument string) any { return argument } -func CheckObject(jsonObject []byte, depth string) { +func CheckObject(jsonObject []byte, depth string) error { jsonString := gjson.GetBytes(jsonObject, depth) if jsonString.Raw == "" { - PrintError(errors.Errorf("path '%s' not found\n", depth)) - } else { - var data any - if err := json.Unmarshal([]byte(jsonString.Raw), &data); err != nil { - PrintError(err) - } else { - PrintAnswer(data) - } + return errors.Errorf("path '%s' not found\n", depth) } + var data any + err := json.Unmarshal([]byte(jsonString.Raw), &data) + if err != nil { + return err + } + return PrintAnswer(data) } diff --git a/command/utils/config.go b/command/utils/config.go deleted file mode 100644 index 2dd6040..0000000 --- a/command/utils/config.go +++ /dev/null @@ -1,19 +0,0 @@ -package utils - -import ( - "ispctl/model" -) - -type UpdateConfigService interface { - CreateUpdateConfig(stringToChange string, configuration *model.Config) (map[string]any, error) -} - -func CreateUpdateConfig(stringToChange string, configuration *model.Config, service UpdateConfigService) error { - answer, err := service.CreateUpdateConfig(stringToChange, configuration) - if err != nil { - return err - } else if answer != nil { - PrintAnswer(answer) - } - return nil -} diff --git a/command/vars.go b/command/vars.go index 3bed455..a810f60 100644 --- a/command/vars.go +++ b/command/vars.go @@ -98,8 +98,7 @@ func (c Variables) getByName(ctx *cli.Context) error { if err != nil { return err } - utils.PrintAnswer(variable) - return nil + return utils.PrintAnswer(variable) } func (c Variables) set(ctx *cli.Context) error {