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

Support multiple input options in cli #600

Merged
merged 4 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions tools/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,10 @@ func getFlagsForStart() []cli.Flag {
Usage: "Configure if the same workflow Id is allowed for use in new workflow execution. " +
"Options: AllowDuplicate, AllowDuplicateFailedOnly, RejectDuplicate",
},
cli.StringFlag{
Name: FlagInputWithAlias,
Usage: "Optional input for the workflow, in JSON format. If there are multiple parameters, concatenate them and separate by space.",
cli.StringSliceFlag{
Name: FlagInputWithAlias,
Usage: "Optional input for the workflow in JSON format. If there are multiple parameters, pass each as a separate input flag. " +
"Pass \"null\" for null values",
},
cli.StringFlag{
Name: FlagInputFileWithAlias,
Expand Down
69 changes: 47 additions & 22 deletions tools/cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,51 +738,76 @@ func newContextWithTimeout(c *cli.Context, timeout time.Duration) (context.Conte

// process and validate input provided through cmd or file
func processJSONInput(c *cli.Context) *commonpb.Payloads {
rawJson := processJSONInputHelper(c, jsonTypeInput)
if len(rawJson) == 0 {
return nil
}
jsonsRaw := readJSONInputs(c, jsonTypeInput)

var v interface{}
if err := json.Unmarshal(rawJson, &v); err != nil {
ErrorAndExit("Input is not a valid JSON.", err)
}
var jsons []interface{}
for _, jsonRaw := range jsonsRaw {
if jsonRaw == nil {
jsons = append(jsons, nil)
} else {
var j interface{}
if err := json.Unmarshal(jsonRaw, &j); err != nil {
ErrorAndExit("Input is not a valid JSON.", err)
}
jsons = append(jsons, j)
}

p, err := payloads.Encode(v)
}
p, err := payloads.Encode(jsons...)
if err != nil {
ErrorAndExit("Unable to encode Input.", err)
}

return p
}

// process and validate json
func processJSONInputHelper(c *cli.Context, jType jsonType) []byte {
var flagNameOfRawInput string
var flagNameOfInputFileName string
// read multiple inputs presented in json format
func readJSONInputs(c *cli.Context, jType jsonType) [][]byte {
var flagRawInput string
var flagInputFileName string

switch jType {
case jsonTypeInput:
flagNameOfRawInput = FlagInput
flagNameOfInputFileName = FlagInputFile
flagRawInput = FlagInput
flagInputFileName = FlagInputFile
case jsonTypeMemo:
flagNameOfRawInput = FlagMemo
flagNameOfInputFileName = FlagMemoFile
flagRawInput = FlagMemo
flagInputFileName = FlagMemoFile
default:
return nil
}

if c.IsSet(flagNameOfRawInput) {
return []byte(c.String(flagNameOfRawInput))
} else if c.IsSet(flagNameOfInputFileName) {
inputFile := c.String(flagNameOfInputFileName)
if c.IsSet(flagRawInput) {
inputsG := c.Generic(flagRawInput)

var inputs *cli.StringSlice
var ok bool
if inputs, ok = inputsG.(*cli.StringSlice); !ok {
// input could be provided as StringFlag instead of StringSliceFlag
ss := make(cli.StringSlice, 1)
ss[0] = fmt.Sprintf("%v", inputsG)
mastermanu marked this conversation as resolved.
Show resolved Hide resolved
inputs = &ss
}

var inputsRaw [][]byte
for _, i := range *inputs {
if strings.EqualFold(i, "null") {
inputsRaw = append(inputsRaw, []byte(nil))
} else {
inputsRaw = append(inputsRaw, []byte(i))
}
}

return inputsRaw
} else if c.IsSet(flagInputFileName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is multiple-file input in the next PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope so.

inputFile := c.String(flagInputFileName)
// This method is purely used to parse input from the CLI. The input comes from a trusted user
// #nosec
data, err := ioutil.ReadFile(inputFile)
if err != nil {
ErrorAndExit("Error reading input file", err)
}
return data
return [][]byte{data}
}
return nil
}
Expand Down
5 changes: 3 additions & 2 deletions tools/cli/workflowCommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,11 @@ func processMemo(c *cli.Context) map[string]*commonpb.Payload {
memoKeys = strings.Split(rawMemoKey, " ")
}

rawMemoValue := string(processJSONInputHelper(c, jsonTypeMemo))
if rawMemoValue == "" {
jsonsRaw := readJSONInputs(c, jsonTypeMemo)
if len(jsonsRaw) == 0 {
return nil
}
rawMemoValue := string(jsonsRaw[0]) // StringFlag may contain up to one json

if err := validateJSONs(rawMemoValue); err != nil {
ErrorAndExit("Input is not valid JSON, or JSONs concatenated with spaces/newlines.", err)
Expand Down