Example #1
0
    def test_sl_error(self, stdoutmock, climock):
        ex = SoftLayer.SoftLayerAPIError('SoftLayer_Exception', 'Not found')
        climock.side_effect = ex

        with self.assertRaises(SystemExit):
            core.main()

        self.assertIn("SoftLayerAPIError(SoftLayer_Exception): Not found",
                      stdoutmock.getvalue())
Example #2
0
def cli(ctx, env):
    """Enters a shell for slcli."""

    # Set up the environment
    env = copy.deepcopy(env)
    env.load_modules_from_python(routes.ALL_ROUTES)
    env.aliases.update(routes.ALL_ALIASES)
    env.vars['global_args'] = ctx.parent.params
    env.vars['is_shell'] = True
    env.vars['last_exit_code'] = 0

    # Set up prompt_toolkit settings
    app_path = click.get_app_dir('softlayer_shell')
    if not os.path.exists(app_path):
        os.makedirs(app_path)
    complete = completer.ShellCompleter(core.cli)

    session = PromptSession()

    while True:
        try:
            line = session.prompt(
                completer=complete,
                complete_while_typing=True,
                auto_suggest=AutoSuggestFromHistory(),
            )

            # Parse arguments
            try:
                args = shlex.split(line)
            except ValueError as ex:
                print("Invalid Command: %s" % ex)
                continue

            if not args:
                continue

            # Run Command
            try:
                # Reset client so that the client gets refreshed
                env.client = None
                core.main(args=list(get_env_args(env)) + args,
                          obj=env,
                          prog_name="",
                          reraise_exceptions=True)
            except SystemExit as ex:
                env.vars['last_exit_code'] = ex.code
            except EOFError:
                return
            except ShellExit:
                return
            except Exception as ex:
                env.vars['last_exit_code'] = 1
                traceback.print_exc(file=sys.stderr)

        except KeyboardInterrupt:
            env.vars['last_exit_code'] = 130
Example #3
0
    def test_auth_error(self, stdoutmock, climock):
        ex = SoftLayer.SoftLayerAPIError('SoftLayer_Exception',
                                         'Invalid API token.')
        climock.side_effect = ex

        with self.assertRaises(SystemExit):
            core.main()

        self.assertIn("Authentication Failed:", stdoutmock.getvalue())
        self.assertIn("use 'sl config setup'", stdoutmock.getvalue())
Example #4
0
def cli(ctx, env):
    """Enters a shell for slcli."""

    # Set up the environment
    env = copy.deepcopy(env)
    env.load_modules_from_python(routes.ALL_ROUTES)
    env.aliases.update(routes.ALL_ALIASES)
    env.vars['global_args'] = ctx.parent.params
    env.vars['is_shell'] = True
    env.vars['last_exit_code'] = 0

    # Set up prompt_toolkit settings
    app_path = click.get_app_dir('softlayer_shell')
    if not os.path.exists(app_path):
        os.makedirs(app_path)
    complete = completer.ShellCompleter(core.cli)

    while True:
        try:
            line = p_shortcuts.prompt(
                completer=complete,
                complete_while_typing=True,
                auto_suggest=p_auto_suggest.AutoSuggestFromHistory(),
            )

            # Parse arguments
            try:
                args = shlex.split(line)
            except ValueError as ex:
                print("Invalid Command: %s" % ex)
                continue

            if not args:
                continue

            # Run Command
            try:
                # Reset client so that the client gets refreshed
                env.client = None
                core.main(args=list(get_env_args(env)) + args,
                          obj=env,
                          prog_name="",
                          reraise_exceptions=True)
            except SystemExit as ex:
                env.vars['last_exit_code'] = ex.code
            except EOFError:
                return
            except ShellExit:
                return
            except Exception as ex:
                env.vars['last_exit_code'] = 1
                traceback.print_exc(file=sys.stderr)

        except KeyboardInterrupt:
            env.vars['last_exit_code'] = 130
