示例#1
0
def active(name):
    namespaces = list_namespaces()
    if name in map(lambda n: n['name'], namespaces):
        click.echo("Setting the active namespace to {}".format(name))
        get_user_config().set('namespace', name)
    else:
        click.echo(format_text('Namespace not found', TextStyle.FAIL))
示例#2
0
def get_session():
    session.headers.update({
        'Authorization':
        'JWT ' + get_stored_token(),
        'ACTIVE-NAMESPACE':
        get_user_config().get('namespace', None)
    })
    return session
示例#3
0
 def _get_latest_version(self):
     cached_version_info = get_user_config().get('version_info')
     if cached_version_info is None:
         latest_version = get_latest_version()
         last_check = datetime.now()
     else:
         last_check, latest_version = cached_version_info.get(
             'last_check',
             None), Version(cached_version_info.get('latest_version', None))
         if latest_version is None or (datetime.now() -
                                       last_check) > timedelta(hours=6):
             latest_version = get_latest_version()
             last_check = datetime.now()
     get_user_config().set(
         "version_info",
         dict(last_check=last_check, latest_version=str(latest_version)),
     )
     return latest_version
示例#4
0
def login_required(fn):
    # TODO: Move out of utils
    from fandogh_cli.config import get_user_config

    def please_login_first(*args, **kwargs):
        click.echo(
            "Please login first. You can do it by running 'fandogh login' command"
        )

    please_login_first.__doc__ = fn.__doc__
    token_obj = get_user_config().get('token')
    if token_obj is None:
        return please_login_first
    return fn
示例#5
0
def list():
    namespaces = list_namespaces()
    default_name_space = get_user_config().get('namespace', None)
    if default_name_space is None and len(namespaces) > 1:
        click.echo(
            format_text(
                'You already have more than 1 namespace and none selected as default namespace.\n Please select a namespace as default one',
                TextStyle.FAIL))
    click.echo('Your namespaces: ')
    for namespace in namespaces:
        message = ' * {}'.format(namespace['name'])
        if namespace['name'] == default_name_space:
            message += ' (active)'
        click.echo(message)
示例#6
0
 def _check_for_error_collection_permission(self):
     collect_error = get_user_config().get('collect_error')
     if collect_error is None:
         if os.environ.get('COLLECT_ERROR', False):
             get_user_config().set('collect_error', 'YES')
         else:
             confirmed = click.confirm(
                 'Would you like to let Fandogh CLI to send context information in case any unhandled error happens?'
             )
             if confirmed:
                 get_user_config().set("collect_error", 'YES')
             else:
                 get_user_config().set("collect_error", 'NO')
示例#7
0
def collect(cmd, ctx, exception=None):
    try:
        let_collection = get_user_config().get('collect_error', 'NO')
        if let_collection == 'YES':
            info = dict(_static_info)
            info['cmd'] = cmd.name
            info['params'] = ctx.params
            info['error'] = exception.message if hasattr(exception, 'message') else str(exception)

            report(info)

    except Exception as e:
        click.echo(format_text('Error in reporting problem. Please share this error with to help us to improve the service.', TextStyle.FAIL), err=True)
        click.echo(format_text('Caused by {}'.format(exception), TextStyle.FAIL), err=True)
        click.echo(format_text('Report error: {}'.format(e), TextStyle.FAIL), err=True)
示例#8
0
    def _get_latest_version(self):
        try:

            cached_version_info = get_user_config().get('version_info')
            if cached_version_info is None:
                latest_version = get_latest_version()
                last_check = datetime.now()
            else:
                last_check, latest_version = cached_version_info.get(
                    'last_check', None), Version(
                        cached_version_info.get('latest_version', None))
                if latest_version is None or (datetime.now() -
                                              last_check) > timedelta(hours=6):
                    latest_version = get_latest_version()
                    last_check = datetime.now()
            get_user_config().set(
                "version_info",
                dict(last_check=last_check,
                     latest_version=str(latest_version)),
            )
            return latest_version
        except Exception as exp:
            collect(self, {"error": "Error while getting latest version"}, exp)
            return None
示例#9
0
    def _check_for_error_collection_permission(self):
        def get_value(value):
            if value is None:
                return None

            value = str(value).lower()
            if value in ['no', '0', 'n', 'false']:
                return 'NO'
            return 'YES'

        collect_error = get_user_config().get('collect_error')
        if collect_error is None:
            env_value = get_value(os.environ.get('COLLECT_ERROR'))
            if env_value is not None:
                get_user_config().set('collect_error', env_value)
            else:
                confirmed = click.confirm(
                    'Would you like to let Fandogh CLI to send context information in case any unhandled error happens?'
                )
                if confirmed:
                    get_user_config().set("collect_error", 'YES')
                else:
                    get_user_config().set("collect_error", 'NO')
示例#10
0
def get_stored_token():
    token_obj = get_user_config().get('token')
    if token_obj is None:
        raise AuthenticationError()
    return token_obj