diff --git a/cli/create.go b/cli/create.go index 9bc4e161a8f..a4d3e1b3880 100644 --- a/cli/create.go +++ b/cli/create.go @@ -519,17 +519,21 @@ func resolvePreset(presets []codersdk.Preset, presetName string) (*codersdk.Pres return nil, ErrNoPresetFound } -// presetNoneOption is the selection menu label for creating a workspace -// without a preset, matching the dashboard's "None" option. -const presetNoneOption = "None" - -// promptPresetSelection shows a CLI selection menu of the presets defined in the template version, -// preceded by a "None" option. -// Returns the selected preset, or nil if the user selected "None". -func promptPresetSelection(inv *serpent.Invocation, presets []codersdk.Preset) (*codersdk.Preset, error) { - presetMap := make(map[string]*codersdk.Preset) - presetOptions := []string{presetNoneOption} +const ( + // presetNoneOption is the selection menu label for creating a workspace + // without a preset, matching the dashboard's "None" option. + presetNoneOption = "None" + // presetNoneFallbackOption replaces presetNoneOption when a preset's + // label is also "None", so the menu never shows two identical entries. + presetNoneFallbackOption = "None (no preset)" +) +// presetSelectOptions builds the preset selection menu entries. The first +// entry is the label for applying no preset, which is also returned as +// noneOption. presetMap maps every other entry to its preset. +func presetSelectOptions(presets []codersdk.Preset) (options []string, noneOption string, presetMap map[string]*codersdk.Preset) { + presetMap = make(map[string]*codersdk.Preset, len(presets)) + presetOptions := make([]string, 0, len(presets)) for _, preset := range presets { var option string if preset.Description == "" { @@ -541,6 +545,19 @@ func promptPresetSelection(inv *serpent.Invocation, presets []codersdk.Preset) ( presetMap[option] = &preset } + noneOption = presetNoneOption + if _, ok := presetMap[noneOption]; ok { + noneOption = presetNoneFallbackOption + } + return append([]string{noneOption}, presetOptions...), noneOption, presetMap +} + +// promptPresetSelection shows a CLI selection menu of the presets defined in the template version, +// preceded by an option to apply no preset. +// Returns the selected preset, or nil if the user chose to apply no preset. +func promptPresetSelection(inv *serpent.Invocation, presets []codersdk.Preset) (*codersdk.Preset, error) { + presetOptions, noneOption, presetMap := presetSelectOptions(presets) + // Show selection UI _, _ = fmt.Fprintln(inv.Stdout, pretty.Sprint(cliui.DefaultStyles.Wrap, "Select a preset below:")) selected, err := cliui.Select(inv, cliui.SelectOptions{ @@ -551,7 +568,7 @@ func promptPresetSelection(inv *serpent.Invocation, presets []codersdk.Preset) ( return nil, xerrors.Errorf("failed to select preset: %w", err) } - if selected == presetNoneOption { + if selected == noneOption { return nil, nil //nolint:nilnil // A nil preset means no preset is applied. } return presetMap[selected], nil diff --git a/cli/create_internal_test.go b/cli/create_internal_test.go new file mode 100644 index 00000000000..56ff181ed60 --- /dev/null +++ b/cli/create_internal_test.go @@ -0,0 +1,67 @@ +package cli + +import ( + "testing" + + "github.com/google/uuid" + "github.com/stretchr/testify/require" + + "github.com/coder/coder/v2/codersdk" +) + +func TestPresetSelectOptions(t *testing.T) { + t.Parallel() + + eu := codersdk.Preset{ID: uuid.New(), Name: "eu", Description: "Deploys to EU"} + us := codersdk.Preset{ID: uuid.New(), Name: "us"} + namedNone := codersdk.Preset{ID: uuid.New(), Name: "None"} + namedNoneWithDescription := codersdk.Preset{ID: uuid.New(), Name: "None", Description: "Minimal"} + + tests := []struct { + name string + presets []codersdk.Preset + wantOptions []string + wantNone string + wantPresets map[string]uuid.UUID + }{ + { + name: "NoneFirst", + presets: []codersdk.Preset{eu, us}, + wantOptions: []string{"None", "eu: Deploys to EU", "us"}, + wantNone: "None", + wantPresets: map[string]uuid.UUID{"eu: Deploys to EU": eu.ID, "us": us.ID}, + }, + { + // A preset whose label is "None" must stay selectable, so the + // option for applying no preset gets a distinct label. + name: "PresetNamedNone", + presets: []codersdk.Preset{namedNone, us}, + wantOptions: []string{"None (no preset)", "None", "us"}, + wantNone: "None (no preset)", + wantPresets: map[string]uuid.UUID{"None": namedNone.ID, "us": us.ID}, + }, + { + name: "PresetNamedNoneWithDescription", + presets: []codersdk.Preset{namedNoneWithDescription}, + wantOptions: []string{"None", "None: Minimal"}, + wantNone: "None", + wantPresets: map[string]uuid.UUID{"None: Minimal": namedNoneWithDescription.ID}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + options, noneOption, presetMap := presetSelectOptions(tt.presets) + require.Equal(t, tt.wantOptions, options) + require.Equal(t, tt.wantNone, noneOption) + require.NotContains(t, presetMap, noneOption) + require.Len(t, presetMap, len(tt.wantPresets)) + for option, id := range tt.wantPresets { + require.Contains(t, presetMap, option) + require.Equal(t, id, presetMap[option].ID) + } + }) + } +}