def provider_group_factory(): """Dynamically generate provider groups for all providers, and add all basic command to it""" for provider in all_providers(): p = get_notifier(provider) provider_name = p.name help = f"Options for '{provider_name}'" group = click.Group(name=provider_name, help=help) # Notify command notify = partial(_notify, p=p) group.add_command( schema_to_command(p, "notify", notify, add_message=True)) # Resources command resources_callback = partial(_resources, p=p) resources_cmd = click.Command( "resources", callback=resources_callback, help="Show provider resources list", ) group.add_command(resources_cmd) pretty_opt = click.Option(["--pretty/--not-pretty"], help="Output a pretty version of the JSON") # Add any provider resources for resource in p.resources: rsc = getattr(p, resource) rsrc_callback = partial(_resource, rsc) rsrc_command = schema_to_command(rsc, resource, rsrc_callback, add_message=False) rsrc_command.params.append(pretty_opt) group.add_command(rsrc_command) for name, description in CORE_COMMANDS.items(): callback = func_factory(p, name) params = [pretty_opt] command = click.Command( name, callback=callback, help=description.format(provider_name), params=params, ) group.add_command(command) notifiers_cli.add_command(group)
def providers(): """Shows all available providers""" click.echo(", ".join(all_providers()))
def notifiers(): """Notifiers CLI operation""" @notifiers.command() def providers(): """Shows all available providers""" click.echo(', '.join(all_providers())) @notifiers.command(context_settings=dict( ignore_unknown_options=True, allow_extra_args=True, )) @click.argument('provider', type=click.Choice(all_providers()), envvar='NOTIFIERS_DEFAULT_PROVIDER') @click.pass_context def notify(ctx, provider): """Send a notification to a passed provider. Data should be passed via a key=value input like so: notifiers notify pushover token=foo user=bar message=test """ p = get_notifier(provider) data = {} for item in ctx.args: data.update([item.split('=')]) if 'message' not in data:
@click.group() def cli(): """Notifiers CLI operation""" @cli.command() def providers(): """Shows all available providers""" click.echo(', '.join(all_providers())) @cli.command(context_settings=dict( ignore_unknown_options=True, allow_extra_args=True, )) @click.argument('provider', type=click.Choice(all_providers()), envvar='NOTIFIERS_DEFAULT_PROVIDER') @click.pass_context def notify(ctx, provider): """Send a notification to a passed provider. Data should be passed via a key=value input like so: notifiers notify pushover token=foo user=bar message=test """ p = get_notifier(provider) data = {} for item in ctx.args: data.update([item.split('=')]) if 'message' not in data: message = click.get_text_stream('stdin').read()