Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] auto mode and zsh support for shell command #70

Merged
merged 5 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 subShell(c.String("name"), c.String("shell"), c.String("command"))
}
59 changes: 54 additions & 5 deletions cli/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,74 @@ import (
"fmt"
"os"
"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)
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)
Expand All @@ -35,15 +84,15 @@ func bashShell(cluster string, 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(shells[shell].Prompt))

// Set up KUBECONFIG
setKube := fmt.Sprintf("KUBECONFIG=%s", kubeConfigPath)

// 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

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
iwilltry42 marked this conversation as resolved.
Show resolved Hide resolved
},
},
Expand Down