Skip to content

Commit 6fefbbe

Browse files
authored
Merge pull request #779 from rancher-sandbox/shell
Add --shell option to limactl shell
2 parents 68229f2 + 5532954 commit 6fefbbe

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

cmd/limactl/shell.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func newShellCommand() *cobra.Command {
3636

3737
shellCmd.Flags().SetInterspersed(false)
3838

39+
shellCmd.Flags().String("shell", "", "shell interpreter, e.g. /bin/bash")
3940
shellCmd.Flags().String("workdir", "", "working directory")
4041
return shellCmd
4142
}
@@ -83,19 +84,19 @@ func shellAction(cmd *cobra.Command, args []string) error {
8384
return err
8485
}
8586
if workDir != "" {
86-
changeDirCmd = fmt.Sprintf("cd %q || exit 1", workDir)
87+
changeDirCmd = fmt.Sprintf("cd %s || exit 1", shellescape.Quote(workDir))
8788
// FIXME: check whether y.Mounts contains the home, not just len > 0
8889
} else if len(y.Mounts) > 0 {
8990
hostCurrentDir, err := os.Getwd()
9091
if err == nil {
91-
changeDirCmd = fmt.Sprintf("cd %q", hostCurrentDir)
92+
changeDirCmd = fmt.Sprintf("cd %s", shellescape.Quote(hostCurrentDir))
9293
} else {
9394
changeDirCmd = "false"
9495
logrus.WithError(err).Warn("failed to get the current directory")
9596
}
9697
hostHomeDir, err := os.UserHomeDir()
9798
if err == nil {
98-
changeDirCmd = fmt.Sprintf("%s || cd %q", changeDirCmd, hostHomeDir)
99+
changeDirCmd = fmt.Sprintf("%s || cd %s", changeDirCmd, shellescape.Quote(hostHomeDir))
99100
} else {
100101
logrus.WithError(err).Warn("failed to get the home directory")
101102
}
@@ -108,7 +109,16 @@ func shellAction(cmd *cobra.Command, args []string) error {
108109
}
109110
logrus.Debugf("changeDirCmd=%q", changeDirCmd)
110111

111-
script := fmt.Sprintf("%s ; exec $SHELL --login", changeDirCmd)
112+
shell, err := cmd.Flags().GetString("shell")
113+
if err != nil {
114+
return err
115+
}
116+
if shell == "" {
117+
shell = `"$SHELL"`
118+
} else {
119+
shell = shellescape.Quote(shell)
120+
}
121+
script := fmt.Sprintf("%s ; exec %s --login", changeDirCmd, shell)
112122
if len(args) > 1 {
113123
script += fmt.Sprintf(
114124
" -c %s",

0 commit comments

Comments
 (0)