Skip to content

Commit

Permalink
fix issue #49
Browse files Browse the repository at this point in the history
  • Loading branch information
wjo1212 committed Dec 10, 2018
1 parent b9e6cbf commit 57d54e0
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 21 deletions.
18 changes: 12 additions & 6 deletions aliyunlogcli/cli_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,31 @@ def show_result(result, format_output, decode_output=None):
if isinstance(result, (six.text_type, six.binary_type)):
print(result)
else:
fmt = format_output.lower().strip()
escape = 'no_escape' not in fmt
json_fmt = 'json' in fmt
if six.PY2:
last_ex = None
for encoding in encodings:
try:
if format_output.lower().strip() == 'json':
print(json.dumps(result, sort_keys=True, indent=2, separators=(',', ': '), encoding=encoding))
if json_fmt:
print(json.dumps(result, sort_keys=True, indent=2, separators=(',', ': '),
encoding=encoding, ensure_ascii=escape))
else:
print(json.dumps(result, sort_keys=True, encoding=encoding))
print(json.dumps(result, sort_keys=True, encoding=encoding, ensure_ascii=escape))

break
except UnicodeDecodeError as ex:
last_ex = ex
else:
raise last_ex
else:
if format_output.lower().strip() == 'json':
print(json.dumps(result, sort_keys=True, indent=2, separators=(',', ': '), cls=get_encoder_cls(encodings)))
if json_fmt:
print(json.dumps(result, sort_keys=True, indent=2, separators=(',', ': '),
cls=get_encoder_cls(encodings), ensure_ascii=escape))
else:
print(json.dumps(result, sort_keys=True, cls=get_encoder_cls(encodings)))
print(json.dumps(result, sort_keys=True, cls=get_encoder_cls(encodings),
ensure_ascii=escape))


def _process_response_data(data, jmes_filter, format_output, decode_output):
Expand Down
20 changes: 10 additions & 10 deletions aliyunlogcli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@

