Пример #1
0
def cli(ctx, pattern, arguments, safe):
    """Executes a saved command."""

    matches = utils.grep_commands(pattern)
    if matches:
        selected = utils.select_command(matches)
        if selected >= 0:
            cmd, desc = matches[selected]
            pcmd = utils.create_pcmd(cmd)
            params = utils.get_params_in_pcmd(pcmd)

            arguments = list(arguments)
            kargs = {}
            for p in params:
                if arguments:
                    val = arguments.pop(0)
                    click.echo("{}: {}".format(p, val))
                    kargs[p] = val
                elif not safe:
                    val = click.prompt("Enter value for '{}'".format(p))
                    kargs[p] = val
            click.echo("\n")

            final_cmd = utils.substitute_pcmd(pcmd, kargs, safe)

            command = "$ {} :: {}".format(final_cmd, desc)
            if click.confirm("Execute\n\t{}\n\n?".format(command),
                             default=True):
                os.system(final_cmd)
    elif matches == []:
        click.echo('No saved commands matches the pattern {}'.format(pattern))
    else:
        click.echo("No commands to run, Add one by 'keep new'. ")
Пример #2
0
def cli(ctx, pattern):
    """Deletes a saved command."""
    matches = utils.grep_commands(pattern)
    if matches:
        click.echo("\n\n")
        for idx, match in enumerate(matches):
            cmd, desc = match
            click.secho(" " + str(idx + 1) + "\t", nl=False, fg='yellow')
            click.secho("$ {} :: {}".format(cmd, desc), fg='green')
        click.echo("\n\n")

        val = 1
        while True and len(matches) > 1:
            val = click.prompt(
                "Choose command to remove [1-{}] (0 to cancel)".format(
                    len(matches)),
                type=int)
            if val in range(len(matches) + 1):
                break
            click.echo("Number is not in range")
        if val > 0:
            cmd, desc = matches[val - 1]
            command = "$ {} :: {}".format(cmd, desc)
            if click.confirm("Remove\n\t{}\n\n?".format(command),
                             default=True):
                commands = utils.read_commands()
                del commands[cmd]
                utils.write_commands(commands)
                click.echo('Command successfully removed!')
    elif matches == []:
        click.echo('No saved commands matches the pattern {}'.format(pattern))
    else:
        click.echo("No commands to remove, Add one by 'keep new'. ")
Пример #3
0
def cli(ctx, pattern):
    """Searches for a saved command."""
    matches = utils.grep_commands(pattern)
    if matches:
        for cmd, desc in matches:
            click.secho("$ {} :: {}".format(cmd, desc), fg='green')
    elif matches == []:
        click.echo('No saved commands matches the pattern {}'.format(pattern))
    else:
        click.echo('No commands to show. Add one by `keep new`.')
Пример #4
0
def cli(ctx, pattern, arguments, safe):
    """Executes a saved command."""

    matches = utils.grep_commands(pattern)
    if matches:
        click.echo("\n\n")
        for idx, match in enumerate(matches):
            cmd, desc = match
            click.secho(" " + str(idx + 1) + "\t", nl=False, fg='yellow')
            click.secho("$ {} :: {}".format(cmd, desc), fg='green')
        click.echo("\n\n")

        selection = 1
        while True and len(matches) > 1:
            selection = click.prompt(
                "Choose command to execute [1-{}] (0 to cancel)".format(
                    len(matches)),
                type=int)
            if selection in range(len(matches) + 1):
                break
            click.echo("Number is not in range")
        if selection > 0:
            cmd, desc = matches[selection - 1]
            pcmd = utils.create_pcmd(cmd)
            params = utils.get_params_in_pcmd(pcmd)

            arguments = list(arguments)
            kargs = {}
            for p in params:
                if arguments:
                    val = arguments.pop(0)
                    click.echo("{}: {}".format(p, val))
                    kargs[p] = val
                elif not safe:
                    val = click.prompt("Enter value for '{}'".format(p))
                    kargs[p] = val
            click.echo("\n")

            final_cmd = utils.substitute_pcmd(pcmd, kargs, safe)

            command = "$ {} :: {}".format(final_cmd, desc)
            if click.confirm("Execute\n\t{}\n\n?".format(command),
                             default=True):
                os.system(final_cmd)
    elif matches == []:
        click.echo('No saved commands matches the pattern {}'.format(pattern))
    else:
        click.echo("No commands to run, Add one by 'keep new'. ")
