diff options
author | Clay Gerrard <clay.gerrard@gmail.com> | 2019-07-02 11:23:22 -0500 |
---|---|---|
committer | Tim Burke <tim.burke@gmail.com> | 2019-07-09 13:25:52 -0700 |
commit | 936631eac617c83bec1f1c44b1adcaa51d36329c (patch) | |
tree | 1e727d61a4392ca087742bd9c9cc238cf6c97fd6 /swiftclient/shell.py | |
parent | 4b3e33b3c28892616d42cf3f1497dd8db63c783b (diff) | |
download | python-swiftclient-936631eac617c83bec1f1c44b1adcaa51d36329c.tar.gz |
Optionally display listings in raw json
Symlinks have recently added some new keys to container listings. It's
very convenient to be able to see and reason about the extra information
in container listings.
Allowing raw json output is similar with what the client already does
for the info command, and it's forward compatible with any listing
enhancements added by future middleware development.
Change-Id: I88fb38529342ac4e4198aeccd2f10c69c7396704
Diffstat (limited to 'swiftclient/shell.py')
-rwxr-xr-x | swiftclient/shell.py | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/swiftclient/shell.py b/swiftclient/shell.py index 0459533..2be85af 100755 --- a/swiftclient/shell.py +++ b/swiftclient/shell.py @@ -33,7 +33,8 @@ from sys import argv as sys_argv, exit, stderr, stdin from time import gmtime, strftime from swiftclient import RequestException -from swiftclient.utils import config_true_value, generate_temp_url, prt_bytes +from swiftclient.utils import config_true_value, generate_temp_url, \ + prt_bytes, JSONableIterable from swiftclient.multithreading import OutputManager from swiftclient.exceptions import ClientException from swiftclient import __version__ as client_version @@ -578,6 +579,8 @@ def st_list(parser, args, output_manager, return_parser=False): help='Roll up items with the given delimiter. For containers ' 'only. See OpenStack Swift API documentation for ' 'what this means.') + parser.add_argument('-j', '--json', action='store_true', + help='print listing information in json') parser.add_argument( '-H', '--header', action='append', dest='header', default=[], @@ -616,6 +619,20 @@ def st_list(parser, args, output_manager, return_parser=False): else: stats_parts_gen = swift.list(container=container) + if options.get('json', False): + def listing(stats_parts_gen=stats_parts_gen): + for stats in stats_parts_gen: + if stats["success"]: + for item in stats['listing']: + yield item + else: + raise stats["error"] + + json.dump( + JSONableIterable(listing()), output_manager.print_stream, + sort_keys=True, indent=2) + output_manager.print_msg('') + return for stats in stats_parts_gen: if stats["success"]: _print_stats(options, stats, human) |