Example #5
0
    def test_unexpected_error(self, stdoutmock, climock):
        climock.side_effect = AttributeError('Attribute foo does not exist')

        with self.assertRaises(SystemExit):
            core.main()

        self.assertIn("Feel free to report this error as it is likely a bug",
                      stdoutmock.getvalue())
        self.assertIn("Traceback (most recent call last)",
                      stdoutmock.getvalue())
        self.assertIn("AttributeError: Attribute foo does not exist",
                      stdoutmock.getvalue())
Example #6
0
def cli(ctx, env):
    """Enters a shell for slcli."""

    # Set up the environment
    env = copy.deepcopy(env)
    env.load_modules_from_python(routes.ALL_ROUTES)
    env.aliases.update(routes.ALL_ALIASES)
    env.vars['global_args'] = ctx.parent.params
    env.vars['is_shell'] = True
    env.vars['last_exit_code'] = 0

    # Set up prompt_toolkit settings
    app_path = click.get_app_dir('softlayer')
    if not os.path.exists(app_path):
        os.makedirs(app_path)
    history = p_history.FileHistory(os.path.join(app_path, 'history'))
    complete = completer.ShellCompleter()

    while True:
        try:
            line = p_shortcuts.get_input(
                u"(%s)> " % env.vars['last_exit_code'],
                completer=complete,
                history=history,
            )
            try:
                args = shlex.split(line)
            except ValueError as ex:
                print("Invalid Command: %s" % ex)
                continue

            if not args:
                continue

            # Reset client so that the client gets refreshed
            env.client = None

            core.main(args=list(get_env_args(env)) + args,
                      obj=env,
                      prog_name="",
                      reraise_exceptions=True)
        except SystemExit as ex:
            env.vars['last_exit_code'] = ex.code
        except KeyboardInterrupt:
            env.vars['last_exit_code'] = 1
        except EOFError:
            return
        except ShellExit:
            return
        except Exception as ex:
            env.vars['last_exit_code'] = 1
            traceback.print_exc(file=sys.stderr)
Example #7
0
def cli(ctx, env):
    """Enters a shell for slcli."""

    # Set up the environment
    env = copy.deepcopy(env)
    env.load_modules_from_python(routes.ALL_ROUTES)
    env.aliases.update(routes.ALL_ALIASES)
    env.vars["global_args"] = ctx.parent.params
    env.vars["is_shell"] = True
    env.vars["last_exit_code"] = 0

    # Set up prompt_toolkit settings
    app_path = click.get_app_dir("softlayer")
    if not os.path.exists(app_path):
        os.makedirs(app_path)
    history = p_history.FileHistory(os.path.join(app_path, "history"))
    complete = completer.ShellCompleter()

    while True:
        try:
            line = p_shortcuts.get_input("(%s)> " % env.vars["last_exit_code"], completer=complete, history=history)
            try:
                args = shlex.split(line)
            except ValueError as ex:
                print("Invalid Command: %s" % ex)
                continue

            if not args:
                continue

            # Reset client so that the client gets refreshed
            env.client = None

            core.main(args=list(get_env_args(env)) + args, obj=env, prog_name="", reraise_exceptions=True)
        except SystemExit as ex:
            env.vars["last_exit_code"] = ex.code
        except KeyboardInterrupt:
            env.vars["last_exit_code"] = 1
        except EOFError:
            return
        except ShellExit:
            return
        except Exception as ex:
            env.vars["last_exit_code"] = 1
            traceback.print_exc(file=sys.stderr)
