def tree(ctx, pages): """Remove page(s) including their descendants.""" with api.context() as cf: for page_ref in pages: root_page = cf.get(page_ref) root_children = cf.get(root_page._expandable.children, expand='page', limit=200) # Get confirmation answer = None while answer not in {'yes', 'no', 'n'}: answer = input('REALLY remove {} children of »{}« and all their descendants? [yes|No|N] ' .format(len(root_children.page.results), root_page.title)) answer = answer.lower() or 'n' # Delete data on positive confirmation if answer != 'yes': click.echo('No confirmation, did not delete anything!') else: counter = 0 try: iter_pages = progress(sorted(root_children.page.results, key=lambda x: x.title.lower())) for page in iter_pages: print(CLEARLINE + "DEL", page.title, end='\r') iter_pages.set_postfix_str('') while True: children = cf.get(page._expandable.children, expand='page', limit=200).page.results if not children: break for child in children: cf.delete_page(child) counter += 1 cf.delete_page(page) counter += 1 finally: print(CLEARLINE + "Deleted {} pages.\n".format(counter))
def banner(title): "Helper" click.echo('') click.secho('~~~ {} ~~~'.format(title), fg='green', bg='black', bold=True)
def login(ctx): """Set or change Vault login credentials in your keyring.""" if not keyring: raise UsageError("'keyring' support is not available, please read" " 'https://config-sesame.readthedocs.io/en/latest/deploy.html'!", ctx=ctx) url, user, token, _ = vault.default_credentials() if not url: raise UsageError("You MUST provide a VAULT_ADDR!", ctx=ctx) if token: click.secho("WARN: You have a VAULT_TOKEN variable in your environment," " which will override any keyring credentials!", fg='yellow', bg='black', bold=True, reverse=True) click.echo("Please enter credentials for storing in {}.{}..." .format(keyring.get_keyring().__class__.__module__, keyring.get_keyring().__class__.__name__)) access = security.Credentials(url) user, token = access.auth_pair(force_console=True) # Prompt for new password keyring.set_password(url, user, token) click.echo("Updated {}'s password (token) for {}".format(user, url))
def usage(ctx, query, top=0): """Create report on usage of different entities (macros, labels, …).""" if not ctx.obj.entity: click.serror("No --entity selected!") return if top: click.echo("TOP {:d}".format(top)) outname = getattr(ctx.obj.outfile, 'name', None) with api.context() as cf: ctx.obj.cql.append('type=page AND macro != "{}"'.format(query)) try: response = cf.get("content/search", cql=' AND '.join(ctx.obj.cql)) except api.ERRORS as cause: # Just log and otherwise ignore any errors api.diagnostics(cause) else: print('Got {} results.'.format(len(response.results))) if response.results: print_result(ctx, response.results[0])
def open_command(ctx, cfgfile=None, bases=None, outfile=''): """Open vault and amend configuration file(s).""" if not cfgfile: raise UsageError("You provided no configuration file names!", ctx=ctx) try: conn = vault.Connection() except ValueError as cause: if "target" in str(cause): click.serror( "{} -- forgot to edit configuration or set VAULT_ADDR?", cause) else: raise data = cfgdata.read_merged_files(cfgfile) secrets = lookup_secrets(data, bases, conn) #ppyaml(cfgdata, sys.stdout) if outfile in ('', '-'): ppyaml(secrets, sys.stdout) else: if not ctx.obj.quiet: click.echo('Writing secrets to "{}"...'.format(outfile)) with io.open(outfile, 'w', encoding='utf-8') as handle: ppyaml(secrets, handle)
def help_command(ctx, config_dump=False): """Print some information on the system environment.""" def banner(title): "Helper" click.echo('') click.secho('~~~ {} ~~~'.format(title), fg='green', bg='black', bold=True) if config_dump: ctx.obj.cfg.dump() sys.exit(0) app_name = ctx.find_root().info_name click.secho('*** "{}" Help & Information ***'.format(app_name), fg='white', bg='blue', bold=True) banner('Version Information') click.echo(config.version_info(ctx)) banner('Configuration') locations = ctx.obj.cfg.locations(exists=False) locations = [(u'✔' if os.path.exists(i) else u'✘', click.pretty_path(i)) for i in locations] click.echo( u'The following configuration files are merged in order, if they exist:\n {0}' .format(u'\n '.join(u'{} {}'.format(*i) for i in locations), )) banner('Confluence Stats') with api.context() as cf: try: user = cf.user() spaces = list(cf.getall('space')) except api.ERRORS as cause: # Just log and otherwise ignore any errors api.diagnostics(cause) else: click.echo( u'Confluence API [{u._info.server} / {u._info.sen}]' u' accessed as {u.displayName} [{u.username}].'.format(u=user)) click.echo(u'{} spaces found.'.format(len(spaces))) click.echo(u'\nMost recently created:') for space in itertools.islice( sorted(spaces, key=lambda i: i.id, reverse=True), 5): click.echo(u' {:10} {:>15} {}'.format( space.type, space.key, space.name)) banner('More Help') click.echo( "Call '{} --help' to get a list of available commands & options.". format(app_name)) click.echo( "Call '{} «command» --help' to get help on a specific command.".format( app_name)) click.echo( "Call '{} --version' to get the above version information separately.". format(app_name)) click.echo( "Call '{} --license' to get licensing informatioon.".format(app_name))
def banner(title): "Helper" click.echo('') click.secho('~~~ {} ~~~'.format(title), fg='green', bg='black', bold=True)
def help_command(ctx, config_dump=False): """Print some information on the system environment.""" def banner(title): "Helper" click.echo('') click.secho('~~~ {} ~~~'.format(title), fg='green', bg='black', bold=True) if config_dump: ctx.obj.cfg.dump() sys.exit(0) app_name = ctx.find_root().info_name click.secho('*** "{}" Help & Information ***'.format(app_name), fg='white', bg='blue', bold=True) banner('Version Information') click.echo(config.version_info(ctx)) banner('Configuration') locations = ctx.obj.cfg.locations(exists=False) locations = [(u'✔' if os.path.exists(i) else u'✘', click.pretty_path(i)) for i in locations] click.echo(u'The following configuration files are merged in order, if they exist:\n {0}'.format( u'\n '.join(u'{} {}'.format(*i) for i in locations), )) banner('More Help') click.echo("Call '{} --help' to get a list of available commands & options.".format(app_name)) click.echo("Call '{} «command» --help' to get help on a specific command.".format(app_name)) click.echo("Call '{} --version' to get the above version information separately.".format(app_name)) click.echo("Call '{} --license' to get licensing informatioon.".format(app_name))
def help_command(ctx, config_dump=False): """Print some information on the system environment.""" def banner(title): "Helper" click.echo('') click.secho('~~~ {} ~~~'.format(title), fg='green', bg='black', bold=True) if config_dump: ctx.obj.cfg.dump() sys.exit(0) app_name = ctx.find_root().info_name click.secho('*** "{}" Help & Information ***'.format(app_name), fg='white', bg='blue', bold=True) banner('Version Information') click.echo(config.version_info(ctx)) banner('Configuration') locations = ctx.obj.cfg.locations(exists=False) locations = [(u'✔' if os.path.exists(i) else u'✘', click.pretty_path(i)) for i in locations] click.echo(u'The following configuration files are merged in order, if they exist:\n {0}'.format( u'\n '.join(u'{} {}'.format(*i) for i in locations), )) banner('Confluence Stats') with api.context() as cf: try: spaces = list(cf.getall('space')) except api.ERRORS as cause: # Just log and otherwise ignore any errors click.serror("API ERROR: {}", cause) else: click.echo(u'{} spaces found.'.format(len(spaces))) click.echo(u'\nMost recently created:') for space in itertools.islice(sorted(spaces, key=lambda i: i.id, reverse=True), 5): click.echo(u' {:10} {:>15} {}'.format(space.type, space.key, space.name)) banner('More Help') click.echo("Call '{} --help' to get a list of available commands & options.".format(app_name)) click.echo("Call '{} «command» --help' to get help on a specific command.".format(app_name)) click.echo("Call '{} --version' to get the above version information separately.".format(app_name)) click.echo("Call '{} --license' to get licensing informatioon.".format(app_name))
def help_command(ctx, config_dump=False): """Print some information on the system environment.""" def banner(title): "Helper" click.echo('') click.secho('~~~ {} ~~~'.format(title), fg='green', bg='black', bold=True) if config_dump: ctx.obj.cfg.dump() sys.exit(0) app_name = ctx.find_root().info_name click.secho('*** "{}" Help & Information ***'.format(app_name), fg='white', bg='blue', bold=True) banner('Version Information') click.echo(config.version_info(ctx)) banner('Configuration') locations = ctx.obj.cfg.locations(exists=False) locations = [(u'✔' if os.path.exists(i) else u'✘', click.pretty_path(i)) for i in locations] click.echo(u'The following configuration files are merged in order, if they exist:\n {0}'.format( u'\n '.join(u'{} {}'.format(*i) for i in locations), )) banner('Vault Information') try: conn = vault.Connection() except ValueError as cause: if "target" in str(cause): click.serror("{} -- forgot to edit configuration or set VAULT_ADDR?", cause) else: raise else: print(conn) policies = conn.api.list_policies() if 'root' in policies: click.secho("WARN: You are connected using a 'root' token!", fg='yellow', bg='black', bold=True, reverse=True) print("Policies: {}".format(', '.join(policies))) print("Auth Backends:") for mount, data in conn.api.list_auth_backends().items(): print(" {mount:15s} {type:15s} {description}".format(mount=mount, **data)) print("Storage:") for mount, data in conn.api.list_secret_backends().items(): print(" {mount:15s} {type:15s} {description}".format(mount=mount, **data)) banner('More Help') click.echo("Call '{} --help' to get a list of available commands & options.".format(app_name)) click.echo("Call '{} «command» --help' to get help on a specific command.".format(app_name)) click.echo("Call '{} --version' to get the above version information separately.".format(app_name)) click.echo("Call '{} --license' to get licensing informatioon.".format(app_name))
def help_command(ctx, config_dump=False): """Print some information on the system environment.""" def banner(title): "Helper" click.echo('') click.secho('~~~ {} ~~~'.format(title), fg='green', bg='black', bold=True) if config_dump: ctx.obj.cfg.dump() sys.exit(0) app_name = ctx.find_root().info_name click.secho('*** "{}" Help & Information ***'.format(app_name), fg='white', bg='blue', bold=True) banner('Version Information') click.echo(config.version_info(ctx)) banner('Configuration') locations = ctx.obj.cfg.locations(exists=False) locations = [(u'✔' if os.path.exists(i) else u'✘', click.pretty_path(i)) for i in locations] click.echo( u'The following configuration files are merged in order, if they exist:\n {0}' .format(u'\n '.join(u'{} {}'.format(*i) for i in locations), )) banner('Active Login') try: api = github.api(config=None) # TODO: config object except AssertionError as cause: click.serror("AUTH: {}", cause) else: try: dump_user(api, api.gh_config.user) limit = api.ratelimit_remaining fgcol = 'yellow' if limit >= 100 else 'cyan' click.secho('\n{} calls remaining in this hour.'.format(limit), fg=fgcol, bg='black', bold=True) except ConnectionError as cause: click.serror("HTTP: {}", cause) except github.GitHubError as cause: click.serror(github.pretty_cause(cause, "API")) banner('More Help') click.echo( "Call '{} --help' to get a list of available commands & options.". format(app_name)) click.echo( "Call '{} «command» --help' to get help on a specific command.".format( app_name)) click.echo( "Call '{} --version' to get the above version information separately.". format(app_name)) click.echo( "Call '{} --license' to get licensing informatioon.".format(app_name))