|
|
@ -4,25 +4,74 @@ import ( |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"os/exec" |
|
|
|
"os/exec" |
|
|
|
|
|
|
|
"path" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func bashShell(cluster string, command string) error { |
|
|
|
type shell struct { |
|
|
|
|
|
|
|
Name string |
|
|
|
|
|
|
|
Options []string |
|
|
|
|
|
|
|
Prompt string |
|
|
|
|
|
|
|
Env map[string]string |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var shells = map[string]shell{ |
|
|
|
|
|
|
|
"bash": { |
|
|
|
|
|
|
|
Name: "bash", |
|
|
|
|
|
|
|
Options: []string{ |
|
|
|
|
|
|
|
"--noprofile", // don't load .profile/.bash_profile
|
|
|
|
|
|
|
|
"--norc", // don't load .bashrc
|
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
Prompt: "PS1", |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
"zsh": { |
|
|
|
|
|
|
|
Name: "zsh", |
|
|
|
|
|
|
|
Options: []string{ |
|
|
|
|
|
|
|
"--no-rcs", // don't load .zshrc
|
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
Prompt: "PROMPT", |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// subShell
|
|
|
|
|
|
|
|
func subShell(cluster, shell, command string) error { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// check if the selected shell is supported
|
|
|
|
|
|
|
|
if shell == "auto" { |
|
|
|
|
|
|
|
shell = path.Base(os.Getenv("SHELL")) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
supported := false |
|
|
|
|
|
|
|
for supportedShell := range shells { |
|
|
|
|
|
|
|
if supportedShell == shell { |
|
|
|
|
|
|
|
supported = true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if !supported { |
|
|
|
|
|
|
|
return fmt.Errorf("ERROR: selected shell [%s] is not supported", shell) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// get kubeconfig for selected cluster
|
|
|
|
kubeConfigPath, err := getKubeConfig(cluster) |
|
|
|
kubeConfigPath, err := getKubeConfig(cluster) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// check if we're already in a subshell
|
|
|
|
subShell := os.ExpandEnv("$__K3D_CLUSTER__") |
|
|
|
subShell := os.ExpandEnv("$__K3D_CLUSTER__") |
|
|
|
if len(subShell) > 0 { |
|
|
|
if len(subShell) > 0 { |
|
|
|
return fmt.Errorf("Error: Already in subshell of cluster %s", subShell) |
|
|
|
return fmt.Errorf("Error: Already in subshell of cluster %s", subShell) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bashPath, err := exec.LookPath("bash") |
|
|
|
// get path of shell executable
|
|
|
|
|
|
|
|
shellPath, err := exec.LookPath(shell) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
cmd := exec.Command(bashPath, "--noprofile", "--norc") |
|
|
|
// set shell specific options (command line flags)
|
|
|
|
|
|
|
|
shellOptions := shells[shell].Options |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cmd := exec.Command(shellPath, shellOptions...) |
|
|
|
|
|
|
|
|
|
|
|
if len(command) > 0 { |
|
|
|
if len(command) > 0 { |
|
|
|
cmd.Args = append(cmd.Args, "-c", command) |
|
|
|
cmd.Args = append(cmd.Args, "-c", command) |
|
|
@ -35,7 +84,7 @@ func bashShell(cluster string, command string) error { |
|
|
|
cmd.Stderr = os.Stderr |
|
|
|
cmd.Stderr = os.Stderr |
|
|
|
|
|
|
|
|
|
|
|
// Set up Promot
|
|
|
|
// Set up Promot
|
|
|
|
setPS1 := fmt.Sprintf("PS1=[%s}%s", cluster, os.Getenv("PS1")) |
|
|
|
setPrompt := fmt.Sprintf("%s=[%s} %s", shells[shell].Prompt, cluster, os.Getenv(shells[shell].Prompt)) |
|
|
|
|
|
|
|
|
|
|
|
// Set up KUBECONFIG
|
|
|
|
// Set up KUBECONFIG
|
|
|
|
setKube := fmt.Sprintf("KUBECONFIG=%s", kubeConfigPath) |
|
|
|
setKube := fmt.Sprintf("KUBECONFIG=%s", kubeConfigPath) |
|
|
@ -43,7 +92,7 @@ func bashShell(cluster string, command string) error { |
|
|
|
// Declare subshell
|
|
|
|
// Declare subshell
|
|
|
|
subShell = fmt.Sprintf("__K3D_CLUSTER__=%s", cluster) |
|
|
|
subShell = fmt.Sprintf("__K3D_CLUSTER__=%s", cluster) |
|
|
|
|
|
|
|
|
|
|
|
newEnv := append(os.Environ(), setPS1, setKube, subShell) |
|
|
|
newEnv := append(os.Environ(), setPrompt, setKube, subShell) |
|
|
|
|
|
|
|
|
|
|
|
cmd.Env = newEnv |
|
|
|
cmd.Env = newEnv |
|
|
|
|
|
|
|
|
|
|
|