Skip to content

Commit

Permalink
feat(exec): Add --command flag that runs a single command then exits
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Oct 6, 2023
1 parent 466239c commit 9268200
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 13 deletions.
1 change: 1 addition & 0 deletions cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion docs/kubedb_exec.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 3 additions & 12 deletions internal/actions/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

type Exec struct {
config.Exec `mapstructure:",squash"`
Args []string
}

func (action Exec) Run(ctx context.Context) error {
Expand All @@ -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,
Expand All @@ -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
}

0 comments on commit 9268200

Please sign in to comment.