Skip to content

Commit 379d036

Browse files
JM1igsilya
authored andcommitted
vswitchd: Add JSON output for 'list-commands' command.
The 'list-commands' command now supports machine-readable JSON output in addition to the plain-text output for humans. Reported-at: https://bugzilla.redhat.com/1824861 Signed-off-by: Jakob Meng <code@jakobmeng.de> Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
1 parent 3c572af commit 379d036

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Post-v3.3.0
66
* Added new option [-f|--format] to choose the output format, e.g. 'json'
77
or 'text' (by default).
88
* Added new option [--pretty] to print JSON output in a readable fashion.
9+
* 'list-commands' now supports output in JSON format.
910
- Userspace datapath:
1011
* Conntrack now supports 'random' flag for selecting ports in a range
1112
while natting and 'persistent' flag for selection of the IP address

lib/unixctl.c

+29-13
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,40 @@ static void
9595
unixctl_list_commands(struct unixctl_conn *conn, int argc OVS_UNUSED,
9696
const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
9797
{
98-
struct ds ds = DS_EMPTY_INITIALIZER;
99-
const struct shash_node **nodes = shash_sort(&commands);
100-
size_t i;
98+
if (unixctl_command_get_output_format(conn) == UNIXCTL_OUTPUT_FMT_JSON) {
99+
struct json *json_commands = json_object_create();
100+
const struct shash_node *node;
101101

102-
ds_put_cstr(&ds, "The available commands are:\n");
102+
SHASH_FOR_EACH (node, &commands) {
103+
const struct unixctl_command *command = node->data;
103104

104-
for (i = 0; i < shash_count(&commands); i++) {
105-
const struct shash_node *node = nodes[i];
106-
const struct unixctl_command *command = node->data;
105+
if (command->usage) {
106+
json_object_put_string(json_commands, node->name,
107+
command->usage);
108+
}
109+
}
110+
unixctl_command_reply_json(conn, json_commands);
111+
} else {
112+
struct ds ds = DS_EMPTY_INITIALIZER;
113+
const struct shash_node **nodes = shash_sort(&commands);
114+
size_t i;
115+
116+
ds_put_cstr(&ds, "The available commands are:\n");
107117

108-
if (command->usage) {
109-
ds_put_format(&ds, " %-23s %s\n", node->name, command->usage);
118+
for (i = 0; i < shash_count(&commands); ++i) {
119+
const struct shash_node *node = nodes[i];
120+
const struct unixctl_command *command = node->data;
121+
122+
if (command->usage) {
123+
ds_put_format(&ds, " %-23s %s\n", node->name,
124+
command->usage);
125+
}
110126
}
111-
}
112-
free(nodes);
127+
free(nodes);
113128

114-
unixctl_command_reply(conn, ds_cstr(&ds));
115-
ds_destroy(&ds);
129+
unixctl_command_reply(conn, ds_cstr(&ds));
130+
ds_destroy(&ds);
131+
}
116132
}
117133

118134
static void

tests/ovs-vswitchd.at

+15
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,18 @@ AT_CHECK_UNQUOTED([ovs-appctl --format json --pretty version], [0], [dnl
283283
])
284284

285285
AT_CLEANUP
286+
287+
AT_SETUP([ovs-vswitchd list-commands])
288+
OVS_VSWITCHD_START
289+
290+
AT_CHECK([ovs-appctl list-commands], [0], [ignore])
291+
AT_CHECK([ovs-appctl --format json list-commands], [0], [stdout])
292+
293+
# Check that ovs-appctl prints a single line with a trailing newline.
294+
AT_CHECK([wc -l stdout], [0], [1 stdout
295+
])
296+
297+
# Check that ovs-appctl prints a JSON document.
298+
AT_CHECK([ovstest test-json stdout], [0], [ignore])
299+
300+
AT_CLEANUP

0 commit comments

Comments
 (0)