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'
43 lines
728 B
Go
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()
|
|
}
|