-
Notifications
You must be signed in to change notification settings - Fork 16
fix: use StringArrayVar for --key flag to preserve special characters #206
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ package update | |
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/briandowns/spinner" | ||
| "github.com/spf13/cobra" | ||
|
|
@@ -16,6 +17,7 @@ type Options struct { | |
| name string | ||
| environmentID string | ||
| keys map[string]string | ||
| rawKeys []string | ||
| skipConfirm bool | ||
| inputDone bool | ||
| } | ||
|
|
@@ -35,7 +37,7 @@ func NewCmdUpdateVariable(f *cmdutil.Factory) *cobra.Command { | |
| util.AddServiceParam(cmd, &opts.id, &opts.name) | ||
| util.AddEnvOfServiceParam(cmd, &opts.environmentID) | ||
| cmd.Flags().BoolVarP(&opts.skipConfirm, "yes", "y", false, "Skip confirmation") | ||
| cmd.Flags().StringToStringVarP(&opts.keys, "key", "k", nil, "Key value pair of the variable") | ||
| cmd.Flags().StringArrayVarP(&opts.rawKeys, "key", "k", nil, "Key value pair of the variable (KEY=VALUE)") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Line 40 accepts Suggested fixfunc runUpdateVariable(f *cmdutil.Factory, opts *Options) error {
+ // If --key is provided, honor flags and skip interactive prompts.
+ if len(opts.rawKeys) > 0 {
+ return runUpdateVariableNonInteractive(f, opts)
+ }
if f.Interactive {
opts.keys = make(map[string]string)
return runUpdateVariableInteractive(f, opts)
}
return runUpdateVariableNonInteractive(f, opts)
}As per coding guidelines: "Commands should support both interactive and non-interactive modes; if a flag is provided, skip the interactive prompt". Also applies to: 122-129 🤖 Prompt for AI Agents |
||
|
|
||
| return cmd | ||
| } | ||
|
|
@@ -104,7 +106,27 @@ func runUpdateVariableInteractive(f *cmdutil.Factory, opts *Options) error { | |
| return runUpdateVariableNonInteractive(f, opts) | ||
| } | ||
|
|
||
| func parseRawKeys(rawKeys []string) (map[string]string, error) { | ||
| result := make(map[string]string, len(rawKeys)) | ||
| for _, raw := range rawKeys { | ||
| parts := strings.SplitN(raw, "=", 2) | ||
| if len(parts) != 2 || parts[0] == "" { | ||
| return nil, fmt.Errorf("invalid --key format %q: expected KEY=VALUE", raw) | ||
| } | ||
| result[parts[0]] = parts[1] | ||
| } | ||
| return result, nil | ||
| } | ||
|
|
||
| func runUpdateVariableNonInteractive(f *cmdutil.Factory, opts *Options) error { | ||
| // Parse rawKeys from StringArrayVar into the keys map (non-interactive mode) | ||
| if opts.keys == nil && len(opts.rawKeys) > 0 { | ||
| parsed, err := parseRawKeys(opts.rawKeys) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| opts.keys = parsed | ||
| } | ||
| if opts.id == "" && opts.name != "" { | ||
| service, err := util.GetServiceByName(f.Config, f.ApiClient, opts.name) | ||
| if err != nil { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--keyshould bypass prompts in interactive sessions, but currently doesn’t.Line 40 captures raw
--keyvalues, yet Line 102-109 parses them only in the non-interactive path. Withf.Interactive=true, users are still forced into prompts even when flags are supplied.Suggested fix
func runCreateVariable(f *cmdutil.Factory, opts *Options) error { + // If --key is provided, execute flag-driven flow directly. + if len(opts.rawKeys) > 0 { + return runCreateVariableNonInteractive(f, opts) + } if f.Interactive { return runCreateVariableInteractive(f, opts) } else { return runCreateVariableNonInteractive(f, opts) } }As per coding guidelines: "Commands should support both interactive and non-interactive modes; if a flag is provided, skip the interactive prompt".
Also applies to: 102-109
🤖 Prompt for AI Agents