diff --git a/knack/output.py b/knack/output.py index cb71eff..0972c78 100644 --- a/knack/output.py +++ b/knack/output.py @@ -8,6 +8,7 @@ import traceback from collections import OrderedDict from io import StringIO +import sys from .events import EVENT_INVOKER_POST_PARSE_ARGS, EVENT_PARSER_GLOBAL_CREATE from .log import get_logger @@ -29,6 +30,39 @@ def default(self, o): # pylint: disable=method-hidden return o.decode() return json.JSONEncoder.default(self, o) +def format_cmd(obj): + result = obj.result + if not 'value' in result: + raise CLIError("Table output unavailable. " + "Use the --query option to specify an appropriate query. " + "Use --debug for more info.") + + value = result['value'][0] + if not 'message' in value: + raise CLIError("Table output unavailable. " + "Use the --query option to specify an appropriate query. " + "Use --debug for more info.") + + message = value['message'] + + stdout_begin = message.find("[stdout]\n") + stderr_begin = message.find("[stderr]\n") + if stdout_begin == -1 or stderr_begin == -1: + raise CLIError("Table output unavailable. " + "Use the --query option to specify an appropriate query. " + "Use --debug for more info.") + + stdout_begin += len("[stdout]\n") + # We remove two to avoid the last \n. + stderr_output = message[stdout_begin:stderr_begin - 2] + + stderr_begin += len("[stderr]\n") + stdout_output = message[stderr_begin:-2] + + print(stderr_output, file = sys.stderr) + + return stdout_output + def format_json(obj): result = obj.result @@ -99,6 +133,7 @@ class OutputProducer(object): 'table': format_table, 'tsv': format_tsv, 'none': format_none, + 'cmd': format_cmd, } @staticmethod