Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly-parse inputs for commands that take a single --input only #423

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"os"
"os/user"
Expand Down Expand Up @@ -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)

Expand Down
Loading