Skip to content

Commit 6b979e7

Browse files
committed
feat: support no-prompt and ensure required are set
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]>
1 parent 4de4bd7 commit 6b979e7

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

internal/commands/root/root.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,16 @@ func NewCmd(dockerCli command.Cli, isPlugin bool) *cobra.Command {
166166
}
167167

168168
func getValuesLocal(src, action string) map[string]string {
169-
opts := make(map[string]string)
170-
if ask {
171-
return opts
172-
}
169+
localOpts := make(map[string]string)
173170

174171
lc := runkit.GetLocalConfig()
175172
img, ok := lc.Images[src]
176173
if !ok {
177-
return opts
174+
return localOpts
178175
}
179176
act, ok := img.Actions[action]
180177
if !ok {
181-
return opts
178+
return localOpts
182179
}
183180
return act.Opts
184181
}
@@ -190,13 +187,17 @@ func run(ctx context.Context, out io.Writer, src string, rk *runkit.RunKit, acti
190187
return err
191188
}
192189

193-
localOpts := getValuesLocal(src, action)
190+
localOpts := map[string]string{}
191+
192+
if !ask {
193+
localOpts = getValuesLocal(src, action)
194194

195-
for _, opt := range opts {
196-
if key, value, ok := strings.Cut(opt, "="); ok {
197-
localOpts[key] = value
198-
} else {
199-
return fmt.Errorf("invalid option value %s", opt)
195+
for _, opt := range opts {
196+
if key, value, ok := strings.Cut(opt, "="); ok {
197+
localOpts[key] = value
198+
} else {
199+
return fmt.Errorf("invalid option value %s", opt)
200+
}
200201
}
201202
}
202203

internal/prompt/prompt.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ func Ask(action *runkit.Action, opts map[string]string) (map[string]string, erro
6363
if _, ok := opts[opt.Name]; ok {
6464
continue
6565
}
66+
if opt.NoPrompt {
67+
continue
68+
}
6669
opt := opt
6770
if len(opt.Values) == 0 {
6871
fields = append(fields,

runkit/run.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ func (r *Runnable) compute() error {
132132
}
133133

134134
func (r *Runnable) SetOptionValues(opts map[string]string) error {
135+
for _, opt := range r.Action.Options {
136+
if opt.Required && opts[opt.Name] == "" {
137+
return fmt.Errorf("option %q is required", opt.Name)
138+
}
139+
}
140+
135141
r.data.Opts = opts
136142

137143
if err := r.compute(); err != nil {

runkit/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type (
3030
Opt struct {
3131
Name string `yaml:"name" json:"name"`
3232
Description string `yaml:"desc" json:"desc,omitempty"`
33+
NoPrompt bool `yaml:"no-prompt,omitempty" json:"no_prompt,omitempty"`
3334
Prompt string `yaml:"prompt,omitempty" json:"prompt,omitempty"`
3435
Required bool `yaml:"required,omitempty" json:"required,omitempty"`
3536
Values []string `yaml:"values,omitempty" json:"values,omitempty"`

0 commit comments

Comments
 (0)