Skip to content

Commit

Permalink
output: Add new output mode: cmd.
Browse files Browse the repository at this point in the history
This new output is thought to be used when executing a command in a VM/VMSS.
It permits to print the output of the command like if this was executed locally.

Signed-off-by: Francis Laniel <flaniel@linux.microsoft.com>
  • Loading branch information
eiffel-fl committed Jan 16, 2023
1 parent 11f2454 commit 65400ed
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions knack/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -99,6 +133,7 @@ class OutputProducer(object):
'table': format_table,
'tsv': format_tsv,
'none': format_none,
'cmd': format_cmd,
}

@staticmethod
Expand Down

0 comments on commit 65400ed

Please sign in to comment.