MSP-5014 Replace call flag.CheckGlobal(ctx) by calling Before
This commit is contained in:
1 parent
5dbd07dc03
commit
ff2922cf1e
7 files changed
+29
-64
No files matched your search
+9
-34
@@ -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{
|
||||
|
||||
Reference in new issue
Block a user