Skip to content

Commit

Permalink
feat: support no-prompt and ensure required are set
Browse files Browse the repository at this point in the history
It's now possible to have a "no-prompt" option. Especially if not
required, that means the user can configure with a local
.docker/runx.yaml file for instance without to be prompt if not.

Signed-off-by: Yves Brissaud <[email protected]>
  • Loading branch information
eunomie committed Oct 11, 2024
1 parent 4de4bd7 commit 6b979e7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
25 changes: 13 additions & 12 deletions internal/commands/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,16 @@ func NewCmd(dockerCli command.Cli, isPlugin bool) *cobra.Command {
}

func getValuesLocal(src, action string) map[string]string {
opts := make(map[string]string)
if ask {
return opts
}
localOpts := make(map[string]string)

lc := runkit.GetLocalConfig()
img, ok := lc.Images[src]
if !ok {
return opts
return localOpts
}
act, ok := img.Actions[action]
if !ok {
return opts
return localOpts
}
return act.Opts
}
Expand All @@ -190,13 +187,17 @@ func run(ctx context.Context, out io.Writer, src string, rk *runkit.RunKit, acti
return err
}

localOpts := getValuesLocal(src, action)
localOpts := map[string]string{}

if !ask {
localOpts = getValuesLocal(src, action)

for _, opt := range opts {
if key, value, ok := strings.Cut(opt, "="); ok {
localOpts[key] = value
} else {
return fmt.Errorf("invalid option value %s", opt)
for _, opt := range opts {
if key, value, ok := strings.Cut(opt, "="); ok {
localOpts[key] = value
} else {
return fmt.Errorf("invalid option value %s", opt)
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions internal/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func Ask(action *runkit.Action, opts map[string]string) (map[string]string, erro
if _, ok := opts[opt.Name]; ok {
continue
}
if opt.NoPrompt {
continue
}
opt := opt
if len(opt.Values) == 0 {
fields = append(fields,
Expand Down
6 changes: 6 additions & 0 deletions runkit/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ func (r *Runnable) compute() error {
}

func (r *Runnable) SetOptionValues(opts map[string]string) error {
for _, opt := range r.Action.Options {
if opt.Required && opts[opt.Name] == "" {
return fmt.Errorf("option %q is required", opt.Name)
}
}

r.data.Opts = opts

if err := r.compute(); err != nil {
Expand Down
1 change: 1 addition & 0 deletions runkit/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type (
Opt struct {
Name string `yaml:"name" json:"name"`
Description string `yaml:"desc" json:"desc,omitempty"`
NoPrompt bool `yaml:"no-prompt,omitempty" json:"no_prompt,omitempty"`
Prompt string `yaml:"prompt,omitempty" json:"prompt,omitempty"`
Required bool `yaml:"required,omitempty" json:"required,omitempty"`
Values []string `yaml:"values,omitempty" json:"values,omitempty"`
Expand Down

0 comments on commit 6b979e7

Please sign in to comment.