From 568daf5c9a3f6bab8a62af3af9176e8e365d4efb Mon Sep 17 00:00:00 2001 From: Yves Brissaud Date: Mon, 14 Oct 2024 21:51:25 +0200 Subject: [PATCH] feat: allow to define default value for the prompts Also display the description if it exists Signed-off-by: Yves Brissaud --- README.md | 1 + docs/index.markdown | 1 + internal/prompt/prompt.go | 6 +++++- runkit/types.go | 1 + 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 735bf42..80056b1 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/docs/index.markdown b/docs/index.markdown index 1cd1013..7eddcc5 100644 --- a/docs/index.markdown +++ b/docs/index.markdown @@ -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. diff --git a/internal/prompt/prompt.go b/internal/prompt/prompt.go index 15bdd15..9664702 100644 --- a/internal/prompt/prompt.go +++ b/internal/prompt/prompt.go @@ -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) diff --git a/runkit/types.go b/runkit/types.go index cba5138..f47b08a 100644 --- a/runkit/types.go +++ b/runkit/types.go @@ -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