Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exit/quit dot commands #97

Merged
merged 1 commit into from
Dec 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions awsshell/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@


LOG = logging.getLogger(__name__)
EXIT_REQUESTED = object()


def create_aws_shell(completer, model_completer, docs):
Expand Down Expand Up @@ -82,9 +83,16 @@ def run(self, command, application):
p.communicate()


class ExitHandler(object):
def run(self, command, application):
return EXIT_REQUESTED


class DotCommandHandler(object):
HANDLER_CLASSES = {
'edit': EditHandler,
'exit': ExitHandler,
'quit': ExitHandler,
}

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

def _unknown_cmd(self, cmd_parts, application):
self._err.write("Unknown dot command: %s\n" % cmd_parts[0])
Expand Down Expand Up @@ -204,12 +212,12 @@ def run(self):
self.save_config()
break
else:
if text.strip() in ['quit', 'exit']:
break
if text.startswith('.'):
# These are special commands. The only one supported for
# now is .edit.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this comment be updated?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I'll update this comment.

self._dot_cmd.handle_cmd(text, application=self)
result = self._dot_cmd.handle_cmd(text, application=self)
if result is EXIT_REQUESTED:
break
else:
if text.startswith('!'):
# Then run the rest as a normally shell command.
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,19 @@ def test_prints_error_message_on_unknown_dot_command(errstream):
handler = app.DotCommandHandler(err=errstream)
handler.handle_cmd(".unknown foo bar", None)
assert errstream.getvalue() == "Unknown dot command: .unknown\n"


def test_exit_dot_command_exits_shell():
mock_prompter = mock.Mock()
# Simulate the user entering '.quit'
fake_document = mock.Mock()
fake_document.text = '.quit'
mock_prompter.run.return_value = fake_document
shell = app.AWSShell(mock.Mock(), mock.Mock(), mock.Mock())
shell.create_cli_interface = mock.Mock(return_value=mock_prompter)
shell.run()

# Should have only called run() once. As soon as we
# see the .quit command, we immediately exit and stop prompting
# for more shell commands.
assert mock_prompter.run.call_count == 1