File tree Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Expand file tree Collapse file tree 3 files changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -261,6 +261,19 @@ will be printed.
261
261
Current shell profile: demo
262
262
263
263
264
+ .cd
265
+ ~~~
266
+
267
+ You can change the current working directory of the aws-shell by using
268
+ the ``.cd `` command::
269
+
270
+ aws> !pwd
271
+ /usr
272
+ aws> .cd /tmp
273
+ aws> !pwd
274
+ /tmp
275
+
276
+
264
277
Executing Shell Commands
265
278
------------------------
266
279
Original file line number Diff line number Diff line change @@ -45,6 +45,24 @@ class InputInterrupt(Exception):
45
45
pass
46
46
47
47
48
+ class ChangeDirHandler (object ):
49
+ def __init__ (self , output = sys .stdout , err = sys .stderr , chdir = os .chdir ):
50
+ self ._output = output
51
+ self ._err = err
52
+ self ._chdir = chdir
53
+
54
+ def run (self , command , application ):
55
+ # command is a list of parsed commands
56
+ if len (command ) != 2 :
57
+ self ._err .write ("invalid syntax, must be: .cd dirname\n " )
58
+ return
59
+ dirname = os .path .expandvars (os .path .expanduser (command [1 ]))
60
+ try :
61
+ self ._chdir (dirname )
62
+ except OSError as e :
63
+ self ._err .write ("cd: %s\n " % e )
64
+
65
+
48
66
class EditHandler (object ):
49
67
def __init__ (self , popen_cls = None , env = None ):
50
68
if popen_cls is None :
@@ -121,6 +139,7 @@ class DotCommandHandler(object):
121
139
HANDLER_CLASSES = {
122
140
'edit' : EditHandler ,
123
141
'profile' : ProfileHandler ,
142
+ 'cd' : ChangeDirHandler ,
124
143
}
125
144
126
145
def __init__ (self , output = sys .stdout , err = sys .stderr ):
Original file line number Diff line number Diff line change @@ -93,3 +93,26 @@ def test_delegates_to_complete_changing_profile():
93
93
shell .profile = 'mynewprofile'
94
94
assert completer .change_profile .call_args == mock .call ('mynewprofile' )
95
95
assert shell .profile == 'mynewprofile'
96
+
97
+
98
+ def test_cd_handler_can_chdir ():
99
+ chdir = mock .Mock ()
100
+ handler = app .ChangeDirHandler (chdir = chdir )
101
+ handler .run (['.cd' , 'foo/bar' ], None )
102
+ assert chdir .call_args == mock .call ('foo/bar' )
103
+
104
+
105
+ def test_chdir_syntax_error_prints_err_msg (errstream ):
106
+ chdir = mock .Mock ()
107
+ handler = app .ChangeDirHandler (err = errstream , chdir = chdir )
108
+ handler .run (['.cd' ], None )
109
+ assert 'invalid syntax' in errstream .getvalue ()
110
+ assert not chdir .called
111
+
112
+
113
+ def test_error_displayed_when_chdir_fails (errstream ):
114
+ chdir = mock .Mock ()
115
+ chdir .side_effect = OSError ("FAILED" )
116
+ handler = app .ChangeDirHandler (err = errstream , chdir = chdir )
117
+ handler .run (['.cd' , 'foo' ], None )
118
+ assert 'FAILED' in errstream .getvalue ()
You can’t perform that action at this time.
0 commit comments