From ff2922cf1e9227a94f7e7c556a31f9cd33fd9a5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B8=D0=BD=D0=B5=D0=BB=D1=8C=D0=BD=D0=B8=D0=BA=20?= =?UTF-8?q?=D0=A2=D0=B8=D0=BC=D1=83=D1=80=20=D0=A0=D0=BE=D0=BC=D0=B0=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D0=B8=D1=87?= Date: Thu, 20 Mar 2025 18:17:59 +0300 Subject: [PATCH] MSP-5014 Replace call flag.CheckGlobal(ctx) by calling Before --- command/delete.go | 4 +--- command/get.go | 1 + command/merge.go | 5 +---- command/schema.go | 31 +++++++++++++++---------------- command/set.go | 5 +---- command/status.go | 4 +--- command/vars.go | 43 +++++++++---------------------------------- 7 files changed, 29 insertions(+), 64 deletions(-) diff --git a/command/delete.go b/command/delete.go index 484a725..f9a4024 100644 --- a/command/delete.go +++ b/command/delete.go @@ -15,6 +15,7 @@ func Delete() *cli.Command { return &cli.Command{ Name: "delete", Usage: "delete configuration by module_name", + Before: flag.CheckGlobal, Action: deleteComm.action, BashComplete: bash.Get(bash.ModuleName, bash.ModuleData).Complete, } @@ -25,9 +26,6 @@ var deleteComm deleteCommand type deleteCommand struct{} func (d deleteCommand) action(ctx *cli.Context) error { - if err := flag.CheckGlobal(ctx); err != nil { - return err - } moduleName := ctx.Args().First() pathObject := ctx.Args().Get(1) diff --git a/command/get.go b/command/get.go index 6d557eb..b49eee8 100644 --- a/command/get.go +++ b/command/get.go @@ -14,6 +14,7 @@ func Get() *cli.Command { return &cli.Command{ Name: "get", Usage: "get configuration by module_name", + Before: flag.CheckGlobal, Action: get.action, BashComplete: bash.Get(bash.ModuleName, bash.ModuleData).Complete, } diff --git a/command/merge.go b/command/merge.go index 3332e64..866f7aa 100644 --- a/command/merge.go +++ b/command/merge.go @@ -17,11 +17,8 @@ func Merge() *cli.Command { Name: "merge", Usage: "merge actual config with config from stdin", BashComplete: bash.Get(bash.CommonConfigName, bash.Empty).Complete, + Before: flag.CheckGlobal, Action: func(context *cli.Context) error { - err := flag.CheckGlobal(context) - if err != nil { - return err - } moduleName := context.Args().First() config, err := service.Config.GetConfigurationByModuleName(moduleName) if err != nil { diff --git a/command/schema.go b/command/schema.go index 8df0683..698313e 100644 --- a/command/schema.go +++ b/command/schema.go @@ -17,6 +17,7 @@ func Schema() *cli.Command { return &cli.Command{ Name: "schema", Usage: "get schema configuration by module_name", + Before: flag.CheckGlobal, Action: schema.action, Flags: []cli.Flag{ flag.OutPrintSchema, @@ -30,23 +31,21 @@ var schema schemaCommand type schemaCommand struct{} func (s schemaCommand) action(ctx *cli.Context) error { - if err := flag.CheckGlobal(ctx); err != nil { - return err - } moduleName := ctx.Args().First() - - if schemaConfig := s.getSchemaConfig(moduleName); schemaConfig != nil { - schema := make(map[string]any) - schema["title"] = moduleName - schema["schema"] = schemaConfig - switch ctx.String(flag.OutPrintSchema.Name) { - case flag.OutPrintJsonValue: - utils.PrintAnswer(schema) - case flag.OutPrintHtmlValue: - s.printHtml(schema) - default: - return errors.Errorf("invalid flag value, expected [%s] or [%s]", flag.OutPrintJsonValue, flag.OutPrintHtmlValue) - } + schemaConfig := s.getSchemaConfig(moduleName) + if schemaConfig == nil { + return nil + } + schema := make(map[string]any) + schema["title"] = moduleName + schema["schema"] = schemaConfig + switch ctx.String(flag.OutPrintSchema.Name) { + case flag.OutPrintJsonValue: + utils.PrintAnswer(schema) + case flag.OutPrintHtmlValue: + s.printHtml(schema) + default: + return errors.Errorf("invalid flag value, expected [%s] or [%s]", flag.OutPrintJsonValue, flag.OutPrintHtmlValue) } return nil } diff --git a/command/set.go b/command/set.go index f963f53..adf57dc 100644 --- a/command/set.go +++ b/command/set.go @@ -15,6 +15,7 @@ func Set() *cli.Command { return &cli.Command{ Name: "set", Usage: "set configuration by module_name", + Before: flag.CheckGlobal, Action: set.action, BashComplete: bash.Get(bash.ModuleName, bash.ModuleData).Complete, } @@ -25,10 +26,6 @@ var set setCommand type setCommand struct{} func (s setCommand) action(ctx *cli.Context) error { - if err := flag.CheckGlobal(ctx); err != nil { - return err - } - moduleName := ctx.Args().First() pathObject := ctx.Args().Get(1) changeObject := ctx.Args().Get(2) diff --git a/command/status.go b/command/status.go index 72dccb7..9fb5dee 100644 --- a/command/status.go +++ b/command/status.go @@ -25,6 +25,7 @@ func Status() *cli.Command { return &cli.Command{ Name: "status", Usage: "get available configs", + Before: flag.CheckGlobal, Action: status.action, Flags: []cli.Flag{ flag.OutPrintStatus, @@ -37,9 +38,6 @@ var status statusCommand type statusCommand struct{} func (statusCommand) action(ctx *cli.Context) error { - if err := flag.CheckGlobal(ctx); err != nil { - return err - } if arrayOfModules, err := service.Config.GetAvailableConfigs(); err != nil { return err } else { diff --git a/command/vars.go b/command/vars.go index 5102cd8..9262906 100644 --- a/command/vars.go +++ b/command/vars.go @@ -3,6 +3,8 @@ package command import ( "encoding/csv" "fmt" + "github.com/olekukonko/tablewriter" + "github.com/urfave/cli/v2" "ispctl/bash" "ispctl/command/flag" "ispctl/command/utils" @@ -10,9 +12,6 @@ import ( "ispctl/service" "os" "strings" - - "github.com/olekukonko/tablewriter" - "github.com/urfave/cli/v2" ) func VariablesCommands() *cli.Command { @@ -23,17 +22,20 @@ func VariablesCommands() *cli.Command { { Name: "get", Usage: "vars get ", + Before: flag.CheckGlobal, Action: varsComm.getByName, BashComplete: bash.Get(bash.VariableName, bash.Empty).Complete, }, { Name: "list", Usage: "list all variables", + Before: flag.CheckGlobal, Action: varsComm.list, }, { Name: "set", Usage: "vars set ", + Before: flag.CheckGlobal, Action: varsComm.set, Flags: []cli.Flag{ flag.SetVariableSecretType, @@ -43,12 +45,14 @@ func VariablesCommands() *cli.Command { { Name: "delete", Usage: "vars delete ", + Before: flag.CheckGlobal, Action: varsComm.delete, BashComplete: bash.Get(bash.VariableName, bash.Empty).Complete, }, { Name: "upload", Usage: "vars upload ", + Before: flag.CheckGlobal, Action: varsComm.upload, }, }, @@ -60,17 +64,12 @@ var varsComm varsCommands type varsCommands struct{} func (g varsCommands) list(ctx *cli.Context) error { - if err := flag.CheckGlobal(ctx); err != nil { - return err - } - vars, err := service.Config.GetAllVariables() if err != nil { return err } table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Name", "Description", "Type", "Value", "Configs"}) - for _, v := range vars { configs := make([]string, 0, len(v.ContainsInConfigs)) for _, c := range v.ContainsInConfigs { @@ -78,7 +77,6 @@ func (g varsCommands) list(ctx *cli.Context) error { } table.Append([]string{v.Name, v.Description, v.Type, v.Value, strings.Join(configs, ",")}) } - table.SetBorder(true) table.SetRowLine(true) table.Render() @@ -86,10 +84,6 @@ func (g varsCommands) list(ctx *cli.Context) error { } func (g varsCommands) getByName(ctx *cli.Context) error { - if err := flag.CheckGlobal(ctx); err != nil { - return err - } - variableName := ctx.Args().Get(0) variable, err := service.Config.GetVariableByName(variableName) if err != nil { @@ -100,14 +94,9 @@ func (g varsCommands) getByName(ctx *cli.Context) error { } func (g varsCommands) set(ctx *cli.Context) error { - if err := flag.CheckGlobal(ctx); err != nil { - return err - } - variableName := ctx.Args().Get(0) variableValue := ctx.Args().Get(1) variableType := entity.TextVariableType - if ctx.Bool(flag.SetVariableSecretType.Name) { variableType = entity.SecretVariableType } @@ -119,25 +108,14 @@ func (g varsCommands) set(ctx *cli.Context) error { } func (g varsCommands) delete(ctx *cli.Context) error { - if err := flag.CheckGlobal(ctx); err != nil { - return err - } - - variableName := ctx.Args().Get(0) - return service.Config.DeleteVariable(variableName) + return service.Config.DeleteVariable(ctx.Args().Get(0)) } func (g varsCommands) upload(ctx *cli.Context) error { - if err := flag.CheckGlobal(ctx); err != nil { - return err - } - - filepath := ctx.Args().First() - variables, err := g.readVariablesFromCsv(filepath) + variables, err := g.readVariablesFromCsv(ctx.Args().First()) if err != nil { return err } - return service.Config.UpsertVariables(variables) } @@ -147,7 +125,6 @@ func (g varsCommands) readVariablesFromCsv(filepath string) ([]entity.UpsertVari return nil, fmt.Errorf("failed to open file %s: %w", filepath, err) } defer file.Close() - reader := csv.NewReader(file) reader.TrimLeadingSpace = true records, err := reader.ReadAll() @@ -157,13 +134,11 @@ func (g varsCommands) readVariablesFromCsv(filepath string) ([]entity.UpsertVari if len(records) < 2 { return nil, fmt.Errorf("CSV file must have a header and at least one data row") } - header := records[0] columnIndexes := map[string]int{} for i, col := range header { columnIndexes[strings.ToLower(col)] = i } - variables := make([]entity.UpsertVariableRequest, 0, len(records)-1) for _, row := range records[1:] { variables = append(variables, entity.UpsertVariableRequest{