@@ -26,19 +26,6 @@ var shellCommand = &cli.Command{
26
26
& cli.StringFlag {
27
27
Name : "workdir" ,
28
28
Usage : "working directory" ,
29
- Value : func () string {
30
- wd , err := os .Getwd ()
31
- if err != nil {
32
- logrus .WithError (err ).Warn ("failed to get the current directory" )
33
- home , err := os .UserHomeDir ()
34
- if err != nil {
35
- logrus .WithError (err ).Warn ("failed to get the home directory" )
36
- return "/"
37
- }
38
- return home
39
- }
40
- return wd
41
- }(),
42
29
},
43
30
},
44
31
Action : shellAction ,
@@ -60,16 +47,6 @@ func shellAction(clicontext *cli.Context) error {
60
47
strings .Join (clicontext .Args ().Slice ()[2 :], " " ))
61
48
}
62
49
63
- hostHome , err := os .UserHomeDir ()
64
- if err != nil {
65
- return err
66
- }
67
-
68
- script := fmt .Sprintf (" cd %q || cd %q ; exec bash --login" , clicontext .String ("workdir" ), hostHome )
69
- if clicontext .NArg () > 1 {
70
- script += fmt .Sprintf (" -c %q" , shellescape .QuoteCommand (clicontext .Args ().Tail ()))
71
- }
72
-
73
50
y , instDir , err := store .LoadYAMLByInstanceName (instName )
74
51
if err != nil {
75
52
if errors .Is (err , os .ErrNotExist ) {
@@ -78,6 +55,34 @@ func shellAction(clicontext *cli.Context) error {
78
55
return err
79
56
}
80
57
58
+ // Buildup changeDirCmd to "cd workDir || cd currentDir || cd homeDir"
59
+ 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 ()
68
+ if err == nil {
69
+ changeDirCmd = fmt .Sprintf ("%s || cd %q" , changeDirCmd , currentDir )
70
+ } else {
71
+ logrus .WithError (err ).Warn ("failed to get the current directory" )
72
+ }
73
+ homeDir , err := os .UserHomeDir ()
74
+ if err == nil {
75
+ changeDirCmd = fmt .Sprintf ("%s || cd %q" , changeDirCmd , homeDir )
76
+ } else {
77
+ logrus .WithError (err ).Warn ("failed to get the home directory" )
78
+ }
79
+ }
80
+
81
+ script := fmt .Sprintf (" %s ; exec bash --login" , changeDirCmd )
82
+ if clicontext .NArg () > 1 {
83
+ script += fmt .Sprintf (" -c %q" , shellescape .QuoteCommand (clicontext .Args ().Tail ()))
84
+ }
85
+
81
86
arg0 , err := exec .LookPath ("ssh" )
82
87
if err != nil {
83
88
return err
0 commit comments