Example #8
0
def cli(ctx, env):
    """Enters a shell for slcli."""

    # Set up the environment
    env = copy.deepcopy(env)
    env.load_modules_from_python(routes.ALL_ROUTES)
    env.aliases.update(routes.ALL_ALIASES)
    env.vars['global_args'] = ctx.parent.params
    env.vars['is_shell'] = True
    env.vars['last_exit_code'] = 0

    # Set up prompt_toolkit settings
    app_path = click.get_app_dir('softlayer_shell')
    if not os.path.exists(app_path):
        os.makedirs(app_path)
    history = p_history.FileHistory(os.path.join(app_path, 'history'))
    complete = completer.ShellCompleter(core.cli)

    while True:
        def get_prompt_tokens(_):
            """Returns tokens for the command prompt"""
            tokens = []
            try:
                tokens.append((token.Token.Username, env.client.auth.username))
                tokens.append((token.Token.At, "@"))
            except AttributeError:
                pass

            tokens.append((token.Token.Host, "slcli-shell"))
            if env.vars['last_exit_code']:
                tokens.append((token.Token.ErrorPrompt, '> '))
            else:
                tokens.append((token.Token.Prompt, '> '))

            return tokens

        try:
            line = p_shortcuts.prompt(
                completer=complete,
                history=history,
                auto_suggest=p_auto_suggest.AutoSuggestFromHistory(),
                get_prompt_tokens=get_prompt_tokens,
            )

            # Parse arguments
            try:
                args = shlex.split(line)
            except ValueError as ex:
                print("Invalid Command: %s" % ex)
                continue

            if not args:
                continue

            # Run Command
            try:
                # Reset client so that the client gets refreshed
                env.client = None
                core.main(args=list(get_env_args(env)) + args,
                          obj=env,
                          prog_name="",
                          reraise_exceptions=True)
            except SystemExit as ex:
                env.vars['last_exit_code'] = ex.code
            except EOFError:
                return
            except ShellExit:
                return
            except Exception as ex:
                env.vars['last_exit_code'] = 1
                traceback.print_exc(file=sys.stderr)

        except KeyboardInterrupt:
            env.vars['last_exit_code'] = 130
Example #9
0
def cli(ctx, env):
    """Enters a shell for slcli."""

    # Set up the environment
    env = copy.deepcopy(env)
    env.load_modules_from_python(routes.ALL_ROUTES)
    env.aliases.update(routes.ALL_ALIASES)
    env.vars['global_args'] = ctx.parent.params
    env.vars['is_shell'] = True
    env.vars['last_exit_code'] = 0

    # Set up prompt_toolkit settings
    app_path = click.get_app_dir('softlayer_shell')
    if not os.path.exists(app_path):
        os.makedirs(app_path)
    history = p_history.FileHistory(os.path.join(app_path, 'history'))
    complete = completer.ShellCompleter(core.cli)

    while True:

        def get_prompt_tokens(_):
            """Returns tokens for the command prompt"""
            tokens = []
            try:
                tokens.append((token.Token.Username, env.client.auth.username))
                tokens.append((token.Token.At, "@"))
            except AttributeError:
                pass

            tokens.append((token.Token.Host, "slcli-shell"))
            if env.vars['last_exit_code']:
                tokens.append((token.Token.ErrorPrompt, '> '))
            else:
                tokens.append((token.Token.Prompt, '> '))

            return tokens

        try:
            line = p_shortcuts.prompt(
                completer=complete,
                history=history,
                auto_suggest=p_auto_suggest.AutoSuggestFromHistory(),
                get_prompt_tokens=get_prompt_tokens,
            )

            # Parse arguments
            try:
                args = shlex.split(line)
            except ValueError as ex:
                print("Invalid Command: %s" % ex)
                continue

            if not args:
                continue

            # Run Command
            try:
                # Reset client so that the client gets refreshed
                env.client = None
                core.main(args=list(get_env_args(env)) + args,
                          obj=env,
                          prog_name="",
                          reraise_exceptions=True)
            except SystemExit as ex:
                env.vars['last_exit_code'] = ex.code
            except EOFError:
                return
            except ShellExit:
                return
            except Exception as ex:
                env.vars['last_exit_code'] = 1
                traceback.print_exc(file=sys.stderr)

        except KeyboardInterrupt:
            env.vars['last_exit_code'] = 130