#!/usr/bin/env python
from imlazy import import_or_install
from imlazy.docker import stop_container


click = import_or_install('click')
docker = import_or_install('docker')
questionary = import_or_install('questionary')


@click.command()
@click.argument('filter', required=False)
def dockerstop(filter=None):
    """
    dockerstop FILTER

    Gets a list of running docker containers and presents them
    as a terminal GUI-choice to the user. Optionally, you can
    choose a string to filter against the list of container names.
    """
    client = docker.from_env()
    if filter:
        containers = [c.name for c in client.containers.list() if filter in c.name]
    else:
        containers = [c.name for c in client.containers.list()]

    if containers:
        if len(containers) == 1:
            which = containers[0]
        else:
            which = questionary.select("Stop which container?", choices=containers).ask()

        if which:
            stop_container(which)
        else:
            print("Aborting.")
    else:
        print("No running containers were found.")
        if filter:
            if questionary.confirm("Do you want to try again without a filter?").ask():
                containers = [c.name for c in client.containers.list()]
                which = questionary.select("Stop which container?", choices=containers).ask()
                if which:
                    stop_container(which)
            else:
                print("OK, see you soon.")



if __name__ == '__main__':
    dockerstop()
