From: Jakob Meng <c...@jakobmeng.de> Signed-off-by: Jakob Meng <c...@jakobmeng.de> --- NEWS | 3 +++ lib/unixctl.c | 4 ++-- lib/unixctl.h | 2 +- tests/pmd.at | 2 +- utilities/ovs-appctl.c | 19 +++++++++++++++++-- 5 files changed, 24 insertions(+), 6 deletions(-)
diff --git a/NEWS b/NEWS index fc77a1613..1796895a7 100644 --- a/NEWS +++ b/NEWS @@ -15,6 +15,9 @@ Post-v3.2.0 Reported names adjusted accordingly. * Added new option [-f|--format] to choose the output format, e.g. 'json' or 'text' (by default). + * Added new option [--pretty] to print JSON output in a readable fashion. + E.g. members of objects and elements of arrays are printed one per line, + with indentation. - Python: * Added support for choosing the output format, e.g. 'json' or 'text'. diff --git a/lib/unixctl.c b/lib/unixctl.c index c82a5e92d..e7edbb154 100644 --- a/lib/unixctl.c +++ b/lib/unixctl.c @@ -553,7 +553,7 @@ unixctl_client_create(const char *path, struct jsonrpc **client) * '*err' if not NULL. */ int unixctl_client_transact(struct jsonrpc *client, const char *command, int argc, - char *argv[], enum ovs_output_fmt fmt, + char *argv[], enum ovs_output_fmt fmt, int fmt_flags, char **result, char **err) { struct jsonrpc_msg *request, *reply; @@ -622,7 +622,7 @@ unixctl_client_transact(struct jsonrpc *client, const char *command, int argc, *result = xstrdup(json_string(reply->result)); } else if (reply->result->type == JSON_OBJECT || reply->result->type == JSON_ARRAY) { - *result = json_to_string(reply->result, 0); + *result = json_to_string(reply->result, fmt_flags); } else { VLOG_WARN("%s: unexpected result type in JSON rpc reply: %s", jsonrpc_get_name(client), diff --git a/lib/unixctl.h b/lib/unixctl.h index 4b8193d9d..33d0152e5 100644 --- a/lib/unixctl.h +++ b/lib/unixctl.h @@ -39,7 +39,7 @@ int unixctl_client_create(const char *path, struct jsonrpc **client); int unixctl_client_transact(struct jsonrpc *client, const char *command, int argc, char *argv[], - enum ovs_output_fmt fmt, + enum ovs_output_fmt fmt, int fmt_flags, char **result, char **error); /* Command registration. */ diff --git a/tests/pmd.at b/tests/pmd.at index de2dc0974..21d59bf63 100644 --- a/tests/pmd.at +++ b/tests/pmd.at @@ -105,7 +105,7 @@ dummy@ovs-dummy: hit:0 missed:0 p0 1/1: (dummy-pmd: n_rxq=1, n_txq=1, numa_id=0) ]) -AT_CHECK([ovs-appctl --format json dpif/show], [0], [dnl +AT_CHECK([ovs-appctl --format json --pretty dpif/show], [0], [dnl [[ { "name": "dummy@ovs-dummy", diff --git a/utilities/ovs-appctl.c b/utilities/ovs-appctl.c index feff1be0b..606f293c5 100644 --- a/utilities/ovs-appctl.c +++ b/utilities/ovs-appctl.c @@ -26,6 +26,7 @@ #include "daemon.h" #include "dirs.h" #include "openvswitch/dynamic-string.h" +#include "openvswitch/json.h" #include "jsonrpc.h" #include "process.h" #include "timeval.h" @@ -38,6 +39,7 @@ static void usage(void); /* Parsed command line args. */ struct cmdl_args { enum ovs_output_fmt format; + int format_flags; char *target; }; @@ -67,7 +69,8 @@ main(int argc, char *argv[]) cmd_argc = argc - optind; cmd_argv = cmd_argc ? argv + optind : NULL; error = unixctl_client_transact(client, cmd, cmd_argc, cmd_argv, - args->format, &cmd_result, &cmd_error); + args->format, args->format_flags, + &cmd_result, &cmd_error); if (error) { ovs_fatal(error, "%s: transaction error", args->target); } @@ -113,6 +116,11 @@ Other options:\n\ --timeout=SECS wait at most SECS seconds for a response\n\ -f, --format=FMT Output format. One of: 'json', or 'text'\n\ ('text', by default)\n\ + --pretty By default, JSON in output is printed as compactly as\n\ + possible. This option causes JSON in output to be\n\ + printed in a more readable fashion. Members of objects\n\ + and elements of arrays are printed one per line, with\n\ + indentation.\n\ -h, --help Print this helpful information\n\ -V, --version Display ovs-appctl version information\n", program_name, program_name); @@ -124,6 +132,7 @@ cmdl_args_create(void) { struct cmdl_args *args = xmalloc(sizeof *args); args->format = OVS_OUTPUT_FMT_TEXT; + args->format_flags = 0; args->target = NULL; return args; @@ -143,7 +152,8 @@ parse_command_line(int argc, char *argv[]) { enum { OPT_START = UCHAR_MAX + 1, - VLOG_OPTION_ENUMS + OPT_PRETTY, + VLOG_OPTION_ENUMS, }; static const struct option long_options[] = { {"target", required_argument, NULL, 't'}, @@ -151,6 +161,7 @@ parse_command_line(int argc, char *argv[]) {"format", required_argument, NULL, 'f'}, {"help", no_argument, NULL, 'h'}, {"option", no_argument, NULL, 'o'}, + {"pretty", no_argument, NULL, OPT_PRETTY}, {"version", no_argument, NULL, 'V'}, {"timeout", required_argument, NULL, 'T'}, VLOG_LONG_OPTIONS, @@ -203,6 +214,10 @@ parse_command_line(int argc, char *argv[]) ovs_cmdl_print_options(long_options); exit(EXIT_SUCCESS); + case OPT_PRETTY: + args->format_flags |= JSSF_PRETTY | JSSF_SORT; + break; + case 'T': if (!str_to_uint(optarg, 10, &timeout) || !timeout) { ovs_fatal(0, "value %s on -T or --timeout is invalid", optarg); -- 2.39.2 _______________________________________________ dev mailing list d...@openvswitch.org https://mail.openvswitch.org/mailman/listinfo/ovs-dev