From c760f0a8ac7d7507629aa03d222fd4a70c7b8435 Mon Sep 17 00:00:00 2001 From: Yves Brissaud Date: Thu, 10 Oct 2024 01:39:03 +0200 Subject: [PATCH] feat: override options on the CLI Pass --opt key=value to override any option on the command line Signed-off-by: Yves Brissaud --- docs/reference/docker_runx.yaml | 10 ++++++++++ docs/reference/runx.md | 11 ++++++----- internal/commands/root/root.go | 20 +++++++++++++++----- internal/runkit/image.go | 10 ++++------ 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/docs/reference/docker_runx.yaml b/docs/reference/docker_runx.yaml index e5ff0ab..42eb784 100644 --- a/docs/reference/docker_runx.yaml +++ b/docs/reference/docker_runx.yaml @@ -45,6 +45,16 @@ options: experimentalcli: false kubernetes: false swarm: false + - option: opt + value_type: stringArray + default_value: '[]' + description: Set an option value + deprecated: false + hidden: false + experimental: false + experimentalcli: false + kubernetes: false + swarm: false deprecated: false hidden: false experimental: false diff --git a/docs/reference/runx.md b/docs/reference/runx.md index a95c4c6..4608c0a 100644 --- a/docs/reference/runx.md +++ b/docs/reference/runx.md @@ -14,11 +14,12 @@ Docker Run, better ### Options -| Name | Type | Default | Description | -|:---------------|:-------|:--------|:------------------------------------------------------------------| -| `--ask` | `bool` | | Do not read local configuration option values and always ask them | -| `-d`, `--docs` | `bool` | | Print the documentation of the image | -| `-l`, `--list` | `bool` | | List available actions | +| Name | Type | Default | Description | +|:---------------|:--------------|:--------|:------------------------------------------------------------------| +| `--ask` | `bool` | | Do not read local configuration option values and always ask them | +| `-d`, `--docs` | `bool` | | Print the documentation of the image | +| `-l`, `--list` | `bool` | | List available actions | +| `--opt` | `stringArray` | | Set an option value | diff --git a/internal/commands/root/root.go b/internal/commands/root/root.go index 5ff8bbe..4bb18bf 100644 --- a/internal/commands/root/root.go +++ b/internal/commands/root/root.go @@ -28,6 +28,7 @@ var ( docs bool list bool ask bool + opts []string ) func NewCmd(dockerCli command.Cli, isPlugin bool) *cobra.Command { @@ -99,9 +100,9 @@ func NewCmd(dockerCli command.Cli, isPlugin bool) *cobra.Command { if list || action == "" { if tui.IsATTY(dockerCli.In().FD()) { - action := prompt.SelectAction(rk.Config.Actions) - if action != "" { - return run(cmd.Context(), dockerCli.Err(), src, rk, action) + selectedAction := prompt.SelectAction(rk.Config.Actions) + if selectedAction != "" { + return run(cmd.Context(), dockerCli.Err(), src, rk, selectedAction) } } else { _, _ = fmt.Fprintln(dockerCli.Out(), tui.Markdown(mdActions(rk))) @@ -149,6 +150,7 @@ func NewCmd(dockerCli command.Cli, isPlugin bool) *cobra.Command { f.BoolVarP(&docs, "docs", "d", false, "Print the documentation of the image") f.BoolVarP(&list, "list", "l", false, "List available actions") f.BoolVar(&ask, "ask", false, "Do not read local configuration option values and always ask them") + f.StringArrayVar(&opts, "opt", nil, "Set an option value") return cmd } @@ -180,12 +182,20 @@ func run(ctx context.Context, out io.Writer, src string, rk *runkit.RunKit, acti localOpts := getValuesLocal(src, action) - opts, err := prompt.Ask(runnable.Action, localOpts) + for _, opt := range opts { + if key, value, ok := strings.Cut(opt, "="); ok { + localOpts[key] = value + } else { + return fmt.Errorf("invalid option value %s", opt) + } + } + + options, err := prompt.Ask(runnable.Action, localOpts) if err != nil { return err } - if err = runnable.SetOptionValues(opts); err != nil { + if err = runnable.SetOptionValues(options); err != nil { return err } diff --git a/internal/runkit/image.go b/internal/runkit/image.go index 511c712..4b04ed1 100644 --- a/internal/runkit/image.go +++ b/internal/runkit/image.go @@ -10,12 +10,10 @@ import ( ) const ( - RunxAnnotation = "vnd.docker.reference.type" - RunxManifestType = "runx-manifest" - RunxConfigType = "application/vnd.runx.config+yaml" - RunxDocType = "application/vnd.runx.readme+txt" - RunxFileNameAnnotation = "vnd.runx.filename" - RunxLayerFile = "application/vnd.runx.file.gzip" + RunxAnnotation = "vnd.docker.reference.type" + RunxManifestType = "runx-manifest" + RunxConfigType = "application/vnd.runx.config+yaml" + RunxDocType = "application/vnd.runx.readme+txt" ) func Image(runxConfig, runxDoc []byte) (v1.Image, *v1.Descriptor, error) {