On 4/12/24 09:26, [email protected] wrote:
> From: Jakob Meng <[email protected]>
> 
> 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 <[email protected]>
> ---
>  NEWS                  |  1 +
>  lib/unixctl.c         | 46 ++++++++++++++++++++++++++++++-------------
>  tests/ovs-vswitchd.at | 11 +++++++++++
>  3 files changed, 44 insertions(+), 14 deletions(-)
> 
> diff --git a/NEWS b/NEWS
> index 957351acb..af83623b3 100644
> --- a/NEWS
> +++ b/NEWS
> @@ -13,6 +13,7 @@ Post-v3.3.0
>       * 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.
> +     * 'list-commands' now supports output in JSON format.
>     - Python:
>       * Added support for choosing the output format, e.g. 'json' or 'text'.
>       * Added new option [--pretty] to print JSON output in a readable 
> fashion.
> diff --git a/lib/unixctl.c b/lib/unixctl.c
> index 67cf26418..ca54884d0 100644
> --- a/lib/unixctl.c
> +++ b/lib/unixctl.c
> @@ -68,24 +68,42 @@ static void
>  unixctl_list_commands(struct unixctl_conn *conn, int argc OVS_UNUSED,
>                        const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
>  {
> -    struct ds ds = DS_EMPTY_INITIALIZER;
> -    const struct shash_node **nodes = shash_sort(&commands);
> -    size_t i;
> +    if (unixctl_command_get_output_format(conn) == OVS_OUTPUT_FMT_JSON) {
> +        struct json *json_commands = json_array_create_empty();
> +        const struct shash_node *node;
>  
> -    ds_put_cstr(&ds, "The available commands are:\n");
> +        SHASH_FOR_EACH (node, &commands) {
> +            const struct unixctl_command *command = node->data;
>  
> -    for (i = 0; i < shash_count(&commands); i++) {
> -        const struct shash_node *node = nodes[i];
> -        const struct unixctl_command *command = node->data;
> -
> -        if (command->usage) {
> -            ds_put_format(&ds, "  %-23s %s\n", node->name, command->usage);
> +            if (command->usage) {
> +                struct json *json_command = json_object_create();

An empty line.

> +                json_object_put_string(json_command, "name", node->name);
> +                json_object_put_string(json_command, "usage", 
> command->usage);
> +                json_array_add(json_commands, json_command);

Can it be just a { "name": "usage", "name1": "usage1", ... } map
instead of array of objects?

> +            }
>          }
> -    }
> -    free(nodes);
> +        unixctl_command_reply_json(conn, json_commands);
> +    } else {
> +        struct ds ds = DS_EMPTY_INITIALIZER;
> +        const struct shash_node **nodes = shash_sort(&commands);
> +        size_t i;
>  
> -    unixctl_command_reply(conn, ds_cstr(&ds));
> -    ds_destroy(&ds);
> +        ds_put_cstr(&ds, "The available commands are:\n");
> +
> +        for (i = 0; i < shash_count(&commands); ++i) {
> +            const struct shash_node *node = nodes[i];
> +            const struct unixctl_command *command = node->data;
> +
> +            if (command->usage) {
> +                ds_put_format(&ds, "  %-23s %s\n", node->name,
> +                              command->usage);
> +            }
> +        }
> +        free(nodes);
> +
> +        unixctl_command_reply(conn, ds_cstr(&ds));
> +        ds_destroy(&ds);
> +    }
>  }
>  
>  static void
> diff --git a/tests/ovs-vswitchd.at b/tests/ovs-vswitchd.at
> index 5683896ef..dcda71d04 100644
> --- a/tests/ovs-vswitchd.at
> +++ b/tests/ovs-vswitchd.at
> @@ -281,3 +281,14 @@ AT_CHECK_UNQUOTED([ovs-appctl --format json --pretty 
> version], [0], [dnl
>    "reply-format": "plain"}])
>  
>  AT_CLEANUP
> +
> +AT_SETUP([ovs-vswitchd list-commands])
> +OVS_VSWITCHD_START
> +
> +AT_CHECK([ovs-appctl list-commands], [0], [ignore])
> +AT_CHECK([ovs-appctl --format json list-commands], [0], [stdout])
> +AT_CHECK([wc -l stdout], [0], [0 stdout
> +])

This line looks strange.  What does it do?

> +AT_CHECK([grep -E '^\[[\{"name":.*\}\]]$' stdout], [0], [ignore])
> +
> +AT_CLEANUP

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to