Skip to content

Commit 64de500

Browse files
committed
Add exit/quit dot commands
Closes awslabs#38.
1 parent 9149483 commit 64de500

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

awsshell/app.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131

3232
LOG = logging.getLogger(__name__)
33+
EXIT_REQUESTED = object()
3334

3435

3536
def create_aws_shell(completer, model_completer, docs):
@@ -82,9 +83,16 @@ def run(self, command, application):
8283
p.communicate()
8384

8485

86+
class ExitHandler(object):
87+
def run(self, command, application):
88+
return EXIT_REQUESTED
89+
90+
8591
class DotCommandHandler(object):
8692
HANDLER_CLASSES = {
8793
'edit': EditHandler,
94+
'exit': ExitHandler,
95+
'quit': ExitHandler,
8896
}
8997

9098
def __init__(self, output=sys.stdout, err=sys.stderr):
@@ -109,7 +117,7 @@ def handle_cmd(self, command, application):
109117
else:
110118
# Note we expect the class to support no-arg
111119
# instantiation.
112-
self.HANDLER_CLASSES[cmd_name]().run(parts, application)
120+
return self.HANDLER_CLASSES[cmd_name]().run(parts, application)
113121

114122
def _unknown_cmd(self, cmd_parts, application):
115123
self._err.write("Unknown dot command: %s\n" % cmd_parts[0])
@@ -204,12 +212,12 @@ def run(self):
204212
self.save_config()
205213
break
206214
else:
207-
if text.strip() in ['quit', 'exit']:
208-
break
209215
if text.startswith('.'):
210216
# These are special commands. The only one supported for
211217
# now is .edit.
212-
self._dot_cmd.handle_cmd(text, application=self)
218+
result = self._dot_cmd.handle_cmd(text, application=self)
219+
if result is EXIT_REQUESTED:
220+
break
213221
else:
214222
if text.startswith('!'):
215223
# 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
@@ -42,3 +42,19 @@ def test_prints_error_message_on_unknown_dot_command(errstream):
4242
handler = app.DotCommandHandler(err=errstream)
4343
handler.handle_cmd(".unknown foo bar", None)
4444
assert errstream.getvalue() == "Unknown dot command: .unknown\n"
45+
46+
47+
def test_exit_dot_command_exits_shell():
48+
mock_prompter = mock.Mock()
49+
# Simulate the user entering '.quit'
50+
fake_document = mock.Mock()
51+
fake_document.text = '.quit'
52+
mock_prompter.run.return_value = fake_document
53+
shell = app.AWSShell(mock.Mock(), mock.Mock(), mock.Mock())
54+
shell.create_cli_interface = mock.Mock(return_value=mock_prompter)
55+
shell.run()
56+
57+
# Should have only called run() once. As soon as we
58+
# see the .quit command, we immediately exit and stop prompting
59+
# for more shell commands.
60+
assert mock_prompter.run.call_count == 1

0 commit comments

Comments
 (0)