Merge "Improve output of --json option"
This commit is contained in:
@@ -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
|
:param json_flag: print the list as JSON instead of table
|
||||||
"""
|
"""
|
||||||
if json_flag:
|
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
|
return
|
||||||
formatters = formatters or {}
|
formatters = formatters or {}
|
||||||
mixed_case_fields = mixed_case_fields 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
|
:param json_flag: print `dict` as JSON instead of table
|
||||||
"""
|
"""
|
||||||
if json_flag:
|
if json_flag:
|
||||||
print(json.dumps(dct))
|
print(json.dumps(dct, indent=4, separators=(',', ': ')))
|
||||||
return
|
return
|
||||||
pt = prettytable.PrettyTable([dict_property, dict_value])
|
pt = prettytable.PrettyTable([dict_property, dict_value])
|
||||||
pt.align = 'l'
|
pt.align = 'l'
|
||||||
|
@@ -12,6 +12,7 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import fixtures
|
import fixtures
|
||||||
@@ -597,12 +598,7 @@ class PrintResultStringTestCase(test_base.BaseTestCase):
|
|||||||
out = sys.stdout.getvalue()
|
out = sys.stdout.getvalue()
|
||||||
sys.stdout.close()
|
sys.stdout.close()
|
||||||
sys.stdout = orig
|
sys.stdout = orig
|
||||||
expected = ['''\
|
self.assertEqual([objs[0]._info], json.loads(out))
|
||||||
[{"name": "k1", "value": 1}]
|
|
||||||
''', '''\
|
|
||||||
[{"value": 1, "name": "k1"}]
|
|
||||||
''']
|
|
||||||
self.assertIn(out, expected)
|
|
||||||
|
|
||||||
def test_print_dict_string(self):
|
def test_print_dict_string(self):
|
||||||
orig = sys.stdout
|
orig = sys.stdout
|
||||||
@@ -628,12 +624,8 @@ class PrintResultStringTestCase(test_base.BaseTestCase):
|
|||||||
out = sys.stdout.getvalue()
|
out = sys.stdout.getvalue()
|
||||||
sys.stdout.close()
|
sys.stdout.close()
|
||||||
sys.stdout = orig
|
sys.stdout = orig
|
||||||
expected = ['''\
|
expected = {"K": "k", "Key": "Value"}
|
||||||
{"K": "k", "Key": "Value"}
|
self.assertEqual(expected, json.loads(out))
|
||||||
''', '''\
|
|
||||||
{"Key": "Value", "K": "k"}
|
|
||||||
''']
|
|
||||||
self.assertIn(out, expected)
|
|
||||||
|
|
||||||
def test_print_dict_string_custom_headers(self):
|
def test_print_dict_string_custom_headers(self):
|
||||||
orig = sys.stdout
|
orig = sys.stdout
|
||||||
|
4
releasenotes/notes/add-json-option-0cf29be2a97e0212.yaml
Normal file
4
releasenotes/notes/add-json-option-0cf29be2a97e0212.yaml
Normal 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.
|
Reference in New Issue
Block a user