diff --git a/cmd/shellExecute.go b/cmd/shellExecute.go index e05b37996..0cc51175d 100644 --- a/cmd/shellExecute.go +++ b/cmd/shellExecute.go @@ -26,6 +26,10 @@ type shellExecuteUtilsBundle struct { *piperhttp.Client } +const ( + argumentDelimter = "," +) + func newShellExecuteUtils() shellExecuteUtils { utils := shellExecuteUtilsBundle{ Command: &command.Command{}, @@ -71,7 +75,7 @@ func runShellExecute(config *shellExecuteOptions, telemetryData *telemetry.Custo args := []string{} if len(config.ScriptArguments) > 0 && isArgumentAtPosition(config.ScriptArguments, position) { - args = strings.Split(config.ScriptArguments[position], " ") + args = strings.Split(config.ScriptArguments[position], argumentDelimter) } log.Entry().Info("starting running script:", source) diff --git a/cmd/shellExecute_generated.go b/cmd/shellExecute_generated.go index 0702e551d..fe1a40734 100644 --- a/cmd/shellExecute_generated.go +++ b/cmd/shellExecute_generated.go @@ -117,7 +117,7 @@ func ShellExecuteCommand() *cobra.Command { func addShellExecuteFlags(cmd *cobra.Command, stepConfig *shellExecuteOptions) { cmd.Flags().StringSliceVar(&stepConfig.Sources, "sources", []string{}, "Scripts paths that must be present in the current workspace or https links to scripts. Only https urls from github are allowed and must be in the format :https://{githubBaseurl}/api/v3/repos/{owner}/{repository}/contents/{path to script} Authentication for the download is only supported via the 'githubToken' param. Make sure the script has the necessary execute permissions.") cmd.Flags().StringVar(&stepConfig.GithubToken, "githubToken", os.Getenv("PIPER_githubToken"), "GitHub personal access token as per https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line") - cmd.Flags().StringSliceVar(&stepConfig.ScriptArguments, "scriptArguments", []string{}, "scriptArguments that are needed to be passed to scripts. the scriptArguments list is a flat list and has a positional relationship to the `sources` param. For e.g. The scriptArguments string at position 1 will be considered as the argument(s) for script at position 1 in `sources` list") + cmd.Flags().StringSliceVar(&stepConfig.ScriptArguments, "scriptArguments", []string{}, "scriptArguments that are needed to be passed to scripts. the scriptArguments list is a flat list and has a positional relationship to the `sources` param. For e.g. The scriptArguments string at position 1 will be considered as the argument(s) for script at position 1 in `sources` list. For multiple arguments for a script please add them as a comma seperated string.") } diff --git a/cmd/shellExecute_test.go b/cmd/shellExecute_test.go index 52e64a2ac..3e9083b79 100644 --- a/cmd/shellExecute_test.go +++ b/cmd/shellExecute_test.go @@ -121,7 +121,7 @@ func TestRunShellExecute(t *testing.T) { t.Run("success case - multiple positional script arguments gets added to the correct script", func(t *testing.T) { o := &shellExecuteOptions{ Sources: []string{"path1/script1.sh", "path2/script2.sh"}, - ScriptArguments: []string{"arg1 arg2", "arg3 arg4"}, + ScriptArguments: []string{"arg1,arg2", "arg3,arg4"}, } u := newShellExecuteTestsUtils() @@ -136,4 +136,26 @@ func TestRunShellExecute(t *testing.T) { assert.Equal(t, []string{"arg3", "arg4"}, u.ExecMockRunner.Calls[1].Params) assert.NoError(t, err) }) + + t.Run("success case - no argument for script 1, single argument for script 2 and multiple argument for script3 gets added to the correct script", func(t *testing.T) { + o := &shellExecuteOptions{ + Sources: []string{"path1/script1.sh", "path2/script2.sh", "path3/script3.sh"}, + ScriptArguments: []string{"", "arg2.1", "arg3.1,arg3.2"}, + } + + u := newShellExecuteTestsUtils() + u.AddFile("path1/script1.sh", []byte(`echo dummy1`)) + u.AddFile("path2/script2.sh", []byte(`echo dummy2`)) + u.AddFile("path3/script3.sh", []byte(`echo dummy3`)) + + err := runShellExecute(o, nil, u) + + assert.Equal(t, "path1/script1.sh", u.ExecMockRunner.Calls[0].Exec) + assert.Equal(t, []string{}, u.ExecMockRunner.Calls[0].Params) + assert.Equal(t, "path2/script2.sh", u.ExecMockRunner.Calls[1].Exec) + assert.Equal(t, []string{"arg2.1"}, u.ExecMockRunner.Calls[1].Params) + assert.Equal(t, "path3/script3.sh", u.ExecMockRunner.Calls[2].Exec) + assert.Equal(t, []string{"arg3.1", "arg3.2"}, u.ExecMockRunner.Calls[2].Params) + assert.NoError(t, err) + }) } diff --git a/resources/metadata/shellExecute.yaml b/resources/metadata/shellExecute.yaml index dafe67d51..badd0f77c 100644 --- a/resources/metadata/shellExecute.yaml +++ b/resources/metadata/shellExecute.yaml @@ -43,7 +43,7 @@ spec: - STAGES - STEPS description: scriptArguments that are needed to be passed to scripts. the scriptArguments list is a flat list and has a positional relationship to the `sources` param. - For e.g. The scriptArguments string at position 1 will be considered as the argument(s) for script at position 1 in `sources` list + For e.g. The scriptArguments string at position 1 will be considered as the argument(s) for script at position 1 in `sources` list. For multiple arguments for a script please add them as a comma seperated string. mandatory: false containers: - name: shell