diff --git a/README.md b/README.md index 21025d4..7bd5462 100644 --- a/README.md +++ b/README.md @@ -714,7 +714,7 @@ $ The following is documented within the **Advanced** option of the DNS page within the Cloudflare portal. ``` -$ python -m cli4 /zones/:example.com/dns_records/export | jq -r . | egrep -v '^;;|^$' +$ python -m cli4 /zones/:example.com/dns_records/export | egrep -v '^;;|^$' $ORIGIN . @ 3600 IN SOA example.com. root.example.com. ( 2025552311 ; serial @@ -730,7 +730,7 @@ record2.example.com. 300 IN AAAA 2001:d8b::2 $ ``` -The **jq -r** option is used to convert newlines and tabs within the JSON response data. The egrep is used for documentation brevity. +The egrep is used for documentation brevity. This can also be done via Python code with the following example. ``` diff --git a/README.rst b/README.rst index 5520739..ba411eb 100644 --- a/README.rst +++ b/README.rst @@ -789,7 +789,7 @@ page within the Cloudflare portal. :: - $ python -m cli4 /zones/:example.com/dns_records/export | jq -r . | egrep -v '^;;|^$' + $ python -m cli4 /zones/:example.com/dns_records/export | egrep -v '^;;|^$' $ORIGIN . @ 3600 IN SOA example.com. root.example.com. ( 2025552311 ; serial @@ -804,8 +804,7 @@ page within the Cloudflare portal. record2.example.com. 300 IN AAAA 2001:d8b::2 $ -The **jq -r** option is used to convert newlines and tabs within the -JSON response data. The egrep is used for documentation brevity. +The egrep is used for documentation brevity. This can also be done via Python code with the following example. @@ -1035,7 +1034,7 @@ and/or Python 2.x vs 3.x support ------------------------- -As of May/June 2016 the code is now tested againt pylint. This was +As of May/June 2016 the code is now tested against pylint. This was required in order to move the codebase into Python 3.x. The motivation for this came from `Danielle Madeley (danni) `__. diff --git a/cli4/cli4.py b/cli4/cli4.py index 0e0481b..1f27651 100644 --- a/cli4/cli4.py +++ b/cli4/cli4.py @@ -281,10 +281,16 @@ def cli4(args): if len(results) == 1: results = results[0] - if output == 'json': - sys.stdout.write(json.dumps(results, indent=4, sort_keys=True) + '\n') - # json.dumps(results, sys.stdout, indent=4, sort_keys=True) - # sys.stdout.write('\n') - if output == 'yaml': - sys.stdout.write(yaml.safe_dump(results)) + if isinstance(results, str): + # if the results are a simple string, then it should be dumped directly + # this is only used for /zones/:id/dns_records/export presently + sys.stdout.write(results) + else: + # anything more complex (dict, list, etc) should be dumped as JSON/YAML + if output == 'json': + sys.stdout.write(json.dumps(results, indent=4, sort_keys=True) + '\n') + # json.dumps(results, sys.stdout, indent=4, sort_keys=True) + # sys.stdout.write('\n') + if output == 'yaml': + sys.stdout.write(yaml.safe_dump(results))