Skip to content

Commit

Permalink
broken plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardBronosky committed Apr 6, 2015
1 parent 285b0fe commit 194c7e0
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 0 deletions.
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# aws-cli-plugins
Examples of how to add custom commands to the [aws-cli].

--------

### Warning

This is a work in progress that doesn't currently work.

### Usage

Install the plugin as a python package.

pip install -U git+https://github.com/RichardBronosky/aws-cli-plugins.git

Add the plugin to your `~/.aws/config` file.

```
# ~/.aws/config
[default]
region = us-east-1
output = json
[plugins]
helloworld = awshelloworld
```

Call the helloworld command.

aws helloworld say-hello

### Expected Behavior

```
$ aws helloworld say-hello
['NOTIFY',
'File "/usr/local/lib/python2.7/site-packages/awshelloworld.py", line 53, in awscli_initialize']
['NOTIFY',
'File "/usr/local/lib/python2.7/site-packages/awshelloworld.py", line 66, in inject_commands']
['NOTIFY',
'File "/usr/local/lib/python2.7/site-packages/awshelloworld.py", line 84, in __init__']
['NOTIFY',
'File "/usr/local/lib/python2.7/site-packages/awshelloworld.py", line 89, in _run_main']
['NOTIFY',
'File "/usr/local/lib/python2.7/site-packages/awshelloworld.py", line 109, in _call']
['NOTIFY',
'options',
[...list of options...]]
['NOTIFY',
'parsed_globals',
[...list of parsed_globals...]]
```

### Actual Behavior
$ aws helloworld say-hello
```
['NOTIFY',
'File "/usr/local/lib/python2.7/site-packages/awshelloworld.py", line 53, in awscli_initialize']
usage: aws [options] <command> <subcommand> [parameters]
aws: error: argument command: Invalid choice, valid choices are:
autoscaling | cloudformation
cloudfront | cloudhsm
cloudsearch | cloudsearchdomain
cloudtrail | cloudwatch
cognito-identity | cognito-sync
datapipeline | directconnect
dynamodb | ec2
ecs | elasticache
elasticbeanstalk | elastictranscoder
elb | emr
glacier | iam
importexport | kinesis
kms | lambda
logs | opsworks
rds | redshift
route53 | route53domains
sdb | ses
sns | sqs
ssm | storagegateway
sts | support
swf | s3api
s3 | configure
deploy | configservice
help
```

[aws-cli]: https://github.com/aws/aws-cli/
111 changes: 111 additions & 0 deletions awshelloworld.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"""
Copyright 2014 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.
"""


import logging

from awscli.customizations.commands import BasicCommand

LOG = logging.getLogger(__name__)

# Debugging helpers
from pprint import pprint
import inspect


def notify(*args):
"""Show debug info.
Always use a pretty printed list to make it stand out more.
"""
out = ['NOTIFY']
if len(args):
out.extend(args)
else:
frame = inspect.stack()[1][0]
info = inspect.getframeinfo(frame)
out.extend(['File "{filename}", line {lineno}, in {function}'.format(
**info.__dict__)])
pprint(out, width=1)


class HelloWorldError(Exception):

"""Our standard Exception."""

pass


def awscli_initialize(cli):
"""The entry point for HelloWorld high level commands."""
notify() # fires
# drop to an IPython shell for debugging
# from pprint import pprint; import IPython; IPython.embed()

# cli.register('building-command-table.helloworld', HelloWorld.add_command)
cli.register('building-command-table.helloworld', inject_commands)


def inject_commands(command_table, session, **kwargs):
"""Basically the same as BasicCommand.add_command.
https://github.com/aws/aws-cli/blob/master/awscli/customizations/commands.py#L282-L283
"""
notify() # does NOT fire
command_table['say-hello'] = HelloWorld(session)


class HelloWorld(BasicCommand):

"""Greet the user."""

NAME = 'say-hello'
DESCRIPTION = ('Says hello')
SYNOPSIS = ('aws helloworld say-hello [--name Name]\n')

ARG_TABLE = [
{'name': 'name', 'help_text': 'Helloworld name'},
]

def __init__(self, session):
"""Just trying to see what get's called."""
notify() # does NOT fire
super(HelloWorld, self).__init__(session)

def _run_main(self, args, parsed_globals):
"""Run the command and report success."""
notify() # does NOT fire
self.setup_services(args, parsed_globals) # grasping at straws
self._call(args, parsed_globals)

return 0

def setup_services(self, args, parsed_globals):
"""Create self.helloworld on the off chance that it matters.
https://github.com/aws/aws-cli/blob/develop/awscli/customizations/cloudtrail.py#L115
"""
client_args = {
'region_name': None,
'verify': None
}
self.helloworld = self._session.create_client('helloworld',
**client_args)

def _call(self, options, parsed_globals):
"""Run the command."""
notify() # does NOT fire
notify('options', options)
notify('parsed_globals', parsed_globals)
10 changes: 10 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from setuptools import setup

setup(
name='awsplugins',
version='0.1',
py_modules=['awshelloworld'],
install_requires=[
'awscli',
],
)

0 comments on commit 194c7e0

Please sign in to comment.