Skip to content

Commit

Permalink
Merge pull request aws#2522 from JordonPhillips/turk
Browse files Browse the repository at this point in the history
Alias mturk command
  • Loading branch information
JordonPhillips authored Apr 14, 2017
2 parents ccdb565 + 10af063 commit cac3959
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 14 deletions.
28 changes: 28 additions & 0 deletions awscli/customizations/mturk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from awscli.customizations.utils import make_hidden_command_alias


def register_alias_mturk_command(event_emitter):
event_emitter.register(
'building-command-table.mturk',
alias_mturk_command
)


def alias_mturk_command(command_table, **kwargs):
make_hidden_command_alias(
command_table,
existing_name='list-hits-for-qualification-type',
alias_name='list-hi-ts-for-qualification-type',
)
35 changes: 34 additions & 1 deletion awscli/customizations/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,45 @@ def rename_command(command_table, existing_name, new_name):


def alias_command(command_table, existing_name, new_name):
"""Moves an argument to a new name, keeping the old as a hidden alias."""
"""Moves an argument to a new name, keeping the old as a hidden alias.
:type command_table: dict
:param command_table: The full command table for the CLI or a service.
:type existing_name: str
:param existing_name: The current name of the command.
:type new_name: str
:param new_name: The new name for the command.
"""
current = command_table[existing_name]
_copy_argument(command_table, existing_name, new_name)
current._UNDOCUMENTED = True


def make_hidden_command_alias(command_table, existing_name, alias_name):
"""Create a hidden alias for an exiting command.
This will copy an existing command object in a command table and add a new
entry to the command table with a different name. The new command will
be undocumented.
This is needed if you want to change an existing command, but you still
need the old name to work for backwards compatibility reasons.
:type command_table: dict
:param command_table: The full command table for the CLI or a service.
:type existing_name: str
:param existing_name: The current name of the command.
:type alias_name: str
:param alias_name: The new name for the command.
"""
new = _copy_argument(command_table, existing_name, alias_name)
new._UNDOCUMENTED = True


def validate_mutually_exclusive_handler(*groups):
def _handler(parsed_args, **kwargs):
return validate_mutually_exclusive(parsed_args, *groups)
Expand Down
2 changes: 2 additions & 0 deletions awscli/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
from awscli.customizations.toplevelbool import register_bool_params
from awscli.customizations.waiters import register_add_waiters
from awscli.customizations.opsworkscm import register_alias_opsworks_cm
from awscli.customizations.mturk import register_alias_mturk_command


def awscli_initialize(event_handlers):
Expand Down Expand Up @@ -148,3 +149,4 @@ def awscli_initialize(event_handlers):
register_ec2_page_size_injector(event_handlers)
cloudformation_init(event_handlers)
register_alias_opsworks_cm(event_handlers)
register_alias_mturk_command(event_handlers)
Empty file.
23 changes: 23 additions & 0 deletions tests/functional/mturk/test_alias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from awscli.testutils import BaseAWSCommandParamsTest


class TestAlias(BaseAWSCommandParamsTest):
def test_alias(self):
# This command was aliased, both should work
command_template = 'mturk %s --qualification-type-id foo'
old_command = command_template % 'list-hi-ts-for-qualification-type'
new_command = command_template % 'list-hits-for-qualification-type'
self.run_cmd(old_command, expected_rc=0)
self.run_cmd(new_command, expected_rc=0)
46 changes: 33 additions & 13 deletions tests/unit/customizations/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,43 @@ def test_rename_command_table(self):
class TestCommandTableAlias(BaseAWSHelpOutputTest):

def test_alias_command_table(self):
handler = lambda command_table, **kwargs: utils.alias_command(
command_table, 'ec2', 'foo')
# Verify that we can alias a top level command.
self.session.register('building-command-table.main', handler)
self.driver.main(['foo', 'help'])
self.assert_contains('foo')
old_name = 'ec2'
new_name = 'nopossiblewaythisisalreadythere'

# We can also see subcommands help as well.
self.driver.main(['foo', 'run-instances', 'help'])
self.assert_contains('run-instances')
def handler(command_table, **kwargs):
utils.alias_command(command_table, old_name, new_name)

self._assert_command_exists(old_name, handler)
self._assert_command_exists(new_name, handler)

# Verify that the new name is documented
self.driver.main(['help'])
self.assert_contains(new_name)
self.assert_not_contains(old_name)

# Verify that the old is still available
def test_make_hidden_alias(self):
old_name = 'ec2'
new_name = 'nopossiblewaythisisalreadythere'

def handler(command_table, **kwargs):
utils.make_hidden_command_alias(command_table, old_name, new_name)

self._assert_command_exists(old_name, handler)
self._assert_command_exists(new_name, handler)

# Verify that the new isn't documented
self.driver.main(['help'])
self.assert_not_contains(new_name)
self.assert_contains(old_name)

def _assert_command_exists(self, command_name, handler):
# Verify that we can alias a top level command.
self.session.register('building-command-table.main', handler)
self.driver.main(['ec2', 'help'])
self.assert_contains('ec2')
self.driver.main([command_name, 'help'])
self.assert_contains(command_name)

self.driver.main(['ec2', 'run-instances', 'help'])
# We can also see subcommands help as well.
self.driver.main([command_name, 'run-instances', 'help'])
self.assert_contains('run-instances')


Expand Down

0 comments on commit cac3959

Please sign in to comment.