From 2941f9f0cbadfce0a7ad71a6d35c555b4342a165 Mon Sep 17 00:00:00 2001 From: Mike McLean Date: Apr 25 2017 18:05:11 +0000 Subject: PR#352 Optional JSON output for 'koji call' Merges #352 https://pagure.io/koji/pull-request/352 --- diff --git a/cli/koji b/cli/koji index 7a906fe..a777d4c 100755 --- a/cli/koji +++ b/cli/koji @@ -1345,6 +1345,7 @@ def handle_call(options, session, args): parser = OptionParser(usage=usage) parser.add_option("--python", action="store_true", help=_("Use python syntax for values")) parser.add_option("--kwargs", help=_("Specify keyword arguments as a dictionary (implies --python)")) + parser.add_option("--json-output", action="store_true", help=_("Use JSON syntax for output")) (options, args) = parser.parse_args(args) if len(args) < 1: parser.error(_("Please specify the name of the XML-RPC method")) @@ -1353,6 +1354,8 @@ def handle_call(options, session, args): options.python = True if options.python and ast is None: parser.error(_("The ast module is required to read python syntax")) + if options.json_output and json is None: + parser.error(_("The json module is required to output JSON syntax")) activate_session(session) name = args[0] non_kw = [] @@ -1368,7 +1371,11 @@ def handle_call(options, session, args): kw[key] = arg_filter(value) else: non_kw.append(arg_filter(arg)) - pprint.pprint(getattr(session, name).__call__(*non_kw, **kw)) + response = getattr(session, name).__call__(*non_kw, **kw) + if options.json_output: + print(json.dumps(response, indent=2, separators=(',', ': '))) + else: + pprint.pprint(response) def anon_handle_mock_config(options, session, args): "[info] Create a mock config"