import click
import json as j
import sys

@click.group()
def main():
    pass


def _process(source, functions, action, json):
    from .lambdas import load
    lambdas = load(source, functions)
    for l in lambdas:
        action(l)
    if json:
        d = dict([[l.cfg.name, l.cfg.package] for l in lambdas])
        print(j.dumps(d, separators=(',', ': '), indent=2))
    if functions and not lambdas:
        if not json:
            print('None of the specified lambda definitions were found.')
        sys.exit(1)


# ====== build command ====== #

@main.command('build',
    help='Build the specified lambda functions into packages ready for manual upload to AWS.'
)
@click.option('--source', '-s', default='aws-lambda.yml',
    help='Specifies the source file containing the lambda definitions. Default aws-lambda.yml.'
)
@click.option('--json', '-j', is_flag=True,
    help='Output the built packages in JSON format.'
)
@click.argument('functions', nargs=-1)
def build(source, json, functions):
    _process(source, functions, lambda x: x.build(silent=json), json)


# ====== deploy command ====== #

@main.command('deploy',
    help='Deploy the specified lambda functions to AWS.'
)
@click.option('--source', '-s', default='aws-lambda.yml',
    help='Specifies the source file containing the lambda definitions. Default aws-lambda.yml.'
)
@click.option('--json', '-j', is_flag=True,
    help='Output the built packages in JSON format.'
)
@click.argument('functions', nargs=-1
)
def deploy(source, json, functions):
    _process(source, functions, lambda x: x.deploy(silent=json), json)
@@ -23,16 +23,26 @@
 on as j%0A
+import os%0A
 import s
@@ -523,24 +523,582 @@
 s.exit(1)%0A%0A%0A
+@main.command('list',%0A    help='Lists the lambda functions in the definition file'%0A)%0A@click.option('--source', '-s', default='aws-lambda.yml',%0A    help='Specifies the source file containing the lambda definitions. Default aws-lambda.yml.'%0A)%0A@click.option('--json', '-j', is_flag=True,%0A    help='Output the built packages in JSON format.'%0A)%0A@click.argument('functions', nargs=-1)%0Adef list_cmd(source, json, functions):%0A    _process(%0A        source, functions,%0A        lambda x: sys.stdout.write('' if json else (x.cfg.name + os.linesep)),%0A        json%0A    )%0A%0A
 # ====== bui

Add a list command