MSP-5014 Replace call flag.CheckGlobal(ctx) by calling Before

This commit is contained in:
Синельник Тимур Романович committed 2025-03-20 18:17:59 +03:00
1 parent 5dbd07dc03
commit ff2922cf1e
7 files changed
+29 -64

No files matched your search

+1 -3
View File
@@ -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)
+1
View File
@@ -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,
}
+1 -4
View File
@@ -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 {
+15 -16
View File
@@ -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
}
+1 -4
View File
@@ -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)
+1 -3
View File
@@ -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 {
+9 -34
View File
@@ -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 <name>",
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 <name> <value>",
Before: flag.CheckGlobal,
Action: varsComm.set,
Flags: []cli.Flag{
flag.SetVariableSecretType,
@@ -43,12 +45,14 @@ func VariablesCommands() *cli.Command {
{
Name: "delete",
Usage: "vars delete <name>",
Before: flag.CheckGlobal,
Action: varsComm.delete,
BashComplete: bash.Get(bash.VariableName, bash.Empty).Complete,
},
{
Name: "upload",
Usage: "vars upload <filepath>",
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{