Skip to content

Commit a42e256

Browse files
committed
Merge branch 'quit-exit-dot'
* quit-exit-dot: Update comment Add exit/quit dot commands Conflicts: awsshell/app.py tests/unit/test_app.py Closes awslabs#97.
2 parents 1c9cd63 + d4764ff commit a42e256

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

awsshell/app.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030

3131
LOG = logging.getLogger(__name__)
32+
EXIT_REQUESTED = object()
3233

3334

3435
def create_aws_shell(completer, model_completer, docs):
@@ -135,11 +136,18 @@ def run(self, command, application):
135136
self._err.write("Usage:\n%s\n" % self.USAGE)
136137

137138

139+
class ExitHandler(object):
140+
def run(self, command, application):
141+
return EXIT_REQUESTED
142+
143+
138144
class DotCommandHandler(object):
139145
HANDLER_CLASSES = {
140146
'edit': EditHandler,
141147
'profile': ProfileHandler,
142148
'cd': ChangeDirHandler,
149+
'exit': ExitHandler,
150+
'quit': ExitHandler,
143151
}
144152

145153
def __init__(self, output=sys.stdout, err=sys.stderr):
@@ -164,7 +172,7 @@ def handle_cmd(self, command, application):
164172
else:
165173
# Note we expect the class to support no-arg
166174
# instantiation.
167-
self.HANDLER_CLASSES[cmd_name]().run(parts, application)
175+
return self.HANDLER_CLASSES[cmd_name]().run(parts, application)
168176

169177
def _unknown_cmd(self, cmd_parts, application):
170178
self._err.write("Unknown dot command: %s\n" % cmd_parts[0])
@@ -272,12 +280,13 @@ def run(self):
272280
self.save_config()
273281
break
274282
else:
275-
if text.strip() in ['quit', 'exit']:
276-
break
277283
if text.startswith('.'):
278-
# These are special commands. The only one supported for
279-
# now is .edit.
280-
self._dot_cmd.handle_cmd(text, application=self)
284+
# These are special commands (dot commands) that are
285+
# interpreted by the aws-shell directly and typically used
286+
# to modify some type of behavior in the aws-shell.
287+
result = self._dot_cmd.handle_cmd(text, application=self)
288+
if result is EXIT_REQUESTED:
289+
break
281290
else:
282291
if text.startswith('!'):
283292
# Then run the rest as a normally shell command.

tests/unit/test_app.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,19 @@ def test_error_displayed_when_chdir_fails(errstream):
116116
handler = app.ChangeDirHandler(err=errstream, chdir=chdir)
117117
handler.run(['.cd', 'foo'], None)
118118
assert 'FAILED' in errstream.getvalue()
119+
120+
121+
def test_exit_dot_command_exits_shell():
122+
mock_prompter = mock.Mock()
123+
# Simulate the user entering '.quit'
124+
fake_document = mock.Mock()
125+
fake_document.text = '.quit'
126+
mock_prompter.run.return_value = fake_document
127+
shell = app.AWSShell(mock.Mock(), mock.Mock(), mock.Mock())
128+
shell.create_cli_interface = mock.Mock(return_value=mock_prompter)
129+
shell.run()
130+
131+
# Should have only called run() once. As soon as we
132+
# see the .quit command, we immediately exit and stop prompting
133+
# for more shell commands.
134+
assert mock_prompter.run.call_count == 1

0 commit comments

Comments
 (0)