示例#1
0
文件: cli.py 项目: Leopardob/be
def ls(topics):
    """List contents of current context

    \b
    Usage:
        $ be ls
        - spiderman
        - hulk
        $ be ls spiderman
        - peter
        - mjay
        $ be ls spiderman seq01
        - 1000
        - 2000
        - 2500

    Return codes:
        0 Normal
        2 When insufficient arguments are supplied,
            or a template is unsupported.

    """

    if self.isactive():
        lib.echo("ERROR: Exit current project first")
        sys.exit(lib.USER_ERROR)

    # List projects
    if len(topics) == 0:
        for project in lib.list_projects():
            lib.echo("- %s (project)" % project)
        sys.exit(lib.NORMAL)

    # List inventory of project
    elif len(topics) == 1:
        for item, binding in lib.list_inventory(project=topics[0]):
            lib.echo("- %s (%s)" % (item, binding))
        sys.exit(lib.NORMAL)

    # List specific portion of template
    else:
        try:
            for item in lib.list_pattern(topics):
                lib.echo("- %s" % item)
        except IndexError as exc:
            lib.echo(exc)
            sys.exit(lib.USER_ERROR)

    sys.exit(lib.NORMAL)
示例#2
0
文件: cli.py 项目: Leopardob/be
def tab(topics, complete):
    """Utility sub-command for tabcompletion

    This command is meant to be called by a tab completion
    function and is given a the currently entered topics,
    along with a boolean indicating whether or not the
    last entered argument is complete.

    """

    # Discard `be tab`
    topics = list(topics)[2:]

    # When given an incomplete argument,
    # the argument is *sometimes* returned twice (?)
    # .. note:: Seen in Git Bash on Windows
    # $ be in giant [TAB]
    # -> ['giant']
    # $ be in gi[TAB]
    # -> ['gi', 'gi']
    if len(topics) > 1 and topics[-1] == topics[-2]:
        topics.pop()

    # Return projects
    if len(topics) == 0:
        projects = lib.list_projects()
        sys.stdout.write(" ".join(projects))

    # Return inventory
    elif len(topics) == 1:
        project = topics[0]
        projects = lib.list_projects()

        # Check to see if the project name is complete
        if not complete:
            projects = [i for i in projects if i.startswith(project)]
            sys.stdout.write(" ".join(projects))
        else:
            # Return inventory
            inventory = lib.list_inventory(project)
            items = [i for i, b in inventory]
            sys.stdout.write(" ".join(items))

    else:
        project, item = topics[:2]

        # Check to see if the inventory name is complete
        if len(topics) == 2 and not complete:
            inventory = lib.list_inventory(project)
            items = [i for i, b in inventory]
            items = [i for i in items if i.startswith(item)]
            sys.stdout.write(" ".join(items))

        else:
            try:
                item = topics[-1]
                items = lib.list_pattern(topics)
                if not complete:
                    items = lib.list_pattern(topics[:-1])
                    items = [i for i in items if i.startswith(item)]
                    sys.stdout.write(" ".join(items) + " ")
                else:
                    sys.stdout.write(" ".join(items) + " ")

            except IndexError:
                sys.exit(lib.NORMAL)