Improve output of --json option

Indent the output of commands when using the --json option. This patch
is a follow-up patch for https://review.openstack.org/#/c/283519/

Change-Id: Ieb3ffd8c362b37ad9b78bd3ecd98499b26c03bd0
This commit is contained in:
Aline Bousquet 2016-03-02 23:08:27 +00:00
parent 992ed7c758
commit 01ff3dfd2a
3 changed files with 11 additions and 14 deletions

View File

@ -154,7 +154,8 @@ def print_list(objs, fields, formatters=None, sortby_index=0,
:param json_flag: print the list as JSON instead of table
"""
if json_flag:
print(json.dumps([o._info for o in objs]))
print(json.dumps([o._info for o in objs], indent=4,
separators=(',', ': ')))
return
formatters = formatters or {}
mixed_case_fields = mixed_case_fields or []
@ -205,7 +206,7 @@ def print_dict(dct, dict_property="Property", wrap=0, dict_value='Value',
:param json_flag: print `dict` as JSON instead of table
"""
if json_flag:
print(json.dumps(dct))
print(json.dumps(dct, indent=4, separators=(',', ': ')))
return
pt = prettytable.PrettyTable([dict_property, dict_value])
pt.align = 'l'

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
import sys
import fixtures
@ -597,12 +598,7 @@ class PrintResultStringTestCase(test_base.BaseTestCase):
out = sys.stdout.getvalue()
sys.stdout.close()
sys.stdout = orig
expected = ['''\
[{"name": "k1", "value": 1}]
''', '''\
[{"value": 1, "name": "k1"}]
''']
self.assertIn(out, expected)
self.assertEqual([objs[0]._info], json.loads(out))
def test_print_dict_string(self):
orig = sys.stdout
@ -628,12 +624,8 @@ class PrintResultStringTestCase(test_base.BaseTestCase):
out = sys.stdout.getvalue()
sys.stdout.close()
sys.stdout = orig
expected = ['''\
{"K": "k", "Key": "Value"}
''', '''\
{"Key": "Value", "K": "k"}
''']
self.assertIn(out, expected)
expected = {"K": "k", "Key": "Value"}
self.assertEqual(expected, json.loads(out))
def test_print_dict_string_custom_headers(self):
orig = sys.stdout

View File

@ -0,0 +1,4 @@
---
features:
- Add a --json option to the client to display the JSON
response body from the Ironic API without formatting it.