#!python

from json import loads
import sys
import select

import click

from codev.perform.cli import command, main


@command(
    help='Run configuration.',
    confirmation="Run configuration '{configuration_with_option}' of project '{project}'?"
)
def run(codev_perform):
    """
    Deploy project

    :param codev_perform:
    :type codev_perform: codev.CodevPerform
    :return:
    :rtype: bool
    """
    if select.select([sys.stdin,], [], [], 0.0)[0]:
        load_vars = loads(sys.stdin.read())
    else:
        load_vars = {}
    return codev_perform.run(load_vars)


@command(help='Execute command in project context in isolation.')
@click.argument('command', nargs=-1)
def execute(codev_perform, command):
    """
    :param codev_perform:
    :type codev_perform: codev.CodevPerform
    :param command:
    :type command: str
    :return:
    :rtype: bool
    """

    if select.select([sys.stdin], [], [], 0.0)[0]:
        arguments = loads(sys.stdin.read())
    else:
        arguments = {}
    return codev_perform.execute(' '.join(command), arguments)

if __name__ == "__main__":
    main()

