From 92682005f60f21f709b241a42788f9a876b4b906 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Thu, 5 Oct 2023 23:35:59 -0500 Subject: [PATCH] feat(exec): Add `--command` flag that runs a single command then exits --- cmd/exec/exec.go | 1 + docs/kubedb_exec.md | 3 ++- internal/actions/exec/exec.go | 15 +++------------ 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/cmd/exec/exec.go b/cmd/exec/exec.go index 649fb42e..799ecada 100644 --- a/cmd/exec/exec.go +++ b/cmd/exec/exec.go @@ -20,6 +20,7 @@ func NewCommand() *cobra.Command { PreRunE: preRun, } + cmd.Flags().StringVarP(&action.Command, "command", "c", "", "Run a single command and exit") return cmd } diff --git a/docs/kubedb_exec.md b/docs/kubedb_exec.md index cce83b95..b94f739f 100644 --- a/docs/kubedb_exec.md +++ b/docs/kubedb_exec.md @@ -9,7 +9,8 @@ kubedb exec [flags] ### Options ``` - -h, --help help for exec + -c, --command string Run a single command and exit + -h, --help help for exec ``` ### Options inherited from parent commands diff --git a/internal/actions/exec/exec.go b/internal/actions/exec/exec.go index 778b8b0c..1b23a46c 100644 --- a/internal/actions/exec/exec.go +++ b/internal/actions/exec/exec.go @@ -14,7 +14,6 @@ import ( type Exec struct { config.Exec `mapstructure:",squash"` - Args []string } func (action Exec) Run(ctx context.Context) error { @@ -33,12 +32,11 @@ func (action Exec) Run(ctx context.Context) error { sizeQueue = t.MonitorSize(t.GetSize()) } - podCmd := action.buildCommand(action.Args) return t.Safe(func() error { return action.Client.Exec( ctx, action.Pod, - podCmd.String(), + action.buildCommand().String(), t.In, t.Out, os.Stderr, @@ -49,15 +47,8 @@ func (action Exec) Run(ctx context.Context) error { }) } -func (action Exec) buildCommand(args []string) (cmd *command.Builder) { - if len(args) == 0 { - cmd = action.Dialect.ExecCommand(action.Exec) - } else { - cmd = command.NewBuilder("exec") - for _, arg := range args { - cmd.Push(arg) - } - } +func (action Exec) buildCommand() *command.Builder { + cmd := action.Dialect.ExecCommand(action.Exec) log.WithField("cmd", cmd).Trace("finished building command") return cmd }