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

aliases and autocomplete script #16

Merged
merged 2 commits into from
May 18, 2017
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
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exclude .travis.yml
include README.rst
include LICENSE
include setup.cfg
include shipami-complete.sh
prune .cache
prune .git
prune build
Expand Down
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
keywords='aws ec2 ami marketplace',

packages=find_packages(exclude=['contrib', 'docs', 'tests*']),
package_data={
'codebuilder': [
'shipami-complete.sh'
]
},
include_package_data=True,

install_requires=[
'click==6.7',
Expand Down
18 changes: 17 additions & 1 deletion shipami/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,23 @@ def validate_filter(ctx, param, filters):
raise click.BadParameter('filter must be in format "key=value"')
return tuple(validated_filters)

@click.group()
class AliasedGroup(click.Group):
ALIASES = {
'ls': 'list',
'cp': 'copy',
'rm': 'delete'
}

def get_command(self, ctx, cmd_name):
rv = click.Group.get_command(self, ctx, cmd_name)
if rv is not None:
return rv

if self.ALIASES.get(cmd_name, None):
return click.Group.get_command(self, ctx, self.ALIASES[cmd_name])
return None

@click.group(cls=AliasedGroup)
@click.version_option(VERSION)
@click.option('--region')
@click.option('-v', '--verbose', is_flag=True, default=False)
Expand Down
8 changes: 8 additions & 0 deletions shipami/shipami-complete.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
_shipami_completion() {
COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \
COMP_CWORD=$COMP_CWORD \
_SHIPAMI_COMPLETE=complete $1 ) )
return 0
}

complete -F _shipami_completion -o default shipami;
48 changes: 48 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ def test_list(self, base_image, released_image):
assert 'origin' not in lines[1]
assert 'eu-west-1:{}'.format(base_image.id) in lines[1]

def test_list_aliased(self, base_image, released_image):
r = runner.invoke(shipami, ['ls'])

lines = r.output.splitlines()

assert r.exit_code == 0
assert base_image.id in lines[2]
assert 'origin' in lines[2]
assert 'eu-west-1:{}'.format(released_image.id) in lines[2]

assert released_image.id in lines[1]
assert 'origin' not in lines[1]
assert 'eu-west-1:{}'.format(base_image.id) in lines[1]

def test_list_quiet(self, base_image, released_image):
r = runner.invoke(shipami, ['list', '-q'])

Expand Down Expand Up @@ -187,6 +201,30 @@ def test_copy(self, ec2, base_image):
assert image.name == base_image.name
assert sorted(image.tags, key=lambda _: _['Key']) == sorted(expected_tags, key=lambda _: _['Key'])

def test_copy_aliased(self, ec2, base_image):
image_number = len(ec2.meta.client.describe_images()['Images'])

expected_tags = [
{
'Key': 'shipami:managed',
'Value': 'True'
},
{
'Key': 'shipami:copied_from',
'Value': 'eu-west-1:{}'.format(base_image.id)
}
]

r = runner.invoke(shipami, ['cp', base_image.id])

returned_image_id = r.output.strip()
image = ec2.Image(returned_image_id)

assert r.exit_code == 0
assert len(ec2.meta.client.describe_images()['Images']) == image_number + 1
assert image.name == base_image.name
assert sorted(image.tags, key=lambda _: _['Key']) == sorted(expected_tags, key=lambda _: _['Key'])

def test_copy_wait(self, ec2, base_image):
image_number = len(ec2.meta.client.describe_images()['Images'])

Expand Down Expand Up @@ -275,6 +313,16 @@ def test_delete(self, ec2, copied_image):
assert len(ec2.meta.client.describe_images()['Images']) == 1
assert returned_image_id == copied_image_id

def test_delete_aliased(self, ec2, copied_image):
copied_image_id = copied_image.id
r = runner.invoke(shipami, ['rm', copied_image_id])

returned_image_id = r.output.strip()

assert r.exit_code == 0
assert len(ec2.meta.client.describe_images()['Images']) == 1
assert returned_image_id == copied_image_id

def test_delete_not_managed(self, ec2, base_image):
base_image_id = base_image.id
r = runner.invoke(shipami, ['delete', base_image_id])
Expand Down