From eb5b88052e63d9720ada4c9f8e0aae47d97f4c54 Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Wed, 29 May 2019 09:14:36 +0200 Subject: [PATCH 1/5] add auto mode for shell based on SHELL env var and support zsh --- cli/commands.go | 7 ++----- cli/shell.go | 44 +++++++++++++++++++++++++++++++++++++++++--- main.go | 2 +- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/cli/commands.go b/cli/commands.go index 3fe64c26..a3d0173b 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -354,10 +354,7 @@ func GetKubeConfig(c *cli.Context) error { return nil } +// Shell starts a new subshell with the KUBECONFIG pointing to the selected cluster func Shell(c *cli.Context) error { - if c.String("shell") != "bash" { - return fmt.Errorf("%s is not supported. Only bash is supported", c.String("shell")) - } - - return bashShell(c.String("name"), c.String("command")) + return shell(c.String("name"), c.String("shell"), c.String("command")) } diff --git a/cli/shell.go b/cli/shell.go index 7139a11e..19678fbd 100644 --- a/cli/shell.go +++ b/cli/shell.go @@ -4,25 +4,63 @@ import ( "fmt" "os" "os/exec" + "path" ) -func bashShell(cluster string, command string) error { +var shells = map[string]map[string][]string{ + "bash": { + "options": []string{ + "--noprofile", // don't load .profile/.bash_profile + "--norc", // don't load .bashrc + }, + }, + "zsh": { + "options": []string{ + "--no-rcs", // don't load .zshrc + }, + }, +} + +// shell +func shell(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) if err != nil { return err } + // check if we're already in a subshell subShell := os.ExpandEnv("$__K3D_CLUSTER__") if len(subShell) > 0 { 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 { 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 { cmd.Args = append(cmd.Args, "-c", command) diff --git a/main.go b/main.go index 51386f3d..a82a4bcc 100644 --- a/main.go +++ b/main.go @@ -61,7 +61,7 @@ func main() { }, cli.StringFlag{ Name: "shell, s", - Value: "bash", + Value: "auto", Usage: "Sub shell type. Only bash is supported. (default bash)", }, }, From 93115400fc2f112ed5374027f33deec21389e0b2 Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Fri, 31 May 2019 09:43:45 +0200 Subject: [PATCH 2/5] add shell struct with prompt differentiation --- cli/commands.go | 2 +- cli/shell.go | 27 +++++++++++++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/cli/commands.go b/cli/commands.go index a3d0173b..5aa16adb 100644 --- a/cli/commands.go +++ b/cli/commands.go @@ -356,5 +356,5 @@ func GetKubeConfig(c *cli.Context) error { // Shell starts a new subshell with the KUBECONFIG pointing to the selected cluster func Shell(c *cli.Context) error { - return shell(c.String("name"), c.String("shell"), c.String("command")) + return subShell(c.String("name"), c.String("shell"), c.String("command")) } diff --git a/cli/shell.go b/cli/shell.go index 19678fbd..e2df04a0 100644 --- a/cli/shell.go +++ b/cli/shell.go @@ -7,22 +7,33 @@ import ( "path" ) -var shells = map[string]map[string][]string{ +type shell struct { + Name string + Options []string + Prompt string + Env map[string]string +} + +var shells = map[string]shell{ "bash": { - "options": []string{ + Name: "bash", + Options: []string{ "--noprofile", // don't load .profile/.bash_profile "--norc", // don't load .bashrc }, + Prompt: "PS1", }, "zsh": { - "options": []string{ + Name: "zsh", + Options: []string{ "--no-rcs", // don't load .zshrc }, + Prompt: "PROMPT", }, } -// shell -func shell(cluster, shell, command string) error { +// subShell +func subShell(cluster, shell, command string) error { // check if the selected shell is supported if shell == "auto" { @@ -58,7 +69,7 @@ func shell(cluster, shell, command string) error { } // set shell specific options (command line flags) - shellOptions := shells[shell]["options"] + shellOptions := shells[shell].Options cmd := exec.Command(shellPath, shellOptions...) @@ -73,7 +84,7 @@ func shell(cluster, shell, command string) error { cmd.Stderr = os.Stderr // 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("PS1")) // Set up KUBECONFIG setKube := fmt.Sprintf("KUBECONFIG=%s", kubeConfigPath) @@ -81,7 +92,7 @@ func shell(cluster, shell, command string) error { // Declare subshell subShell = fmt.Sprintf("__K3D_CLUSTER__=%s", cluster) - newEnv := append(os.Environ(), setPS1, setKube, subShell) + newEnv := append(os.Environ(), setPrompt, setKube, subShell) cmd.Env = newEnv From f220f0121aa40cda7c5fe6dd196f8251139ee103 Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Fri, 31 May 2019 10:52:48 +0200 Subject: [PATCH 3/5] get prompt from env depdending on selected shell --- cli/shell.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/shell.go b/cli/shell.go index e2df04a0..8b76fc8d 100644 --- a/cli/shell.go +++ b/cli/shell.go @@ -84,7 +84,7 @@ func subShell(cluster, shell, command string) error { cmd.Stderr = os.Stderr // Set up Promot - setPrompt := fmt.Sprintf("%s=[%s} %s", shells[shell].Prompt, cluster, os.Getenv("PS1")) + setPrompt := fmt.Sprintf("%s=[%s} %s", shells[shell].Prompt, cluster, shells[shell].Prompt) // Set up KUBECONFIG setKube := fmt.Sprintf("KUBECONFIG=%s", kubeConfigPath) From 6a46ef9d94aec074808cc36ac6a3d9f761518790 Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Fri, 31 May 2019 12:00:00 +0200 Subject: [PATCH 4/5] fix prompt from env --- cli/shell.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/shell.go b/cli/shell.go index 8b76fc8d..6e20c3b3 100644 --- a/cli/shell.go +++ b/cli/shell.go @@ -84,7 +84,7 @@ func subShell(cluster, shell, command string) error { cmd.Stderr = os.Stderr // Set up Promot - setPrompt := fmt.Sprintf("%s=[%s} %s", shells[shell].Prompt, cluster, shells[shell].Prompt) + setPrompt := fmt.Sprintf("%s=[%s} %s", shells[shell].Prompt, cluster, os.Getenv(shells[shell].Prompt)) // Set up KUBECONFIG setKube := fmt.Sprintf("KUBECONFIG=%s", kubeConfigPath) From f771dcf8b519053e19321009397be7e76e3c94e8 Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Fri, 31 May 2019 12:34:44 +0200 Subject: [PATCH 5/5] shell flag text --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index a82a4bcc..64ebe3d0 100644 --- a/main.go +++ b/main.go @@ -62,7 +62,7 @@ func main() { cli.StringFlag{ Name: "shell, s", Value: "auto", - Usage: "Sub shell type. Only bash is supported. (default bash)", + Usage: "which shell to use. One of [auto, bash, zsh]", }, }, Action: run.Shell,