# Path: aeriscloud/cli/helpers.py
# class Command(click.Command):
#     # This is inherited by the Context to switch between pure POSIX and
#     # argparse-like argument parsing. Setting it to false will push any
#     # unknown flag to the argument located where the flag was declared
#     allow_interspersed_args = config.get('config', 'posix', default=False)
# 
#     def __init__(self, *args, **kwargs):
#         cs = dict(help_option_names=['-h', '--help'])
#         super(Command, self).__init__(context_settings=cs,
#                                       *args, **kwargs)
# 
# def info(text, **kwargs):
#     click.secho(text, fg='cyan', **kwargs)
# 
# def fatal(text, code=1, **kwargs):
#     error(text, **kwargs)
#     sys.exit(code)
# 
# def success(text, **kwargs):
#     click.secho(text, fg='green', **kwargs)
# 
# def warning(text, **kwargs):
#     click.secho(text, fg='yellow', err=True, **kwargs)
# 
# def move_shell_to(path):
#     """
#     If the process is called through the function set in scripts/env.sh,
#     will move the user to the given path on successful exit
# 
#     :param path: str
#     """
#     if 'AERIS_CD_TMP_FILE' in os.environ:
#         with open(os.environ['AERIS_CD_TMP_FILE'], 'w') as f:
#             f.write(path)
# 
# Path: aeriscloud/ansible.py
# def ansible_env(env):
#     def __init__(self, inventory, hostname):
#     def ssh_host(self):
#     def ssh_key(self):
#     def ssh_user(self):
#     def variables(self):
# def get_organization_list():
# def get_env_path(organization):
# def get_env_file(organization, environment):
# def list_jobs():
# def get_job_file(job):
# def get_inventory_file(inventory):
# def get_inventory_list():
# def run_job(job, inventory, *args, **kwargs):
# def run_env(organization, environment, inventory, *args, **kwargs):
# def run_playbook(playbook, inventory, *args, **kwargs):
# def run(inventory, shell_cmd, limit, *args, **kwargs):
# def shell(inventory, *args, **kwargs):
#     def __init__(self, inventory_path):
#     def get_ansible_inventory(self):
#     def get_hosts(self, pattern='all'):
#     def get_groups(self):
# class ACHost(object):
# class Inventory(object):


import click
import os
import shlex
import shutil
import sys
from sh import git, ErrorReturnCode
from aeriscloud.cli.helpers import Command, info, fatal, success, warning, \
    move_shell_to
from aeriscloud.ansible import inventory_path, get_inventory_list
    from subprocess import call
def update():
    for inventory in os.listdir(inventory_path):
        if not os.path.exists(os.path.join(inventory_path, inventory, '.git')):
            info("Skip %s" % inventory)
            continue

        info("Updating %s ..." % inventory)
        os.chdir(os.path.join(inventory_path, inventory))
        try:
            vgit('pull')
        except ErrorReturnCode:
            fatal("Unable to update the inventories.")
    success("All the inventories have been updated.")


def update_inventory(name, dest_path):
    if not os.path.exists(os.path.join(dest_path, '.git')):
        warning("The %s inventory is not a git repository and has not "
                "been updated." % name)
        return

    click.echo('We will update the %s inventory' % name)
    os.chdir(dest_path)
    try:
        vgit('pull')
    except ErrorReturnCode:
        fatal("Unable to update the inventory %s." % name)
    success("The %s inventory has been updated." % name)



# Path: aeriscloud/cli/helpers.py
# class Command(click.Command):
#     # This is inherited by the Context to switch between pure POSIX and
#     # argparse-like argument parsing. Setting it to false will push any
#     # unknown flag to the argument located where the flag was declared
#     allow_interspersed_args = config.get('config', 'posix', default=False)
# 
#     def __init__(self, *args, **kwargs):
#         cs = dict(help_option_names=['-h', '--help'])
#         super(Command, self).__init__(context_settings=cs,
#                                       *args, **kwargs)
# 
# def info(text, **kwargs):
#     click.secho(text, fg='cyan', **kwargs)
# 
# def fatal(text, code=1, **kwargs):
#     error(text, **kwargs)
#     sys.exit(code)
# 
# def success(text, **kwargs):
#     click.secho(text, fg='green', **kwargs)
# 
# def warning(text, **kwargs):
#     click.secho(text, fg='yellow', err=True, **kwargs)
# 
# def move_shell_to(path):
#     """
#     If the process is called through the function set in scripts/env.sh,
#     will move the user to the given path on successful exit
# 
#     :param path: str
#     """
#     if 'AERIS_CD_TMP_FILE' in os.environ:
#         with open(os.environ['AERIS_CD_TMP_FILE'], 'w') as f:
#             f.write(path)
# 
# Path: aeriscloud/ansible.py
# def ansible_env(env):
#     def __init__(self, inventory, hostname):
#     def ssh_host(self):
#     def ssh_key(self):
#     def ssh_user(self):
#     def variables(self):
# def get_organization_list():
# def get_env_path(organization):
# def get_env_file(organization, environment):
# def list_jobs():
# def get_job_file(job):
# def get_inventory_file(inventory):
# def get_inventory_list():
# def run_job(job, inventory, *args, **kwargs):
# def run_env(organization, environment, inventory, *args, **kwargs):
# def run_playbook(playbook, inventory, *args, **kwargs):
# def run(inventory, shell_cmd, limit, *args, **kwargs):
# def shell(inventory, *args, **kwargs):
#     def __init__(self, inventory_path):
#     def get_ansible_inventory(self):
#     def get_hosts(self, pattern='all'):
#     def get_groups(self):
# class ACHost(object):
# class Inventory(object):


# Path: aeriscloud/cli/cloud/inventory.py
import click
import os
import shlex
import shutil
import sys
from sh import git, ErrorReturnCode
from aeriscloud.cli.helpers import Command, info, fatal, success, warning, \
    move_shell_to
from aeriscloud.ansible import inventory_path, get_inventory_list
    from subprocess import call

def update():
    for inventory in os.listdir(inventory_path):
        if not os.path.exists(os.path.join(inventory_path, inventory, '.git')):
            info("Skip %s" % inventory)
            continue

        info("Updating %s ..." % inventory)
        os.chdir(os.path.join(inventory_path, inventory))
        try:
            vgit('pull')
        except ErrorReturnCode:
            fatal("Unable to update the inventories.")
    success("All the inventories have been updated.")


def update_inventory(name, dest_path):
    if not os.path.exists(os.path.join(dest_path, '.git')):
        warning("The %s inventory is not a git repository and has not "
                "been updated." % name)
        return

    click.echo('We will update the %s inventory' % name)
    os.chdir(dest_path)
    try:
        vgit('pull')
    except ErrorReturnCode:
        fatal("Unable to update the inventory %s." % name)
    success("The %s inventory has been updated." % name)



@cli.command(cls=Command)