#!/usr/bin/env python

"""
Use cases

codev init --template=[template]
    -- create new project

codev install [environment] [infrastructure] [version]
    -- install project

codev start [environment] [infrastructure] [version]
    -- start installed project

codev stop [environment] [infrastructure] [version]
    -- stop installed project

codev run [script] [environment] [infrastructure] [version]
"""

import click

from codev.configuration import YAMLConfiguration
from codev.cli import execution


@click.group()
def main():
    pass


@main.command()
@click.option('-p', '--path', default='.codev', help='Path to configuration file.')
def init(path):
    """
    Create .codev file in current directory or in given file path.
    """

    if path.exists(path):
        if not click.confirm('A file "%(filepath)s" already exists. Overwrite?' % {'filepath': path}):
            return

    configuration = YAMLConfiguration()
    try:
        configuration.save_to_file(path)
    except Exception as e:
        raise click.ClickException(e)


@main.command()
@execution(
    confirmation='Install project "{configuration.project}" on environment "{environment}" with infrastructure "{infrastructure}" at installation "{installation}"'
)
def install(executor, infrastructure, installation):
    """
    install project on environment infrastructure version
    """
    executor.install(infrastructure, installation)


@main.command()
@execution(
    confirmation='Execute script "{script}" at project "{configuration.project}" on environment "{environment}" with infrastructure "{infrastructure}" at installation "{installation}"'
)
@click.argument('script')
def run(executor, script):
    executor.run(script)

if __name__ == "__main__":
    main()

