Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdbe0685

Browse files
committed
chore: default to preset if available and none specified
1 parentac12e0a commitdbe0685

File tree

3 files changed

+203
-118
lines changed

3 files changed

+203
-118
lines changed

‎cli/create.go‎

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ import (
2121
"github.com/coder/serpent"
2222
)
2323

24-
// PresetNone represents the special preset value "none".
25-
// It is used when a user runs `create --presetnone`,
24+
// PresetNone represents the special preset value "None".
25+
// It is used when a user runs `create --presetNone`,
2626
// indicating that the CLI should not apply any preset.
27-
constPresetNone="none"
27+
constPresetNone="None"
2828

2929
func (r*RootCmd)create()*serpent.Command {
3030
var (
@@ -278,18 +278,25 @@ func (r *RootCmd) create() *serpent.Command {
278278
varpreset*codersdk.Preset
279279
varpresetParameters []codersdk.WorkspaceBuildParameter
280280

281-
// If the template has no presets, or the user explicitly used --presetnone,
282-
// skip applying a preset.
281+
// If the template has no presets, or the user explicitly used --presetNone,
282+
// skip applying a preset
283283
iflen(tvPresets)>0&&presetName!=PresetNone {
284-
//Resolve which preset to use
285-
preset,err=resolvePreset(inv,tvPresets,presetName)
284+
//Attempt to resolve which preset to use
285+
preset,err=resolvePreset(tvPresets,presetName)
286286
iferr!=nil {
287287
returnxerrors.Errorf("unable to resolve preset: %w",err)
288288
}
289289

290-
// Convert preset parameters into workspace build parameters.
290+
// If no preset found, prompt the user to choose a preset
291+
ifpreset==nil {
292+
ifpreset,err=promptPresetSelection(inv,tvPresets);err!=nil {
293+
returnxerrors.Errorf("unable to prompt user for preset: %w",err)
294+
}
295+
}
296+
297+
// Convert preset parameters into workspace build parameters
291298
presetParameters=presetParameterAsWorkspaceBuildParameters(preset.Parameters)
292-
// Inform the user which preset was applied and its parameters.
299+
// Inform the user which preset was applied and its parameters
293300
displayAppliedPreset(inv,preset,presetParameters)
294301
}else {
295302
// Inform the user that no preset was applied
@@ -377,7 +384,7 @@ func (r *RootCmd) create() *serpent.Command {
377384
serpent.Option{
378385
Flag:"preset",
379386
Env:"CODER_PRESET_NAME",
380-
Description:"Specify the name of a template version preset. Use 'none' to explicitly indicate that no preset should be used.",
387+
Description:"Specify the name of a template version preset. Use 'None' to explicitly indicate that no preset should be used.",
381388
Value:serpent.StringOf(&presetName),
382389
},
383390
serpent.Option{
@@ -431,52 +438,47 @@ type prepWorkspaceBuildArgs struct {
431438
RichParameterDefaults []codersdk.WorkspaceBuildParameter
432439
}
433440

434-
// resolvePreset determines which preset to use based on the --preset flag,
435-
// or prompts the user to select one if the flag is not provided.
436-
funcresolvePreset(inv*serpent.Invocation,presets []codersdk.Preset,presetNamestring) (*codersdk.Preset,error) {
441+
// resolvePreset returns the preset matching the given presetName (if specified),
442+
// or the default preset (if any).
443+
// Returns nil if no matching or default preset is found.
444+
funcresolvePreset(presets []codersdk.Preset,presetNamestring) (*codersdk.Preset,error) {
437445
// If preset name is specified, find it
438446
ifpresetName!="" {
439-
for_,preset:=rangepresets {
440-
ifpreset.Name==presetName {
441-
return&preset,nil
447+
for_,p:=rangepresets {
448+
ifp.Name==presetName {
449+
return&p,nil
442450
}
443451
}
444452
returnnil,xerrors.Errorf("preset %q not found",presetName)
445453
}
446454

447-
// No preset specified, prompt user to select one
448-
returnpromptPresetSelection(inv,presets)
455+
// No preset name specified, search for the default preset
456+
for_,p:=rangepresets {
457+
ifp.Default {
458+
return&p,nil
459+
}
460+
}
461+
462+
// No preset found, return nil to indicate no preset found
463+
returnnil,nil
449464
}
450465

451466
// promptPresetSelection shows a CLI selection menu of the presets defined in the template version.
467+
// Returns the selected preset
452468
funcpromptPresetSelection(inv*serpent.Invocation,presets []codersdk.Preset) (*codersdk.Preset,error) {
453469
presetMap:=make(map[string]*codersdk.Preset)
454-
vardefaultOptionstring
455-
varoptions []string
470+
varpresetOptions []string
456471

457-
// Process presets, with the default option (if any) listed first.
458472
for_,preset:=rangepresets {
459473
option:=preset.Name
460-
ifpreset.Default {
461-
option="(default) "+preset.Name
462-
defaultOption=option
463-
}
474+
presetOptions=append(presetOptions,option)
464475
presetMap[option]=&preset
465476
}
466477

467-
ifdefaultOption!="" {
468-
options=append(options,defaultOption)
469-
}
470-
foroption:=rangepresetMap {
471-
ifoption!=defaultOption {
472-
options=append(options,option)
473-
}
474-
}
475-
476478
// Show selection UI
477479
_,_=fmt.Fprintln(inv.Stdout,pretty.Sprint(cliui.DefaultStyles.Wrap,"Select a preset below:"))
478480
selected,err:=cliui.Select(inv, cliui.SelectOptions{
479-
Options:options,
481+
Options:presetOptions,
480482
HideSearch:true,
481483
})
482484
iferr!=nil {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp