@@ -55,30 +55,38 @@ func shellAction(clicontext *cli.Context) error {
55
55
return err
56
56
}
57
57
58
- // Buildup changeDirCmd to "cd workDir || cd currentDir || cd homeDir"
58
+ // When workDir is explicitly set, the shell MUST have workDir as the cwd, or exit with an error.
59
+ //
60
+ // changeDirCmd := "cd workDir || exit 1" if workDir != ""
61
+ // := "cd hostCurrentDir || cd hostHomeDir" if workDir == ""
59
62
var changeDirCmd string
60
- workDir := clicontext .String ("workdir" )
61
- if workDir == "" {
62
- changeDirCmd = "false"
63
- } else {
64
- changeDirCmd = fmt .Sprintf ("cd %q" , workDir )
65
- }
66
- if len (y .Mounts ) > 0 {
67
- currentDir , err := os .Getwd ()
63
+ if workDir := clicontext .String ("workdir" ); workDir != "" {
64
+ changeDirCmd = fmt .Sprintf ("cd %q || exit 1" , workDir )
65
+ // FIXME: check whether y.Mounts contains the home, not just len > 0
66
+ } else if len (y .Mounts ) > 0 {
67
+ hostCurrentDir , err := os .Getwd ()
68
68
if err == nil {
69
- changeDirCmd = fmt .Sprintf ("%s || cd %q" , changeDirCmd , currentDir )
69
+ changeDirCmd = fmt .Sprintf ("cd %q" , hostCurrentDir )
70
70
} else {
71
+ changeDirCmd = "false"
71
72
logrus .WithError (err ).Warn ("failed to get the current directory" )
72
73
}
73
- homeDir , err := os .UserHomeDir ()
74
+ hostHomeDir , err := os .UserHomeDir ()
74
75
if err == nil {
75
- changeDirCmd = fmt .Sprintf ("%s || cd %q" , changeDirCmd , homeDir )
76
+ changeDirCmd = fmt .Sprintf ("%s || cd %q" , changeDirCmd , hostHomeDir )
76
77
} else {
77
78
logrus .WithError (err ).Warn ("failed to get the home directory" )
78
79
}
80
+ } else {
81
+ logrus .Debug ("the host home does not seem mounted, so the guest shell will have a different cwd" )
82
+ }
83
+
84
+ if changeDirCmd == "" {
85
+ changeDirCmd = "false"
79
86
}
87
+ logrus .Debugf ("changeDirCmd=%q" , changeDirCmd )
80
88
81
- script := fmt .Sprintf (" %s ; exec bash --login" , changeDirCmd )
89
+ script := fmt .Sprintf ("%s ; exec bash --login" , changeDirCmd )
82
90
if clicontext .NArg () > 1 {
83
91
script += fmt .Sprintf (" -c %q" , shellescape .QuoteCommand (clicontext .Args ().Tail ()))
84
92
}
0 commit comments