# Path: lain_cli/utils.py
# def lain_yaml_data():
#     if not os.path.exists(LAIN_YAML_PATH):
#         error('Missing lain.yaml under current directory')
#         sys.exit(1)
#     with open(LAIN_YAML_PATH) as f:
#         data = f.read()
#     return yaml.load(data)
# 
# def check_phase(phase):
#     if phase not in PHASE_CHOICES:
#         error('phase %s not in available phases: %s.' % (phase, PHASE_CHOICES))
#         warn(
#             'phase can be added using: lain config save %s domain {{ domain }}' % phase)
#         exit(1)
# 
# Path: lain_cli/utils.py
# class TwoLevelCommandBase(object):
#     __metaclass__ = ABCMeta
# 
#     @abstractmethod
#     def subcommands(self):
#         '''return subcommand function list'''
# 
#     @abstractmethod
#     def namespace(self):
#         '''return namespace string'''
# 
#     @abstractmethod
#     def help_message(self):
#         '''return help message string'''
# 
# def get_domain(phase):
#     domain = user_config.get_config().get(phase, {}).get(DOMAIN_KEY, None)
#     if not domain:
#         error('please set %s domain by "lain config save %s %s ${%s_domain}"'
#               % (phase, phase, DOMAIN_KEY, phase))
#         exit(1)
#     return domain


import argparse
import requests, json
from argh.decorators import arg, aliases
from lain_sdk.util import warn
from lain_cli.utils import lain_yaml_data, check_phase
from lain_cli.utils import TwoLevelCommandBase, get_domain
    @classmethod
    def namespace(self):
        return "backup"

    @classmethod
    def help_message(self):
        return "operations for backup"

    @classmethod
    @arg('phase', help="lain cluster phase id, can be added by lain config save")
    def jobs(cls, phase):
        """
        list backup jobs for this lain app
        """
        check_phase(phase)
        appname = lain_yaml_data()['appname']
        route = "api/v2/app/%s/cron/jobs" % appname
        data = cls._request('GET', phase, route, None)
        if data:
            print json.dumps(data, indent=2)

    @classmethod
    @arg('phase', nargs=1, help="lain cluster phase id, can be added by lain config save")
    @arg('proc', nargs=1, help="proc name")
    @arg('path', nargs=1, help="the path of incremental backup, if full-backup given, will show backup's file info")
    @aliases('ls')
    def list(cls, phase, proc, path):
        """
        list files in the incremental backup direcotry
        """

# Path: lain_cli/utils.py
# def lain_yaml_data():
#     if not os.path.exists(LAIN_YAML_PATH):
#         error('Missing lain.yaml under current directory')
#         sys.exit(1)
#     with open(LAIN_YAML_PATH) as f:
#         data = f.read()
#     return yaml.load(data)
# 
# def check_phase(phase):
#     if phase not in PHASE_CHOICES:
#         error('phase %s not in available phases: %s.' % (phase, PHASE_CHOICES))
#         warn(
#             'phase can be added using: lain config save %s domain {{ domain }}' % phase)
#         exit(1)
# 
# Path: lain_cli/utils.py
# class TwoLevelCommandBase(object):
#     __metaclass__ = ABCMeta
# 
#     @abstractmethod
#     def subcommands(self):
#         '''return subcommand function list'''
# 
#     @abstractmethod
#     def namespace(self):
#         '''return namespace string'''
# 
#     @abstractmethod
#     def help_message(self):
#         '''return help message string'''
# 
# def get_domain(phase):
#     domain = user_config.get_config().get(phase, {}).get(DOMAIN_KEY, None)
#     if not domain:
#         error('please set %s domain by "lain config save %s %s ${%s_domain}"'
#               % (phase, phase, DOMAIN_KEY, phase))
#         exit(1)
#     return domain


# Path: lain_cli/backup.py
import argparse
import requests, json
from argh.decorators import arg, aliases
from lain_sdk.util import warn
from lain_cli.utils import lain_yaml_data, check_phase
from lain_cli.utils import TwoLevelCommandBase, get_domain

    @classmethod
    def namespace(self):
        return "backup"

    @classmethod
    def help_message(self):
        return "operations for backup"

    @classmethod
    @arg('phase', help="lain cluster phase id, can be added by lain config save")
    def jobs(cls, phase):
        """
        list backup jobs for this lain app
        """
        check_phase(phase)
        appname = lain_yaml_data()['appname']
        route = "api/v2/app/%s/cron/jobs" % appname
        data = cls._request('GET', phase, route, None)
        if data:
            print json.dumps(data, indent=2)

    @classmethod
    @arg('phase', nargs=1, help="lain cluster phase id, can be added by lain config save")
    @arg('proc', nargs=1, help="proc name")
    @arg('path', nargs=1, help="the path of incremental backup, if full-backup given, will show backup's file info")
    @aliases('ls')
    def list(cls, phase, proc, path):
        """
        list files in the incremental backup direcotry
        """

        check_phase(phase[0])