Esempio n. 1
0
def main():
    method_types, optdoc = parse_method_types_optdoc_from_class(LogClient, LOG_CLIENT_METHOD_BLACK_LIST)

    arguments = docopt(optdoc, version=__version__)
    system_options = normalize_system_options(arguments)

    # process normal log command
    if arguments.get('log', False):
        access_id, access_key, endpoint, jmes_filter = load_config(system_options)
        method_name, args = normalize_inputs(arguments, method_types)
        assert endpoint and access_id and access_key, ValueError("endpoint, access_id or key is not configured")
        client = LogClient(endpoint, access_id, access_key)
        client.set_user_agent(USER_AGENT)

        assert hasattr(client, method_name), "Unknown parsed command:" + method_name

        try:
            ret = getattr(client, method_name)(**args)

            if jmes_filter and ret is not None and ret.get_body():
                # filter with jmes
                try:
                    print(jmespath.compile(jmes_filter).search(ret.get_body()))
                except jmespath.exceptions.ParseError as ex:
                    print("**fail to parse with JMSE path, original data: ", ex)
                    print(_sort_str_dict(ret.get_body()))
                    exit(1)
            elif ret is not None:
                print(_sort_str_dict(ret.get_body()))

        except LogException as ex:
            print(_sort_str_dict(ex.get_resp_body()))
            exit(2)

    # process configure command
    elif arguments.get('configure', False):
        args = arguments['<secure_id>'], arguments['<secure_key>'], arguments['<endpoint>'], \
               arguments['<client_name>'] or LOG_CONFIG_SECTION
        configure_confidential(*args)
Esempio n. 2
0
def main():
    method_types, method_param_usage, optdoc, usage = parse_method_types_optdoc_from_class(
        LogClient, LOG_CLIENT_METHOD_BLACK_LIST)

    arguments = docopt_ex(optdoc,
                          usage,
                          method_param_usage,
                          hlp=False,
                          ver=USER_AGENT)
    if arguments is None:
        exit(1)

    system_options = normalize_system_options(arguments)

    # process normal log command
    if arguments.get('log', False):
        try:
            access_id, access_key, endpoint, jmes_filter, format_output, decode_output = load_config(
                system_options)

            decode_output = _to_string_list(
                decode_output)  # convert decode to list if any

            method_name, args = normalize_inputs(arguments, method_types)
            if not (endpoint and access_id and access_key):
                raise IncompleteAccountInfoError(
                    "endpoint, access_id or key is not configured")

        except IncompleteAccountInfoError as ex:
            print("""
Error!

The default account is not configured or the command doesn't have a well-configured account passed. 

Fix it by either configuring a default account as: 
> aliyunlog configure <access_id> <access-key> <endpoint>

or use option --client-name to specify a well-configured account as:
> aliyunlog configure <access_id> <access-key> <endpoint> <user-bj>
> aliyunlog log .....  --client-name=user-bj

Refer to https://aliyun-log-cli.readthedocs.io/en/latest/tutorials/tutorial_configure_cli_en.html for more info.

            """)
            exit(2)

        client = LogClient(endpoint, access_id, access_key)
        client.set_user_agent(USER_AGENT)

        assert hasattr(client,
                       method_name), "Unknown parsed command:" + method_name

        data = None
        try:
            ret = getattr(client, method_name)(**args)
            jmes_filter = jmes_filter.replace("\\n", '\n')  # parse faked \n
            data = _process_response(ret, jmes_filter, format_output,
                                     decode_output)

        except LogException as ex:
            if data is not None:
                show_result(data, format_output)
            else:
                print(ex)
            exit(3)

    # process configure command
    elif arguments.get('configure', False):
        # process global options
        options = dict((k.replace('--', ''), v) for k, v in arguments.items()
                       if k.startswith('--') and v is not None)
        options.update(system_options)
        if options:
            configure_default_options(options)
        else:
            args = arguments['<secure_id>'], arguments['<secure_key>'], arguments['<endpoint>'], \
                   arguments['<client_name>'] or LOG_CONFIG_SECTION

            if args[0] is None or args[1] is None or args[1] is None:
                print("Invalid parameters.\n")
                print("Usage:\n" + MORE_DOCOPT_CMD)
                exit(1)

            configure_confidential(*args)

    # process configure command
    elif arguments.get('option', False):
        # process global options
        options = dict((k.replace('--', ''), v) for k, v in arguments.items()
                       if k.startswith('--') and v is not None)
        options.update(system_options)
        if options:
            configure_default_options(options)
        else:
            args = arguments['<secure_id>'], arguments['<secure_key>'], arguments['<endpoint>'], \
                   arguments['<client_name>'] or LOG_CONFIG_SECTION

            if args[0] is None or args[1] is None or args[1] is None:
                print("Invalid parameters.\n")
                print("Usage:\n" + MORE_DOCOPT_CMD)
                exit(1)

            configure_confidential(*args)