-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(cli): make -y flag properly support non-interactive mode in create #21208
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
Open
ThomasK33
wants to merge
1
commit into
main
Choose a base branch
from
fix-create-non-interactive-y-flag
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+233
−119
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,17 @@ const PresetNone = "none" | |
|
|
||
| var ErrNoPresetFound = xerrors.New("no preset found") | ||
|
|
||
| // isNonInteractive checks if the command is running in non-interactive mode | ||
| // (i.e., the --yes/-y flag was provided). | ||
| func isNonInteractive(inv *serpent.Invocation) bool { | ||
| if inv.ParsedFlags().Lookup("yes") != nil { | ||
|
Contributor
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. not sure if that’s a pattern or not, but can we not do a double look up of string “yes”, there’s gotta be a better way to handle this |
||
| if skip, _ := inv.ParsedFlags().GetBool("yes"); skip { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| type CreateOptions struct { | ||
| BeforeCreate func(ctx context.Context, client *codersdk.Client, template codersdk.Template, templateVersionID uuid.UUID) error | ||
| AfterCreate func(ctx context.Context, inv *serpent.Invocation, client *codersdk.Client, workspace codersdk.Workspace) error | ||
|
|
@@ -75,6 +86,9 @@ func (r *RootCmd) Create(opts CreateOptions) *serpent.Command { | |
| } | ||
|
|
||
| if workspaceName == "" { | ||
| if isNonInteractive(inv) { | ||
| return xerrors.New("workspace name is required in non-interactive mode; provide it as an argument") | ||
| } | ||
| workspaceName, err = cliui.Prompt(inv, cliui.PromptOptions{ | ||
| Text: "Specify a name for your workspace:", | ||
| Validate: func(workspaceName string) error { | ||
|
|
@@ -122,62 +136,76 @@ func (r *RootCmd) Create(opts CreateOptions) *serpent.Command { | |
| var templateVersionID uuid.UUID | ||
| switch { | ||
| case templateName == "": | ||
| _, _ = fmt.Fprintln(inv.Stdout, pretty.Sprint(cliui.DefaultStyles.Wrap, "Select a template below to preview the provisioned infrastructure:")) | ||
|
|
||
| templates, err := client.Templates(inv.Context(), codersdk.TemplateFilter{}) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| slices.SortFunc(templates, func(a, b codersdk.Template) int { | ||
| return slice.Descending(a.ActiveUserCount, b.ActiveUserCount) | ||
| }) | ||
|
|
||
| templateNames := make([]string, 0, len(templates)) | ||
| templateByName := make(map[string]codersdk.Template, len(templates)) | ||
| // In non-interactive mode, auto-select if only one template exists, | ||
| // otherwise require explicit --template flag. | ||
| if isNonInteractive(inv) { | ||
| if len(templates) == 0 { | ||
| return xerrors.New("no templates available") | ||
| } | ||
| if len(templates) > 1 { | ||
| return xerrors.New("multiple templates available; use the --template flag to specify which one") | ||
| } | ||
| // Only one template available - auto-select it | ||
| template = templates[0] | ||
| templateVersionID = template.ActiveVersionID | ||
| } else { | ||
| _, _ = fmt.Fprintln(inv.Stdout, pretty.Sprint(cliui.DefaultStyles.Wrap, "Select a template below to preview the provisioned infrastructure:")) | ||
|
|
||
| slices.SortFunc(templates, func(a, b codersdk.Template) int { | ||
| return slice.Descending(a.ActiveUserCount, b.ActiveUserCount) | ||
| }) | ||
|
|
||
| // If more than 1 organization exists in the list of templates, | ||
| // then include the organization name in the select options. | ||
| uniqueOrganizations := make(map[uuid.UUID]bool) | ||
| for _, template := range templates { | ||
| uniqueOrganizations[template.OrganizationID] = true | ||
| } | ||
| templateNames := make([]string, 0, len(templates)) | ||
| templateByName := make(map[string]codersdk.Template, len(templates)) | ||
|
|
||
| for _, template := range templates { | ||
| templateName := template.Name | ||
| if len(uniqueOrganizations) > 1 { | ||
| templateName += cliui.Placeholder( | ||
| fmt.Sprintf( | ||
| " (%s)", | ||
| template.OrganizationName, | ||
| ), | ||
| ) | ||
| // If more than 1 organization exists in the list of templates, | ||
| // then include the organization name in the select options. | ||
| uniqueOrganizations := make(map[uuid.UUID]bool) | ||
| for _, tpl := range templates { | ||
| uniqueOrganizations[tpl.OrganizationID] = true | ||
| } | ||
|
|
||
| if template.ActiveUserCount > 0 { | ||
| templateName += cliui.Placeholder( | ||
| fmt.Sprintf( | ||
| " used by %s", | ||
| formatActiveDevelopers(template.ActiveUserCount), | ||
| ), | ||
| ) | ||
| for _, tpl := range templates { | ||
| tplName := tpl.Name | ||
| if len(uniqueOrganizations) > 1 { | ||
| tplName += cliui.Placeholder( | ||
| fmt.Sprintf( | ||
| " (%s)", | ||
| tpl.OrganizationName, | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| if tpl.ActiveUserCount > 0 { | ||
| tplName += cliui.Placeholder( | ||
| fmt.Sprintf( | ||
| " used by %s", | ||
| formatActiveDevelopers(tpl.ActiveUserCount), | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| templateNames = append(templateNames, tplName) | ||
| templateByName[tplName] = tpl | ||
| } | ||
|
|
||
| templateNames = append(templateNames, templateName) | ||
| templateByName[templateName] = template | ||
| } | ||
| // Move the cursor up a single line for nicer display! | ||
| option, err := cliui.Select(inv, cliui.SelectOptions{ | ||
| Options: templateNames, | ||
| HideSearch: true, | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Move the cursor up a single line for nicer display! | ||
| option, err := cliui.Select(inv, cliui.SelectOptions{ | ||
| Options: templateNames, | ||
| HideSearch: true, | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| template = templateByName[option] | ||
| templateVersionID = template.ActiveVersionID | ||
| } | ||
|
|
||
| template = templateByName[option] | ||
| templateVersionID = template.ActiveVersionID | ||
| case sourceWorkspace.LatestBuild.TemplateVersionID != uuid.Nil: | ||
| template, err = client.Template(inv.Context(), sourceWorkspace.TemplateID) | ||
| if err != nil { | ||
|
|
@@ -297,19 +325,28 @@ func (r *RootCmd) Create(opts CreateOptions) *serpent.Command { | |
| if !errors.Is(err, ErrNoPresetFound) { | ||
| return xerrors.Errorf("unable to resolve preset: %w", err) | ||
| } | ||
| // If no preset found, prompt the user to choose a preset | ||
| if preset, err = promptPresetSelection(inv, tvPresets); err != nil { | ||
| return xerrors.Errorf("unable to prompt user for preset: %w", err) | ||
| // No preset found - in non-interactive mode, skip presets instead of prompting | ||
| if isNonInteractive(inv) { | ||
| // Leave preset as nil, effectively skipping presets | ||
| preset = nil | ||
| } else { | ||
| // Interactive mode - prompt the user to choose a preset | ||
| if preset, err = promptPresetSelection(inv, tvPresets); err != nil { | ||
| return xerrors.Errorf("unable to prompt user for preset: %w", err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Convert preset parameters into workspace build parameters | ||
| presetParameters = presetParameterAsWorkspaceBuildParameters(preset.Parameters) | ||
| // Inform the user which preset was applied and its parameters | ||
| displayAppliedPreset(inv, preset, presetParameters) | ||
| } else { | ||
| // Convert preset parameters into workspace build parameters (if a preset was selected) | ||
| if preset != nil { | ||
| presetParameters = presetParameterAsWorkspaceBuildParameters(preset.Parameters) | ||
| // Inform the user which preset was applied and its parameters | ||
| displayAppliedPreset(inv, preset, presetParameters) | ||
| } | ||
| } | ||
| if preset == nil { | ||
| // Inform the user that no preset was applied | ||
| _, _ = fmt.Fprintf(inv.Stdout, "%s", cliui.Bold("No preset applied.")) | ||
| _, _ = fmt.Fprintf(inv.Stdout, "%s\n", cliui.Bold("No preset applied.")) | ||
| } | ||
|
|
||
| if opts.BeforeCreate != nil { | ||
|
|
@@ -330,6 +367,8 @@ func (r *RootCmd) Create(opts CreateOptions) *serpent.Command { | |
| RichParameterDefaults: cliBuildParameterDefaults, | ||
|
|
||
| SourceWorkspaceParameters: sourceWorkspaceParameters, | ||
|
|
||
| NonInteractive: isNonInteractive(inv), | ||
| }) | ||
| if err != nil { | ||
| return xerrors.Errorf("prepare build: %w", err) | ||
|
|
@@ -460,6 +499,8 @@ type prepWorkspaceBuildArgs struct { | |
| RichParameters []codersdk.WorkspaceBuildParameter | ||
| RichParameterFile string | ||
| RichParameterDefaults []codersdk.WorkspaceBuildParameter | ||
|
|
||
| NonInteractive bool | ||
| } | ||
|
|
||
| // resolvePreset returns the preset matching the given presetName (if specified), | ||
|
|
@@ -562,7 +603,8 @@ func prepWorkspaceBuild(inv *serpent.Invocation, client *codersdk.Client, args p | |
| WithPromptRichParameters(args.PromptRichParameters). | ||
| WithRichParameters(args.RichParameters). | ||
| WithRichParametersFile(parameterFile). | ||
| WithRichParametersDefaults(args.RichParameterDefaults) | ||
| WithRichParametersDefaults(args.RichParameterDefaults). | ||
| WithNonInteractive(args.NonInteractive) | ||
| buildParameters, err := resolver.Resolve(inv, args.Action, templateVersionParameters) | ||
| if err != nil { | ||
| return nil, err | ||
|
|
@@ -572,6 +614,7 @@ func prepWorkspaceBuild(inv *serpent.Invocation, client *codersdk.Client, args p | |
| Fetch: func(ctx context.Context) ([]codersdk.TemplateVersionExternalAuth, error) { | ||
| return client.TemplateVersionExternalAuth(ctx, templateVersion.ID) | ||
| }, | ||
| NonInteractive: args.NonInteractive, | ||
| }) | ||
| if err != nil { | ||
| return nil, xerrors.Errorf("template version git auth: %w", err) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This should probably go into the cliui package beside the SkipPromptOption function