Skip to content

Commit 65400ed

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 65400ed

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

knack/output.py

+35
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
@@ -29,6 +30,39 @@ def default(self, o): # pylint: disable=method-hidden
2930
return o.decode()
3031
return json.JSONEncoder.default(self, o)
3132

33+
def format_cmd(obj):
34+
result = obj.result
35+
if not 'value' in result:
36+
raise CLIError("Table output unavailable. "
37+
"Use the --query option to specify an appropriate query. "
38+
"Use --debug for more info.")
39+
40+
value = result['value'][0]
41+
if not 'message' in value:
42+
raise CLIError("Table output unavailable. "
43+
"Use the --query option to specify an appropriate query. "
44+
"Use --debug for more info.")
45+
46+
message = value['message']
47+
48+
stdout_begin = message.find("[stdout]\n")
49+
stderr_begin = message.find("[stderr]\n")
50+
if stdout_begin == -1 or stderr_begin == -1:
51+
raise CLIError("Table output unavailable. "
52+
"Use the --query option to specify an appropriate query. "
53+
"Use --debug for more info.")
54+
55+
stdout_begin += len("[stdout]\n")
56+
# We remove two to avoid the last \n.
57+
stderr_output = message[stdout_begin:stderr_begin - 2]
58+
59+
stderr_begin += len("[stderr]\n")
60+
stdout_output = message[stderr_begin:-2]
61+
62+
print(stderr_output, file = sys.stderr)
63+
64+
return stdout_output
65+
3266

3367
def format_json(obj):
3468
result = obj.result
@@ -99,6 +133,7 @@ class OutputProducer(object):
99133
'table': format_table,
100134
'tsv': format_tsv,
101135
'none': format_none,
136+
'cmd': format_cmd,
102137
}
103138

104139
@staticmethod

0 commit comments

Comments
 (0)