Skip to content

Commit 8ee3421

Browse files
committed
feat: allow to set local values
In a runx config manifest we can ask the user to fill any input. But sometimes we want to be able to set it for a specific project. We can now define it inside the local configuration .docker/runx.yaml. The value from the local configuration will be used, and the user will not be prompt for a value. But if we want to temporaily override it, it's still possible using the --ask flag. Signed-off-by: Yves Brissaud <[email protected]>
1 parent 7550b60 commit 8ee3421

File tree

5 files changed

+55
-11
lines changed

5 files changed

+55
-11
lines changed

docs/reference/docker_runx.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ clink:
1313
- docker_runx_help.yaml
1414
- docker_runx_version.yaml
1515
options:
16+
- option: ask
17+
value_type: bool
18+
default_value: "false"
19+
description: Do not read local configuration option values and always ask them
20+
deprecated: false
21+
hidden: false
22+
experimental: false
23+
experimentalcli: false
24+
kubernetes: false
25+
swarm: false
1626
- option: docs
1727
shorthand: d
1828
value_type: bool

docs/reference/runx.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ Docker Run, better
1414

1515
### Options
1616

17-
| Name | Type | Default | Description |
18-
|:---------------|:-------|:--------|:-------------------------------------|
19-
| `-d`, `--docs` | `bool` | | Print the documentation of the image |
20-
| `-l`, `--list` | `bool` | | List available actions |
17+
| Name | Type | Default | Description |
18+
|:---------------|:-------|:--------|:------------------------------------------------------------------|
19+
| `--ask` | `bool` | | Do not read local configuration option values and always ask them |
20+
| `-d`, `--docs` | `bool` | | Print the documentation of the image |
21+
| `-l`, `--list` | `bool` | | List available actions |
2122

2223

2324
<!---MARKER_GEN_END-->

internal/commands/root/root.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
var (
2828
docs bool
2929
list bool
30+
ask bool
3031
)
3132

3233
func NewCmd(dockerCli command.Cli, isPlugin bool) *cobra.Command {
@@ -100,7 +101,7 @@ func NewCmd(dockerCli command.Cli, isPlugin bool) *cobra.Command {
100101
if tui.IsATTY(dockerCli.In().FD()) {
101102
action := prompt.SelectAction(rk.Config.Actions)
102103
if action != "" {
103-
return run(cmd.Context(), dockerCli.Err(), rk, action)
104+
return run(cmd.Context(), dockerCli.Err(), src, rk, action)
104105
}
105106
} else {
106107
_, _ = fmt.Fprintln(dockerCli.Out(), tui.Markdown(mdActions(rk)))
@@ -109,7 +110,7 @@ func NewCmd(dockerCli command.Cli, isPlugin bool) *cobra.Command {
109110
}
110111

111112
if action != "" {
112-
return run(cmd.Context(), dockerCli.Err(), rk, action)
113+
return run(cmd.Context(), dockerCli.Err(), src, rk, action)
113114
}
114115

115116
return cmd.Help()
@@ -147,17 +148,38 @@ func NewCmd(dockerCli command.Cli, isPlugin bool) *cobra.Command {
147148
f := cmd.Flags()
148149
f.BoolVarP(&docs, "docs", "d", false, "Print the documentation of the image")
149150
f.BoolVarP(&list, "list", "l", false, "List available actions")
151+
f.BoolVar(&ask, "ask", false, "Do not read local configuration option values and always ask them")
150152

151153
return cmd
152154
}
153155

154-
func run(ctx context.Context, out io.Writer, rk *runkit.RunKit, action string) error {
156+
func getValuesLocal(src, action string) map[string]string {
157+
opts := make(map[string]string)
158+
if ask {
159+
return opts
160+
}
161+
162+
lc := runkit.GetLocalConfig()
163+
img, ok := lc.Images[src]
164+
if !ok {
165+
return opts
166+
}
167+
act, ok := img.Actions[action]
168+
if !ok {
169+
return opts
170+
}
171+
return act.Opts
172+
}
173+
174+
func run(ctx context.Context, out io.Writer, src string, rk *runkit.RunKit, action string) error {
155175
runnable, err := rk.GetRunnable(action)
156176
if err != nil {
157177
return err
158178
}
159179

160-
opts, err := prompt.Ask(runnable.Action)
180+
localOpts := getValuesLocal(src, action)
181+
182+
opts, err := prompt.Ask(runnable.Action, localOpts)
161183
if err != nil {
162184
return err
163185
}

internal/prompt/prompt.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,21 @@ func envStr(env []string) string {
4747
return " (required env: " + strings.Join(env, ", ") + ")"
4848
}
4949

50-
func Ask(action runkit.Action) (map[string]string, error) {
50+
func Ask(action runkit.Action, opts map[string]string) (map[string]string, error) {
5151
if len(action.Options) == 0 {
5252
return nil, nil
5353
}
5454

5555
var (
5656
err error
57-
opts = map[string]string{}
5857
form *huh.Form
5958
fields []huh.Field
6059
)
6160

6261
for _, opt := range action.Options {
62+
if _, ok := opts[opt.Name]; ok {
63+
continue
64+
}
6365
opt := opt
6466
if len(opt.Values) == 0 {
6567
fields = append(fields,
@@ -79,6 +81,10 @@ func Ask(action runkit.Action) (map[string]string, error) {
7981
}
8082
}
8183

84+
if len(fields) == 0 {
85+
return opts, nil
86+
}
87+
8288
form = huh.NewForm(huh.NewGroup(fields...))
8389
if err = form.Run(); err != nil {
8490
return nil, err

runkit/types.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ type (
3838
}
3939

4040
ConfigImage struct {
41-
Default string `yaml:"default,omitempty" json:"default,omitempty"`
41+
Default string `yaml:"default,omitempty" json:"default,omitempty"`
42+
Actions map[string]ConfigAction `yaml:"actions,omitempty" json:"actions,omitempty"`
43+
}
44+
45+
ConfigAction struct {
46+
Opts map[string]string `yaml:"opts,omitempty" json:"opts,omitempty"`
4247
}
4348
)
4449

0 commit comments

Comments
 (0)