mirror of
https://github.com/openvswitch/ovs
synced 2025-08-29 05:18:13 +00:00
appctl: Add option '--pretty' for pretty-printing JSON output.
With the '--pretty' option, ovs-appctl will now print JSON output in a more readable fashion, i.e. with additional line breaks, spaces and sorted dictionary keys. Signed-off-by: Jakob Meng <code@jakobmeng.de> Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
This commit is contained in:
parent
97a1bce6aa
commit
a0925cef7d
@ -9,6 +9,7 @@ Synopsis
|
|||||||
[``--target=``<target> | ``-t`` <target>]
|
[``--target=``<target> | ``-t`` <target>]
|
||||||
[``--timeout=``<secs> | ``-T`` <secs>]
|
[``--timeout=``<secs> | ``-T`` <secs>]
|
||||||
[``--format=``<format> | ``-f`` <format>]
|
[``--format=``<format> | ``-f`` <format>]
|
||||||
|
[``--pretty``]
|
||||||
<command> [<arg>...]
|
<command> [<arg>...]
|
||||||
|
|
||||||
``ovs-appctl --help``
|
``ovs-appctl --help``
|
||||||
@ -79,6 +80,13 @@ In normal use only a single option is accepted:
|
|||||||
|
|
||||||
{"reply-format":"plain","reply":"$PLAIN_TEXT_HERE"}
|
{"reply-format":"plain","reply":"$PLAIN_TEXT_HERE"}
|
||||||
|
|
||||||
|
* ``--pretty``
|
||||||
|
|
||||||
|
By default, JSON output is printed as compactly as possible. This option
|
||||||
|
causes JSON in output to be printed in a more readable fashion. For
|
||||||
|
example, members of objects and elements of arrays are printed one
|
||||||
|
per line, with indentation. Requires ``--format=json``.
|
||||||
|
|
||||||
Common Commands
|
Common Commands
|
||||||
===============
|
===============
|
||||||
|
|
||||||
|
1
NEWS
1
NEWS
@ -5,6 +5,7 @@ Post-v3.3.0
|
|||||||
- ovs-appctl:
|
- ovs-appctl:
|
||||||
* Added new option [-f|--format] to choose the output format, e.g. 'json'
|
* Added new option [-f|--format] to choose the output format, e.g. 'json'
|
||||||
or 'text' (by default).
|
or 'text' (by default).
|
||||||
|
* Added new option [--pretty] to print JSON output in a readable fashion.
|
||||||
- Userspace datapath:
|
- Userspace datapath:
|
||||||
* Conntrack now supports 'random' flag for selecting ports in a range
|
* Conntrack now supports 'random' flag for selecting ports in a range
|
||||||
while natting and 'persistent' flag for selection of the IP address
|
while natting and 'persistent' flag for selection of the IP address
|
||||||
|
@ -276,4 +276,10 @@ AT_CHECK_UNQUOTED([ovs-appctl --format json version], [0], [dnl
|
|||||||
{"reply":"$ovs_version","reply-format":"plain"}
|
{"reply":"$ovs_version","reply-format":"plain"}
|
||||||
])
|
])
|
||||||
|
|
||||||
|
AT_CHECK_UNQUOTED([ovs-appctl --format json --pretty version], [0], [dnl
|
||||||
|
{
|
||||||
|
"reply": "$ovs_version",
|
||||||
|
"reply-format": "plain"}
|
||||||
|
])
|
||||||
|
|
||||||
AT_CLEANUP
|
AT_CLEANUP
|
||||||
|
@ -40,13 +40,15 @@ static void usage(void);
|
|||||||
/* Parsed command line args. */
|
/* Parsed command line args. */
|
||||||
struct cmdl_args {
|
struct cmdl_args {
|
||||||
enum unixctl_output_fmt format;
|
enum unixctl_output_fmt format;
|
||||||
|
unsigned int format_flags;
|
||||||
char *target;
|
char *target;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct cmdl_args *cmdl_args_create(void);
|
static struct cmdl_args *cmdl_args_create(void);
|
||||||
static struct cmdl_args *parse_command_line(int argc, char *argv[]);
|
static struct cmdl_args *parse_command_line(int argc, char *argv[]);
|
||||||
static struct jsonrpc *connect_to_target(const char *target);
|
static struct jsonrpc *connect_to_target(const char *target);
|
||||||
static char *reply_to_string(struct json *reply, enum unixctl_output_fmt fmt);
|
static char *reply_to_string(struct json *reply, enum unixctl_output_fmt fmt,
|
||||||
|
unsigned int fmt_flags);
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
@ -84,7 +86,7 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (cmd_error) {
|
if (cmd_error) {
|
||||||
jsonrpc_close(client);
|
jsonrpc_close(client);
|
||||||
msg = reply_to_string(cmd_error, UNIXCTL_OUTPUT_FMT_TEXT);
|
msg = reply_to_string(cmd_error, UNIXCTL_OUTPUT_FMT_TEXT, 0);
|
||||||
fputs(msg, stderr);
|
fputs(msg, stderr);
|
||||||
free(msg);
|
free(msg);
|
||||||
ovs_error(0, "%s: server returned an error", args->target);
|
ovs_error(0, "%s: server returned an error", args->target);
|
||||||
@ -108,13 +110,13 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (cmd_error) {
|
if (cmd_error) {
|
||||||
jsonrpc_close(client);
|
jsonrpc_close(client);
|
||||||
msg = reply_to_string(cmd_error, UNIXCTL_OUTPUT_FMT_TEXT);
|
msg = reply_to_string(cmd_error, UNIXCTL_OUTPUT_FMT_TEXT, 0);
|
||||||
fputs(msg, stderr);
|
fputs(msg, stderr);
|
||||||
free(msg);
|
free(msg);
|
||||||
ovs_error(0, "%s: server returned an error", args->target);
|
ovs_error(0, "%s: server returned an error", args->target);
|
||||||
exit(2);
|
exit(2);
|
||||||
} else if (cmd_result) {
|
} else if (cmd_result) {
|
||||||
msg = reply_to_string(cmd_result, args->format);
|
msg = reply_to_string(cmd_result, args->format, args->format_flags);
|
||||||
fputs(msg, stdout);
|
fputs(msg, stdout);
|
||||||
free(msg);
|
free(msg);
|
||||||
} else {
|
} else {
|
||||||
@ -151,6 +153,8 @@ Other options:\n\
|
|||||||
--timeout=SECS wait at most SECS seconds for a response\n\
|
--timeout=SECS wait at most SECS seconds for a response\n\
|
||||||
-f, --format=FMT Output format. One of: 'json', or 'text'\n\
|
-f, --format=FMT Output format. One of: 'json', or 'text'\n\
|
||||||
(default: text)\n\
|
(default: text)\n\
|
||||||
|
--pretty Format the output in a more readable fashion.\n\
|
||||||
|
Requires: --format=json.\n\
|
||||||
-h, --help Print this helpful information\n\
|
-h, --help Print this helpful information\n\
|
||||||
-V, --version Display ovs-appctl version information\n",
|
-V, --version Display ovs-appctl version information\n",
|
||||||
program_name, program_name);
|
program_name, program_name);
|
||||||
@ -163,6 +167,7 @@ cmdl_args_create(void)
|
|||||||
struct cmdl_args *args = xmalloc(sizeof *args);
|
struct cmdl_args *args = xmalloc(sizeof *args);
|
||||||
|
|
||||||
args->format = UNIXCTL_OUTPUT_FMT_TEXT;
|
args->format = UNIXCTL_OUTPUT_FMT_TEXT;
|
||||||
|
args->format_flags = 0;
|
||||||
args->target = NULL;
|
args->target = NULL;
|
||||||
|
|
||||||
return args;
|
return args;
|
||||||
@ -173,7 +178,8 @@ parse_command_line(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
enum {
|
enum {
|
||||||
OPT_START = UCHAR_MAX + 1,
|
OPT_START = UCHAR_MAX + 1,
|
||||||
VLOG_OPTION_ENUMS
|
OPT_PRETTY,
|
||||||
|
VLOG_OPTION_ENUMS,
|
||||||
};
|
};
|
||||||
static const struct option long_options[] = {
|
static const struct option long_options[] = {
|
||||||
{"target", required_argument, NULL, 't'},
|
{"target", required_argument, NULL, 't'},
|
||||||
@ -181,6 +187,7 @@ parse_command_line(int argc, char *argv[])
|
|||||||
{"format", required_argument, NULL, 'f'},
|
{"format", required_argument, NULL, 'f'},
|
||||||
{"help", no_argument, NULL, 'h'},
|
{"help", no_argument, NULL, 'h'},
|
||||||
{"option", no_argument, NULL, 'o'},
|
{"option", no_argument, NULL, 'o'},
|
||||||
|
{"pretty", no_argument, NULL, OPT_PRETTY},
|
||||||
{"version", no_argument, NULL, 'V'},
|
{"version", no_argument, NULL, 'V'},
|
||||||
{"timeout", required_argument, NULL, 'T'},
|
{"timeout", required_argument, NULL, 'T'},
|
||||||
VLOG_LONG_OPTIONS,
|
VLOG_LONG_OPTIONS,
|
||||||
@ -190,6 +197,7 @@ parse_command_line(int argc, char *argv[])
|
|||||||
char *short_options = xasprintf("+%s", short_options_);
|
char *short_options = xasprintf("+%s", short_options_);
|
||||||
struct cmdl_args *args = cmdl_args_create();
|
struct cmdl_args *args = cmdl_args_create();
|
||||||
unsigned int timeout = 0;
|
unsigned int timeout = 0;
|
||||||
|
bool pretty = false;
|
||||||
int e_options;
|
int e_options;
|
||||||
|
|
||||||
e_options = 0;
|
e_options = 0;
|
||||||
@ -232,6 +240,10 @@ parse_command_line(int argc, char *argv[])
|
|||||||
ovs_cmdl_print_options(long_options);
|
ovs_cmdl_print_options(long_options);
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
|
|
||||||
|
case OPT_PRETTY:
|
||||||
|
pretty = true;
|
||||||
|
break;
|
||||||
|
|
||||||
case 'T':
|
case 'T':
|
||||||
if (!str_to_uint(optarg, 10, &timeout) || !timeout) {
|
if (!str_to_uint(optarg, 10, &timeout) || !timeout) {
|
||||||
ovs_fatal(0, "value %s on -T or --timeout is invalid", optarg);
|
ovs_fatal(0, "value %s on -T or --timeout is invalid", optarg);
|
||||||
@ -261,6 +273,13 @@ parse_command_line(int argc, char *argv[])
|
|||||||
"(use --help for help)");
|
"(use --help for help)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (pretty) {
|
||||||
|
if (args->format != UNIXCTL_OUTPUT_FMT_JSON) {
|
||||||
|
ovs_fatal(0, "--pretty is supported with --format json only");
|
||||||
|
}
|
||||||
|
args->format_flags |= JSSF_PRETTY;
|
||||||
|
}
|
||||||
|
|
||||||
if (!args->target) {
|
if (!args->target) {
|
||||||
args->target = "ovs-vswitchd";
|
args->target = "ovs-vswitchd";
|
||||||
}
|
}
|
||||||
@ -309,7 +328,8 @@ connect_to_target(const char *target)
|
|||||||
/* The caller is responsible for freeing the returned string, with free(), when
|
/* The caller is responsible for freeing the returned string, with free(), when
|
||||||
* it is no longer needed. */
|
* it is no longer needed. */
|
||||||
static char *
|
static char *
|
||||||
reply_to_string(struct json *reply, enum unixctl_output_fmt fmt)
|
reply_to_string(struct json *reply, enum unixctl_output_fmt fmt,
|
||||||
|
unsigned int fmt_flags)
|
||||||
{
|
{
|
||||||
ovs_assert(reply);
|
ovs_assert(reply);
|
||||||
|
|
||||||
@ -324,7 +344,7 @@ reply_to_string(struct json *reply, enum unixctl_output_fmt fmt)
|
|||||||
if (fmt == UNIXCTL_OUTPUT_FMT_TEXT) {
|
if (fmt == UNIXCTL_OUTPUT_FMT_TEXT) {
|
||||||
ds_put_cstr(&ds, json_string(reply));
|
ds_put_cstr(&ds, json_string(reply));
|
||||||
} else {
|
} else {
|
||||||
json_to_ds(reply, JSSF_SORT, &ds);
|
json_to_ds(reply, JSSF_SORT | fmt_flags, &ds);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ds_last(&ds) != EOF && ds_last(&ds) != '\n') {
|
if (ds_last(&ds) != EOF && ds_last(&ds) != '\n') {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user