def handle_failure(cmd, warn_only): if hasattr(cmd, '__name__'): cmd = cmd.__name__ + '()' message = 'Error running `%s`\n\n%s' % (cmd, indent(format_exc())) if warn_only: warn(message) else: abort(message)
def main(argv=None): """Handle the bolt command line call.""" if argv is None: argv = sys.argv[1:] op = OptionParser( usage="bolt <command-1> <command-2> ... [options]", ) op.add_option( '-v', '--version', action='store_true', default=False, help="show program's version number and exit" ) op.add_option( '-f', dest='file', default="Boltfile", help="set the name or path of the bolt file [Boltfile]" ) op.add_option( '-d', dest='defaults_file', default=_rc_path(), help="set the path of the defaults file [~/.bolt.yaml]" ) op.add_option( '-i', dest='identity', action='append', default=None, help="path to SSH private key file(s) -- may be repeated" ) op.add_option( '--hide', metavar='LEVELS', help="comma-separated list of output levels to hide" ) op.add_option( '--show', metavar='LEVELS', help="comma-separated list of output levels to show" ) op.add_option( '--disable', metavar='HOOKS', help="comma-separated list of hooks to disable" ) op.add_option( '--enable', metavar='HOOKS', help="comma-separated list of hooks to enable" ) op.add_option( '--list', action='store_true', default=False, help="show the list of available tasks and exit" ) op.add_option( '--no-pty', action='store_true', default=False, help="do not use pseudo-terminal in run/sudo" ) options, args = op.parse_args(argv) setup_defaults(options.defaults_file) # Load the Boltfile. runner = init_task_runner(options.file, getcwd()) # Autocompletion support. autocomplete_items = runner.tasks.keys() if 'autocomplete' in env: autocomplete_items += env.autocomplete autocomplete(op, ListCompleter(autocomplete_items)) if options.version: print("bolt %s" % __version__) sys.exit() if options.no_pty: env.always_use_pty = False if options.identity: env.key_filename = options.identity split_string = lambda s: filter(None, map(str.strip, s.split(','))) # Handle output levels. if options.show: for level in split_string(options.show): output[level] = True if options.hide: for level in split_string(options.hide): output[level] = False if output.debug: print("Using Boltfile: %s" % runner.path) # Handle hooks related options. if options.disable: for hook in split_string(options.disable): DISABLED_HOOKS.append(hook) if options.enable: for hook in split_string(options.enable): ENABLED_HOOKS.append(hook) if options.list: print('\n'.join(sorted(runner.tasks))) sys.exit() tasks = [] idx = 0 # Parse command line arguments. for task in args: # Initialise variables. _args = [] _kwargs = {} _ctx = None # Handle +env flags. if task.startswith('+'): if ':' in task: name, value = task[1:].split(':', 1) env[name] = value else: env[task[1:]] = True continue # Handle @context specifiers. if task.startswith('@'): if not idx: continue ctx = (task[1:],) existing = tasks[idx-1][3] if existing: new = list(existing) new.extend(ctx) ctx = tuple(new) tasks[idx-1][3] = ctx continue # Handle tasks with parameters. if ':' in task: task, argstr = task.split(':', 1) for pair in _escape_split(',', argstr): k, _, v = pair.partition('=') if _: _kwargs[k] = v else: _args.append(k) idx += 1 task_name = task.replace('_', '-') if task_name not in runner.tasks: abort("Task not found:\n\n%s" % indent(task)) tasks.append([task_name, _args, _kwargs, _ctx]) if not tasks: runner.display_listing() sys.exit() runner.run(tasks)