Пример #5
0
def cli(ctx, pattern):
    """Deletes a saved command."""
    matches = utils.grep_commands(pattern)
    if matches:
        selected = utils.select_command(matches)
        if selected >= 0:
            cmd, desc = matches[selected]
            command = "$ {} :: {}".format(cmd, desc)
            if click.confirm("Remove\n\t{}\n\n?".format(command),
                             default=True):
                utils.remove_command(cmd)
                click.echo('Command successfully removed!')
    elif matches == []:
        click.echo('No saved commands matches the pattern {}'.format(pattern))
    else:
        click.echo("No commands to remove, Add one by 'keep new'. ")
Пример #6
0
def cli(ctx, pattern, arguments, safe, no_confirm):
    """Executes a saved command."""

    if not pattern:
        pattern = "(.*?s)"
    matches = utils.grep_commands(pattern)
    if matches:
        selected = utils.select_command(matches)
        if selected >= 0:
            cmd, desc = matches[selected]
            pcmd = utils.create_pcmd(cmd)
            raw_params, params, defaults = utils.get_params_in_pcmd(pcmd)

            arguments = list(arguments)
            kargs = {}
            for r, p, d in zip(raw_params, params, defaults):
                if arguments:
                    val = arguments.pop(0)
                    click.echo("{}: {}".format(p, val))
                    kargs[r] = val
                elif safe:
                    if d:
                        kargs[r] = d
                else:
                    p_default = d if d else None
                    val = click.prompt("Enter value for '{}'".format(p),
                                       default=p_default)
                    kargs[r] = val
            click.echo("\n")

            final_cmd = utils.substitute_pcmd(pcmd, kargs, safe)

            if no_confirm:
                isconfirmed = True
            else:
                command = "$ {} :: {}".format(final_cmd, desc)
                isconfirmed = click.confirm(
                    "Execute\n\t{}\n\n?".format(command), default=True)

            if isconfirmed:
                os.system(final_cmd)

    elif matches == []:
        click.echo('No saved commands matches the pattern {}'.format(pattern))
    else:
        click.echo("No commands to run, Add one by 'keep new'. ")
Пример #7
0
def cli(ctx, pattern, arguments, safe, no_confirm):
    """Executes a saved command."""

    if not pattern:
        pattern = "(.*?s)"
    matches = utils.grep_commands(pattern)
    if matches:
        selected = utils.select_command(matches)
        if selected >= 0:
            cmd, desc = matches[selected]
            pcmd = utils.create_pcmd(cmd)
            raw_params, params, defaults = utils.get_params_in_pcmd(pcmd)

            arguments = list(arguments)
            kargs = {}
            for r, p, d in zip(raw_params, params, defaults):
                if arguments:
                    val = arguments.pop(0)
                    click.echo(f"{p}: {val}", err=True)
                    kargs[r] = val
                elif safe:
                    if d:
                        kargs[r] = d
                else:
                    p_default = d if d else None
                    val = click.prompt(f"Enter value for '{p}'", default=p_default, err=True)
                    kargs[r] = val

            final_cmd = utils.substitute_pcmd(pcmd, kargs, safe)

            if no_confirm:
                isconfirmed = True
            else:
                command = f"$ {final_cmd} :: {desc}"
                isconfirmed = click.confirm(f"Execute\n\t{command}\n?", default=True, err=True)

            if isconfirmed:
                os.system(final_cmd)

    elif matches == []:
        click.echo(f'No saved commands matches the pattern {pattern}', err=True)
    else:
        click.echo("No commands to run, Add one by 'keep new'. ", err=True)