From 61d2a85b794936faa9b05bb0fb13476990642e11 Mon Sep 17 00:00:00 2001 From: Josh Berry Date: Mon, 29 Jan 2024 15:51:19 -0800 Subject: [PATCH] Properly-parse inputs for commands that take a single `--input` only For commands which only allow a single instance of `--input` to be passed, we were improperly trying to parse `--input` values as multi-valued things when they are not. Stop doing this. Closes #411. --- common/util.go | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/common/util.go b/common/util.go index c13ff9d2..4d7d0360 100644 --- a/common/util.go +++ b/common/util.go @@ -6,6 +6,7 @@ import ( "context" "encoding/json" "errors" + "flag" "fmt" "os" "os/user" @@ -416,25 +417,27 @@ func readJSONInputs(c *cli.Context) ([][]byte, error) { if c.IsSet(FlagInput) { inputsG := c.Generic(FlagInput) - var inputs *cli.StringSlice - var ok bool - if inputs, ok = inputsG.(*cli.StringSlice); !ok { - // input could be provided as StringFlag instead of StringSliceFlag - ss := cli.StringSlice{} - ss.Set(fmt.Sprintf("%v", inputsG)) - inputs = &ss + if inputs, ok := inputsG.(*cli.StringSlice); ok { + // The --input flag can be specified multiple times with strings + var inputsRaw [][]byte + for _, i := range inputs.Value() { + if strings.EqualFold(i, "null") { + inputsRaw = append(inputsRaw, []byte(nil)) + } else { + inputsRaw = append(inputsRaw, []byte(i)) + } + } + + return inputsRaw, nil } - var inputsRaw [][]byte - for _, i := range inputs.Value() { - if strings.EqualFold(i, "null") { - inputsRaw = append(inputsRaw, []byte(nil)) - } else { - inputsRaw = append(inputsRaw, []byte(i)) - } + if input, ok := inputsG.(flag.Value); ok { + // The --input flag accepts only a single string + return [][]byte{[]byte(input.String())}, nil } - return inputsRaw, nil + return nil, fmt.Errorf("BUG: Don't know what the type of the --input flag should be") + } else if c.IsSet(FlagInputFile) { inputFiles := c.StringSlice(FlagInputFile)