GLOBAL_OPTIONS_STR = """
Global Options:
[--access-id=<value>] : use this access id in this command
[--access-key=<value>] : use this access key in this command
[--region-endpoint=<value>] : use this endpoint in this command
[--client-name=<value>] : use this client name in configured accounts
[--jmes-filter=<value>] : filter results using JMES syntax
[--format-output=json] : print formatted json results or else print in one line
[--decode-output=<value>] : encoding list to decode response, comma separated like "utf8,lartin1,gbk", default is "utf8".
[--access-id=<value>] : use this access id in this command
[--access-key=<value>] : use this access key in this command
[--region-endpoint=<value>] : use this endpoint in this command
[--client-name=<value>] : use this client name in configured accounts
[--jmes-filter=<value>] : filter results using JMES syntax
[--format-output=json,no_escape] : print formatted json results or else print in one line; if escape non-ANSI or not with `no_escape`. like: "json", "json,no_escape", "no_escape"
[--decode-output=<value>] : encoding list to decode response, comma separated like "utf8,lartin1,gbk", default is "utf8".
Refer to http://aliyun-log-cli.readthedocs.io/ for more info.
"""
Expand All @@ -50,22 +50,22 @@
1. aliyunlog log <subcommand> [parameters | global options]
2. aliyunlog configure <access_id> <access-key> <endpoint> [<client-name>]
3. aliyunlog configure [--format-output=json] [--default-client=<client_name>] [--decode-output=utf8,latin1]
3. aliyunlog configure [--format-output=json,no_escape] [--default-client=<client_name>] [--decode-output=utf8,latin1]
4. aliyunlog [--help | --version]
Examples:
1. aliyunlog configure AKID123 AKKEY123 cn-hangzhou.log.aliyuncs.com
2. aliyunlog configure --format-output=json --default-client=beijing
2. aliyunlog configure --format-output=json,no_escape --default-client=beijing
3. aliyunlog log create_project --project_name="test"
Subcommand:
{grouped_api}
""" + GLOBAL_OPTIONS_STR

MORE_DOCOPT_CMD = """aliyunlog configure <secure_id> <secure_key> <endpoint> [<client_name>]
aliyunlog configure [--format-output=json] [--default-client=<client_name>] [--decode-output=utf8,latin1]
aliyunlog configure [--format-output=json,no_escape] [--default-client=<client_name>] [--decode-output=utf8,latin1]
"""

DEBUG_SECTION_NAME = "__logging__"
Expand Down
19 changes: 19 additions & 0 deletions doc/tutorials/tutorial_configure_cli_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CLI的配置项目包含如下内容:
- 配置多个账户,以便访问特定区域,用于测试或者跨域操作等。
- [输出格式](#输出格式)
- 修改输出json的格式化方式,默认是在一行打印。
- 修改输出非英文转义方式,默认转义.


<h2 id="配置秘钥与服务入口">配置秘钥与服务入口</h2>
Expand Down Expand Up @@ -117,6 +118,8 @@ aliyunlog configure --default-client=beijing

<h1 id="输出格式">输出格式</h1>

**JSON格式**

输出格式一般是以json形式输出,并且是打印在一行上面,某些情况下需要格式化输出方便查看,可以在特定命令上配置`--format-output=json`,这样CLI会将输出进行格式化。
```shell
aliyunlog log get_log.... --format-output=json
Expand All @@ -127,3 +130,19 @@ aliyunlog log get_log.... --format-output=json
```shell
aliyunlog log configure --format-output=json
```

**Not escape Non-ANSI**

对于非英文字符, 默认是转义为如"\uAB12"的形式. 如果期望可以看到原始字符, 可以添加`no_escape``format-ouput`选项中:

```shell
aliyunlog log get_log.... --format-output=json,no_escape
```

与其他选项用逗号分隔即可.
如果期望所有输出都是这样,可以修改配置项来完成:

```shell
aliyunlog log configure --format-output=json,no_escape
```

17 changes: 17 additions & 0 deletions doc/tutorials/tutorial_configure_cli_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ This command will switch default account to the one `beijing`.

## Format output

**JSON Format**

Output is in json format normally, which is printed in one line in console. In some cases, it's useful to show them in pretty format. You could use option `--format-output=json` in each command for this:
```shell
aliyunlog log get_log.... --format-output=json
Expand All @@ -127,3 +129,18 @@ But if you want to make it a default behavior, configure it directly:
```shell
aliyunlog log configure --format-output=json
```

**Not escape Non-ANSI**

By default, for non-ANSI characters, it will escape it as as "\uAB12". If you want to view the raw string, you could add a `no_escape` to format of output:
```shell
aliyunlog log get_log.... --format-output=json,no_escape
```
It could be `no_escape` or combine with other value in `format-output` with `,`

And if you want to make it a default behavior, configure it directly:

```shell
aliyunlog log configure --format-output=json,no_escape
```

2 changes: 1 addition & 1 deletion doc/tutorials/tutorial_get_resource_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
例如:

```shell
aliyunlog log get_resource_usage --project="my project name" --format-output=json
aliyunlog log get_resource_usage --project="my project name" --format-output=json,no_escape
```

返回的格式是一个json,例如:
Expand Down
6 changes: 3 additions & 3 deletions options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ Usage:

1. aliyunlog log <subcommand> [parameters | global options]
2. aliyunlog configure <access_id> <access-key> <endpoint> [<client-name>]
3. aliyunlog configure [--format-output=json] [--default-client=<client_name>] [--decode-output=utf8,latin1]
3. aliyunlog configure [--format-output=json,no_escape] [--default-client=<client_name>] [--decode-output=utf8,latin1]
4. aliyunlog [--help | --version]


Examples:

1. aliyunlog configure AKID123 AKKEY123 cn-hangzhou.log.aliyuncs.com
2. aliyunlog configure --format-output=json --default-client=beijing
2. aliyunlog configure --format-output=json,no_escape --default-client=beijing
3. aliyunlog log create_project --project_name="test"

Subcommand:
Expand Down Expand Up @@ -137,7 +137,7 @@ Global Options:
[--region-endpoint=<value>] : use this endpoint in this command
[--client-name=<value>] : use this client name in configured accounts
[--jmes-filter=<value>] : filter results using JMES syntax
[--format-output=json] : print formatted json results or else print in one line
[--format-output=json,no_escape] : print formatted json results or else print in one line; if escape non-ANSI or not with `no_escape`. like: "json", "json,no_escape", "no_escape"
[--decode-output=<value>] : encoding list to decode response, comma separated like "utf8,lartin1,gbk", default is "utf8".

Refer to http://aliyun-log-cli.readthedocs.io/ for more info.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import re
import sys

install_requires = ['aliyun-log-python-sdk>=0.6.11', 'jmespath', 'docopt']
install_requires = ['aliyun-log-python-sdk>=0.6.37', 'jmespath', 'docopt']

if sys.version_info[:2] == (2, 6):
install_requires += ['ordereddict']
Expand Down

0 comments on commit 57d54e0

Please sign in to comment.