k3d/cli/shell.go
Andy Zhou 2971dd6845 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'
2019-05-24 17:16:12 -07:00

43 lines
728 B
Go

package run
import (
"fmt"
"os"
"os/exec"
)
func bashShell(cluster string, command string) error {
kubeConfigPath, err := getKubeConfig(cluster)
if err != nil {
return err
}
bashPath, err := exec.LookPath("bash")
if err != nil {
return err
}
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
cmd.Stderr = os.Stderr
// Set up Promot
setPS1 := fmt.Sprintf("PS1=[%s}%s", cluster, os.Getenv("PS1"))
// Set up KUBECONFIG
setKube := fmt.Sprintf("KUBECONFIG=%s", kubeConfigPath)
newEnv := append(os.Environ(), setPS1, setKube)
cmd.Env = newEnv
return cmd.Run()
}