Skip to content

Commit

Permalink
feat: allow to define default value for the prompts
Browse files Browse the repository at this point in the history
Also display the description if it exists

Signed-off-by: Yves Brissaud <[email protected]>
  • Loading branch information
eunomie committed Oct 14, 2024
1 parent f0e1a71 commit 568daf5
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ actions:
required: true|false # If required, an empty value will not be accepted.
values: # A list of possible values for the option. If set, a select will be displayed to the user.
- VALUE
default: VALUE # The default value for the option.
# The command to run. It's defined as a Go template and can use the following variables:
# - `.Ref` will be replaced by the reference to the image the user provided.
# - `.IsTTY` indicates if the command is run in a TTY environment.
Expand Down
1 change: 1 addition & 0 deletions docs/index.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ actions:
required: true|false # If required, an empty value will not be accepted.
values: # A list of possible values for the option. If set, a select will be displayed to the user.
- VALUE
default: VALUE # The default value for the option.
# The command to run. It's defined as a Go template and can use the following variables:
# - `.Ref` will be replaced by the reference to the image the user provided.
# - `.IsTTY` indicates if the command is run in a TTY environment.
Expand Down
6 changes: 5 additions & 1 deletion internal/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,19 @@ func Ask(action *runkit.Action, opts map[string]string) (map[string]string, erro
huh.NewInput().
Title(cmp.Or(opt.Prompt, cmp.Or(opt.Description, opt.Name))).
Key(opt.Name).
Description(opt.Description).
Placeholder(opt.Default).
Suggestions(sugar.If(opt.Default != "", []string{opt.Default}, nil)).
Validate(checkRequired(opt.Required)))
} else {
fields = append(fields,
huh.NewSelect[string]().
Title(cmp.Or(opt.Prompt, cmp.Or(opt.Description, opt.Name))).
Key(opt.Name).
Description(opt.Description).
Validate(checkRequired(opt.Required)).
Options(pizza.Map(opt.Values, func(str string) huh.Option[string] {
return huh.NewOption(str, str)
return huh.NewOption(str, str).Selected(str == opt.Default)
})...))
}
asked = append(asked, opt.Name)
Expand Down
1 change: 1 addition & 0 deletions runkit/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type (
Prompt string `yaml:"prompt,omitempty" json:"prompt,omitempty"`
Required bool `yaml:"required,omitempty" json:"required,omitempty"`
Values []string `yaml:"values,omitempty" json:"values,omitempty"`
Default string `yaml:"default,omitempty" json:"default,omitempty"`
}

ActionType string
Expand Down

0 comments on commit 568daf5

Please sign in to comment.