Skip to content

Commit 162e599

Browse files
committed
output: Add new output mode: cmd.
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 <[email protected]>
1 parent 11f2454 commit 162e599

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

knack/output.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import traceback
99
from collections import OrderedDict
1010
from io import StringIO
11+
import sys
1112

1213
from .events import EVENT_INVOKER_POST_PARSE_ARGS, EVENT_PARSER_GLOBAL_CREATE
1314
from .log import get_logger
@@ -30,6 +31,37 @@ def default(self, o): # pylint: disable=method-hidden
3031
return json.JSONEncoder.default(self, o)
3132

3233

34+
def format_cmd(obj):
35+
result = obj.result
36+
if 'value' not in result:
37+
raise CLIError(f"result ({result}) does not contain key 'value'. "
38+
"Are you sure to use cmd output for the corresponding az command?")
39+
40+
value = result['value'][0]
41+
if 'message' not in value:
42+
raise CLIError(f"value ({value}) does not contain key 'message'. "
43+
"Are you sure to use cmd output for the corresponding az command?")
44+
45+
message = value['message']
46+
47+
stdout_begin = message.find("[stdout]\n")
48+
stderr_begin = message.find("[stderr]\n")
49+
if stdout_begin == -1 or stderr_begin == -1:
50+
raise CLIError(f"message ({message}) does not contain 'stdout' or stderr. "
51+
"Are you sure to use cmd output for the corresponding az command?")
52+
53+
stdout_begin += len("[stdout]\n")
54+
# We remove two to avoid the last \n.
55+
stderr_output = message[stdout_begin:stderr_begin - 2]
56+
57+
stderr_begin += len("[stderr]\n")
58+
stdout_output = message[stderr_begin:-2]
59+
60+
print(stderr_output, file=sys.stderr)
61+
62+
return stdout_output
63+
64+
3365
def format_json(obj):
3466
result = obj.result
3567
# OrderedDict.__dict__ is always '{}', to persist the data, convert to dict first.
@@ -99,6 +131,7 @@ class OutputProducer(object):
99131
'table': format_table,
100132
'tsv': format_tsv,
101133
'none': format_none,
134+
'cmd': format_cmd,
102135
}
103136

104137
@staticmethod

0 commit comments

Comments
 (0)