MSP-5014 Refactor utils

This commit is contained in:
Синельник Тимур Романович committed 2025-03-24 11:30:39 +03:00
1 parent 7c01299e53
commit 8c49ae85ed
10 files changed
+44 -60

No files matched your search

+16
View File
@@ -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
}
+2 -2
View File
@@ -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)
}
+7 -8
View File
@@ -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)
}
+1 -2
View File
@@ -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)
}
+1 -2
View File
@@ -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) {
+2 -2
View File
@@ -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)
}
+1 -1
View File
@@ -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})
+13 -22
View File
@@ -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)
}
-19
View File
@@ -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
}
+1 -2
View File
@@ -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 {