From cf4e147ef4be08c509f8932d7dc923fdd0531ae7 Mon Sep 17 00:00:00 2001 From: Pulak Kanti Bhowmick Date: Tue, 5 Nov 2024 12:35:11 +0600 Subject: [PATCH 1/2] use default value for required arg if not provided Signed-off-by: Pulak Kanti Bhowmick --- cmd/cmd_utils.go | 16 ++++++++++++---- examples/demo-custom-command/atmos.yaml | 2 +- pkg/schema/schema.go | 2 ++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/cmd/cmd_utils.go b/cmd/cmd_utils.go index 2477679de..cfe349621 100644 --- a/cmd/cmd_utils.go +++ b/cmd/cmd_utils.go @@ -165,19 +165,27 @@ func preCustomCommand( parentCommand *cobra.Command, commandConfig *schema.Command, ) { - var sb strings.Builder if len(args) != len(commandConfig.Arguments) { + var sb strings.Builder + missingArgCount := 0 sb.WriteString(fmt.Sprintf("Command requires %d argument(s):\n", len(commandConfig.Arguments))) - for i, arg := range commandConfig.Arguments { + for _, arg := range commandConfig.Arguments { + if !arg.Required || arg.Default != "" { + continue + } if arg.Name == "" { u.LogErrorAndExit(schema.CliConfiguration{}, errors.New("invalid argument configuration: empty argument name")) } - sb.WriteString(fmt.Sprintf(" %d. %s\n", i+1, arg.Name)) + missingArgCount += 1 + sb.WriteString(fmt.Sprintf(" %d. %s\n", missingArgCount, arg.Name)) } if len(args) > 0 { sb.WriteString(fmt.Sprintf("\nReceived %d argument(s): %s", len(args), strings.Join(args, ", "))) } - u.LogErrorAndExit(schema.CliConfiguration{}, errors.New(sb.String())) + + if missingArgCount > 0 { + u.LogErrorAndExit(schema.CliConfiguration{}, errors.New(sb.String())) + } } // no "steps" means a sub command should be specified diff --git a/examples/demo-custom-command/atmos.yaml b/examples/demo-custom-command/atmos.yaml index 9323748eb..bbfef88c2 100644 --- a/examples/demo-custom-command/atmos.yaml +++ b/examples/demo-custom-command/atmos.yaml @@ -52,7 +52,7 @@ commands: description: >- The GitHub repository to fetch the stargazers count for. e.g. cloudposse/atmos - required: true + required: false default: cloudposse/atmos steps: - curl -s https://api.github.com/repos/{{ .Arguments.repo }} | jq -r .stargazers_count diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go index 89e90b061..8eacb3d2f 100644 --- a/pkg/schema/schema.go +++ b/pkg/schema/schema.go @@ -308,6 +308,8 @@ type Command struct { type CommandArgument struct { Name string `yaml:"name" json:"name" mapstructure:"name"` Description string `yaml:"description" json:"description" mapstructure:"description"` + Required bool `yaml:"required" json:"required" mapstructure:"required"` + Default any `yaml:"default" json:"default" mapstructure: "default"` } type CommandFlag struct { From 2879e2c144102fef2555a692dbead89b725c0b27 Mon Sep 17 00:00:00 2001 From: Pulak Kanti Bhowmick Date: Fri, 8 Nov 2024 00:23:19 +0600 Subject: [PATCH 2/2] Update examples/demo-custom-command/atmos.yaml Co-authored-by: Erik Osterman (CEO @ Cloud Posse) --- examples/demo-custom-command/atmos.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/demo-custom-command/atmos.yaml b/examples/demo-custom-command/atmos.yaml index bbfef88c2..9323748eb 100644 --- a/examples/demo-custom-command/atmos.yaml +++ b/examples/demo-custom-command/atmos.yaml @@ -52,7 +52,7 @@ commands: description: >- The GitHub repository to fetch the stargazers count for. e.g. cloudposse/atmos - required: false + required: true default: cloudposse/atmos steps: - curl -s https://api.github.com/repos/{{ .Arguments.repo }} | jq -r .stargazers_count