Add the ability to execute commands directly with the bash subcommand

In addition to provide an interactive shell, this patch adds the
'--command' and '-c' options to allow user to issue a command in the
context of a cluster.

For example:

$ k3d bash -c 'kubectl cluster-info'
pull/66/head
Andy Zhou 5 years ago
parent 5c6f2d7dc5
commit 2971dd6845
  1. 2
      cli/commands.go
  2. 7
      cli/shell.go
  3. 4
      main.go

@ -355,5 +355,5 @@ func GetKubeConfig(c *cli.Context) error {
}
func Bash(c *cli.Context) error {
return bashShell(c.String("name"))
return bashShell(c.String("name"), c.String("command"))
}

@ -6,7 +6,7 @@ import (
"os/exec"
)
func bashShell(cluster string) error {
func bashShell(cluster string, command string) error {
kubeConfigPath, err := getKubeConfig(cluster)
if err != nil {
return err
@ -19,6 +19,11 @@ func bashShell(cluster string) error {
cmd := exec.Command(bashPath, "--noprofile", "--norc")
if len(command) > 0 {
cmd.Args = append(cmd.Args, "-c", command)
}
// Set up stdio
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin

@ -55,6 +55,10 @@ func main() {
Value: defaultK3sClusterName,
Usage: "Set a name for the cluster",
},
cli.StringFlag{
Name: "command, c",
Usage: "Run a shell command in the context of the cluster",
},
},
Action: run.Bash,
},

Loading…
Cancel
Save