From patchwork Fri Apr 12 07:26:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1922884 X-Patchwork-Delegate: i.maximets@samsung.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=DtVmO2I1; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VG7S069Qpz1yYM for ; Fri, 12 Apr 2024 17:27:12 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 96B36827CA; Fri, 12 Apr 2024 07:27:10 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id JbtZ_3qFWEui; Fri, 12 Apr 2024 07:27:09 +0000 (UTC) X-Comment: SPF check N/A for local connections - client-ip=2605:bc80:3010:104::8cd3:938; helo=lists.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver= DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org 8FDF7826F5 Authentication-Results: smtp1.osuosl.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=DtVmO2I1 Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp1.osuosl.org (Postfix) with ESMTPS id 8FDF7826F5; Fri, 12 Apr 2024 07:27:04 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 0B34EC0DDA; Fri, 12 Apr 2024 07:27:04 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 27093C0DD7 for ; Fri, 12 Apr 2024 07:27:02 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 473F782661 for ; Fri, 12 Apr 2024 07:27:01 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id 6v-OhqA2hDgW for ; Fri, 12 Apr 2024 07:26:57 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=170.10.129.124; helo=us-smtp-delivery-124.mimecast.com; envelope-from=jmeng@redhat.com; receiver= DMARC-Filter: OpenDMARC Filter v1.4.2 smtp1.osuosl.org CDF1E82531 Authentication-Results: smtp1.osuosl.org; dmarc=pass (p=none dis=none) header.from=redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org CDF1E82531 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by smtp1.osuosl.org (Postfix) with ESMTPS id CDF1E82531 for ; Fri, 12 Apr 2024 07:26:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712906813; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FUX3JrAfmZIoXwguuOlggZIbHx2+QSjUz0pv3E5vtf4=; b=DtVmO2I1hhRHe8rPhHc4vQjyO0Yk2CajM+9S7zPnFarWesX6SUaHLdhm20x8COtS+3O+rL WYVmxtW5VIfGzrtyvN8NDyC14K6rCUg43PhIi8NWmFFJdNuLS5sy2540Z/OZj6E/My8AlX WJfPWq0cpFddU0Of0fqs9RU5QJyVXfw= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-81-HuDCHIyVNMmDTrKd2v8MkA-1; Fri, 12 Apr 2024 03:26:52 -0400 X-MC-Unique: HuDCHIyVNMmDTrKd2v8MkA-1 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-a52213a0d50so21618166b.2 for ; Fri, 12 Apr 2024 00:26:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712906811; x=1713511611; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FUX3JrAfmZIoXwguuOlggZIbHx2+QSjUz0pv3E5vtf4=; b=dtsdwsEMJxo21qoNKZlJAwQyEU/3smMvbxbdVNlNSJT9EzwmQzdhxY+GzT2wRJ78Oe u2GFwSODWrqkvNzY3be7NJkMq2z7CPIsMcT2eqBmYFhRvielFzFl2XtGvzoRO8yKf6/m X1JShKohHPSuXmxtaBBZWBilaqtsnH+dzsiGoz5XUKDm2vYA2aQ6qeg7w1yMij0/oLVb sYxb2dZnx4b6MkHMM2C9TKvULsE2EN+FXC64AUV+KRETzldnUqFPoenWeJ4NyrQVPyL5 Opp1+RsyAcdMkkrLo2zKwKK+XD4j4Lczo2w0NdMj3Diaiq/Ckb5LAQNFT9fzMqRjCDTj Fztw== X-Gm-Message-State: AOJu0YwNB9te/YHuLakY8LeHh0+7g2XcAkxIUhYJmy92wULlnZD6F3So exqMMtSYmhTRp7apTcJ1lr3EseOg4DbQ0Sa4xH7t/cgD0PbrDlbUjuURV7YFHGWKX3jJjGRJ6B4 uXUwrMfIkP4GPsNJMuxIMxW+pG++XAImoGX1jVha5cmVhzCUxALrJcQYfo7Wr+wBJ2mZgmMkGpo Utj6GzJwx6G8ZeMAW4SdlXoaFjcbmZ X-Received: by 2002:a17:907:2d93:b0:a52:274e:4f63 with SMTP id gt19-20020a1709072d9300b00a52274e4f63mr1398793ejc.7.1712906810933; Fri, 12 Apr 2024 00:26:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFn7lCMm3CFUEQfFHmv6GuD/A/jIxvyYmPRMx1hbCW7V1ktxy0cS90kbNVFtkLMjZvH/UIIhw== X-Received: by 2002:a17:907:2d93:b0:a52:274e:4f63 with SMTP id gt19-20020a1709072d9300b00a52274e4f63mr1398774ejc.7.1712906810572; Fri, 12 Apr 2024 00:26:50 -0700 (PDT) Received: from positronik4lide.. ([87.122.58.150]) by smtp.gmail.com with ESMTPSA id t2-20020a1709064f0200b00a51dd500071sm1502373eju.169.2024.04.12.00.26.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 00:26:50 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org, echaudro@redhat.com, ktraynor@redhat.com, aconole@redhat.com, rjarry@redhat.com Date: Fri, 12 Apr 2024 09:26:37 +0200 Message-Id: <20240412072638.712622-6-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240412072638.712622-1-jmeng@redhat.com> References: <20240412072638.712622-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v9 5/6] vswitchd: Add JSON output for 'list-commands' command. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" From: Jakob Meng 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 --- 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(); + 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); + } } - } - 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 +]) +AT_CHECK([grep -E '^\[[\{"name":.*\}\]]$' stdout], [0], [ignore]) + +AT_CLEANUP