Skip to content

Commit 0bbbb78

Browse files
authored
Support multiple input options in cli (#600)
* Support multiple input options in cli * Add null value support as --input param
1 parent e0fdc8c commit 0bbbb78

File tree

3 files changed

+54
-27
lines changed

3 files changed

+54
-27
lines changed

tools/cli/flags.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,10 @@ func getFlagsForStart() []cli.Flag {
335335
Usage: "Configure if the same workflow Id is allowed for use in new workflow execution. " +
336336
"Options: AllowDuplicate, AllowDuplicateFailedOnly, RejectDuplicate",
337337
},
338-
cli.StringFlag{
339-
Name: FlagInputWithAlias,
340-
Usage: "Optional input for the workflow, in JSON format. If there are multiple parameters, concatenate them and separate by space.",
338+
cli.StringSliceFlag{
339+
Name: FlagInputWithAlias,
340+
Usage: "Optional input for the workflow in JSON format. If there are multiple parameters, pass each as a separate input flag. " +
341+
"Pass \"null\" for null values",
341342
},
342343
cli.StringFlag{
343344
Name: FlagInputFileWithAlias,

tools/cli/util.go

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -738,51 +738,76 @@ func newContextWithTimeout(c *cli.Context, timeout time.Duration) (context.Conte
738738

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

746-
var v interface{}
747-
if err := json.Unmarshal(rawJson, &v); err != nil {
748-
ErrorAndExit("Input is not a valid JSON.", err)
749-
}
743+
var jsons []interface{}
744+
for _, jsonRaw := range jsonsRaw {
745+
if jsonRaw == nil {
746+
jsons = append(jsons, nil)
747+
} else {
748+
var j interface{}
749+
if err := json.Unmarshal(jsonRaw, &j); err != nil {
750+
ErrorAndExit("Input is not a valid JSON.", err)
751+
}
752+
jsons = append(jsons, j)
753+
}
750754

751-
p, err := payloads.Encode(v)
755+
}
756+
p, err := payloads.Encode(jsons...)
752757
if err != nil {
753758
ErrorAndExit("Unable to encode Input.", err)
754759
}
755760

756761
return p
757762
}
758763

759-
// process and validate json
760-
func processJSONInputHelper(c *cli.Context, jType jsonType) []byte {
761-
var flagNameOfRawInput string
762-
var flagNameOfInputFileName string
764+
// read multiple inputs presented in json format
765+
func readJSONInputs(c *cli.Context, jType jsonType) [][]byte {
766+
var flagRawInput string
767+
var flagInputFileName string
763768

764769
switch jType {
765770
case jsonTypeInput:
766-
flagNameOfRawInput = FlagInput
767-
flagNameOfInputFileName = FlagInputFile
771+
flagRawInput = FlagInput
772+
flagInputFileName = FlagInputFile
768773
case jsonTypeMemo:
769-
flagNameOfRawInput = FlagMemo
770-
flagNameOfInputFileName = FlagMemoFile
774+
flagRawInput = FlagMemo
775+
flagInputFileName = FlagMemoFile
771776
default:
772777
return nil
773778
}
774779

775-
if c.IsSet(flagNameOfRawInput) {
776-
return []byte(c.String(flagNameOfRawInput))
777-
} else if c.IsSet(flagNameOfInputFileName) {
778-
inputFile := c.String(flagNameOfInputFileName)
780+
if c.IsSet(flagRawInput) {
781+
inputsG := c.Generic(flagRawInput)
782+
783+
var inputs *cli.StringSlice
784+
var ok bool
785+
if inputs, ok = inputsG.(*cli.StringSlice); !ok {
786+
// input could be provided as StringFlag instead of StringSliceFlag
787+
ss := make(cli.StringSlice, 1)
788+
ss[0] = fmt.Sprintf("%v", inputsG)
789+
inputs = &ss
790+
}
791+
792+
var inputsRaw [][]byte
793+
for _, i := range *inputs {
794+
if strings.EqualFold(i, "null") {
795+
inputsRaw = append(inputsRaw, []byte(nil))
796+
} else {
797+
inputsRaw = append(inputsRaw, []byte(i))
798+
}
799+
}
800+
801+
return inputsRaw
802+
} else if c.IsSet(flagInputFileName) {
803+
inputFile := c.String(flagInputFileName)
779804
// This method is purely used to parse input from the CLI. The input comes from a trusted user
780805
// #nosec
781806
data, err := ioutil.ReadFile(inputFile)
782807
if err != nil {
783808
ErrorAndExit("Error reading input file", err)
784809
}
785-
return data
810+
return [][]byte{data}
786811
}
787812
return nil
788813
}

tools/cli/workflowCommands.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,11 @@ func processMemo(c *cli.Context) map[string]*commonpb.Payload {
322322
memoKeys = strings.Split(rawMemoKey, " ")
323323
}
324324

325-
rawMemoValue := string(processJSONInputHelper(c, jsonTypeMemo))
326-
if rawMemoValue == "" {
325+
jsonsRaw := readJSONInputs(c, jsonTypeMemo)
326+
if len(jsonsRaw) == 0 {
327327
return nil
328328
}
329+
rawMemoValue := string(jsonsRaw[0]) // StringFlag may contain up to one json
329330

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

0 commit comments

Comments
 (0)