diff --git a/internal/cmd/server/command/create/create.go b/internal/cmd/server/command/create/create.go index 1561ab3b5..6ac6fed6d 100644 --- a/internal/cmd/server/command/create/create.go +++ b/internal/cmd/server/command/create/create.go @@ -119,14 +119,15 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, CommandTemplateName: flags.FlagToStringValue(p, cmd, commandTemplateNameFlag), Params: flags.FlagToStringToStringPointer(p, cmd, paramsFlag), } - parsedParams, err := runcommandUtils.ParseScriptParams(*model.Params) + + var err error + model.Params, err = runcommandUtils.ParseScriptParams(model.Params) if err != nil { return nil, &cliErr.FlagValidationError{ Flag: paramsFlag, Details: err.Error(), } } - model.Params = &parsedParams p.DebugInputModel(model) return &model, nil diff --git a/internal/cmd/server/command/create/create_test.go b/internal/cmd/server/command/create/create_test.go index 5b19bf508..8029d65e5 100644 --- a/internal/cmd/server/command/create/create_test.go +++ b/internal/cmd/server/command/create/create_test.go @@ -105,6 +105,16 @@ func TestParseInput(t *testing.T) { flagValues: map[string]string{}, isValid: false, }, + { + description: "params flag missing", + flagValues: fixtureFlagValues(func(flagValues map[string]string) { + delete(flagValues, paramsFlag) + }), + isValid: true, + expectedModel: fixtureInputModel(func(model *inputModel) { + model.Params = nil + }), + }, { description: "project id missing", flagValues: fixtureFlagValues(func(flagValues map[string]string) { diff --git a/internal/pkg/services/runcommand/utils/utils.go b/internal/pkg/services/runcommand/utils/utils.go index d6775f373..358e27c59 100644 --- a/internal/pkg/services/runcommand/utils/utils.go +++ b/internal/pkg/services/runcommand/utils/utils.go @@ -5,13 +5,15 @@ import ( "strings" ) -func ParseScriptParams(params map[string]string) (map[string]string, error) { +func ParseScriptParams(params *map[string]string) (*map[string]string, error) { //nolint:gocritic // flag value is a map pointer if params == nil { return nil, nil } + parsed := map[string]string{} - for k, v := range params { + for k, v := range *params { parsed[k] = v + if k == "script" && strings.HasPrefix(v, "@{") && strings.HasSuffix(v, "}") { // Check if a script file path was specified, like: --params script=@{/tmp/test.sh} fileContents, err := os.ReadFile(v[2 : len(v)-1]) @@ -21,5 +23,6 @@ func ParseScriptParams(params map[string]string) (map[string]string, error) { parsed[k] = string(fileContents) } } - return parsed, nil + + return &parsed, nil } diff --git a/internal/pkg/services/runcommand/utils/utils_test.go b/internal/pkg/services/runcommand/utils/utils_test.go index 5b1d1c69f..320bac18c 100644 --- a/internal/pkg/services/runcommand/utils/utils_test.go +++ b/internal/pkg/services/runcommand/utils/utils_test.go @@ -2,26 +2,34 @@ package utils import ( "testing" + + "github.com/google/go-cmp/cmp" ) func TestParseScriptParams(t *testing.T) { tests := []struct { description string - input map[string]string - expectedOutput map[string]string + input *map[string]string + expectedOutput *map[string]string isValid bool }{ { - "base-ok", - map[string]string{"script": "ls /"}, - map[string]string{"script": "ls /"}, - true, + description: "base-ok", + input: &map[string]string{"script": "ls /"}, + expectedOutput: &map[string]string{"script": "ls /"}, + isValid: true, + }, + { + description: "nil input", + input: nil, + expectedOutput: nil, + isValid: true, }, { - "not-ok-nonexistant-file-specified-for-script", - map[string]string{"script": "@{/some/file/which/does/not/exist/and/thus/fails}"}, - nil, - false, + description: "not-ok-nonexistant-file-specified-for-script", + input: &map[string]string{"script": "@{/some/file/which/does/not/exist/and/thus/fails}"}, + expectedOutput: nil, + isValid: false, }, } @@ -38,8 +46,9 @@ func TestParseScriptParams(t *testing.T) { if !tt.isValid { return } - if output["script"] != tt.expectedOutput["script"] { - t.Errorf("expected output to be %s, got %s", tt.expectedOutput["script"], output["script"]) + diff := cmp.Diff(output, tt.expectedOutput) + if diff != "" { + t.Fatalf("ParseScriptParams() output mismatch (-want +got):\n%s", diff) } }) }