From patchwork Thu May 16 07:20:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1935815 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=BclN56CV; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::136; helo=smtp3.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp3.osuosl.org (smtp3.osuosl.org [IPv6:2605:bc80:3010::136]) (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 4Vg1hm33WCz20KF for ; Thu, 16 May 2024 17:20:40 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 07B5760DC2; Thu, 16 May 2024 07:20:38 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp3.osuosl.org ([127.0.0.1]) by localhost (smtp3.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id Lvs8ng4gUBPI; Thu, 16 May 2024 07:20:35 +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 smtp3.osuosl.org 1F07760DF1 Authentication-Results: smtp3.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=BclN56CV Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp3.osuosl.org (Postfix) with ESMTPS id 1F07760DF1; Thu, 16 May 2024 07:20:35 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id C1B1EC0DD5; Thu, 16 May 2024 07:20:34 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp2.osuosl.org (smtp2.osuosl.org [IPv6:2605:bc80:3010::133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 19830C0037 for ; Thu, 16 May 2024 07:20:32 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 15F87401CA for ; Thu, 16 May 2024 07:20:32 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id meSGTdV6TS6o for ; Thu, 16 May 2024 07:20:31 +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 smtp2.osuosl.org 10BA040B96 Authentication-Results: smtp2.osuosl.org; dmarc=pass (p=none dis=none) header.from=redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 10BA040B96 Authentication-Results: smtp2.osuosl.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=BclN56CV Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by smtp2.osuosl.org (Postfix) with ESMTPS id 10BA040B96 for ; Thu, 16 May 2024 07:20:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715844029; 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=DFnaZj3pXR/HFD/yNtUHiNlrmVlrWAUDyqZ6SotuayU=; b=BclN56CVUAeb2olcJEz/3Z2TrkX92F38e2Lse7mqj2ukULjzCYm9iYbwdWEx/a0NIMSs9J vOeqjyViAD4y1Ku+HGtY28x/tzWxuFWeR6wSh1UD8SfRSlwOOKBvB+ob26PW8MDFFX6lhU SIZy8799rT1d90/q5HJDmv5EENQVRvc= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-671-uGpxvoGQMryBay1OSbO0Wg-1; Thu, 16 May 2024 03:20:28 -0400 X-MC-Unique: uGpxvoGQMryBay1OSbO0Wg-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-41ffb6bc28fso28240095e9.2 for ; Thu, 16 May 2024 00:20:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715844027; x=1716448827; 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=DFnaZj3pXR/HFD/yNtUHiNlrmVlrWAUDyqZ6SotuayU=; b=NDpQ5ZAdDa2neCtSU/hFA/CRdFcxhb2rgz2ZPQ/oj4wqoA/AaziNt5KtBqJjbaOrBI rhtY5FE2MORKaAe4YVMLrKuqFHWj19+LFtv+PSMTzeWjHCE6o31fPZJ+OjQHntbD1moe tF0YJFoXvTbHb+03KYh4q8gO4XiUSDe5WTKUH9OkvfQ5gf5RCHPthFkSrKqdpu7E+xKR G+Rw2PHxu5G8Md7G9qTEpTIgHnhoV0o8Enu8yZ2B71nRUEbjCSoihhJh/WqRsecUXkNi OQStpsV5Lz88s8UDvzTbBFzSqFmNadMV2dgas8Zf4W6+s2s+Av5AmKpq8vIMTOGsXOdf l7Cw== X-Gm-Message-State: AOJu0YyiLIps+zWmoH6YUqAVjZPN8Bn6TPIz7j65gAxrv9pKCAaE6Q75 yv/LWMoyFKwLybRNpMoJcZ8hXoM0PYmH0gO5yPlOWz9zxiKzUf6cSD2pTzTPvvpC4uSdjoFiOxc JR+kXM0DOV8EL4bmWwjFFjQa6BdwzLlsD5ERUxdcC0FaXqVWpHgCtN5LTXnIPXKefrXWWYlt2jU bYytHLRh9arfsmpEz01P10kbYRqRV6 X-Received: by 2002:a05:600c:4e8c:b0:419:f241:633b with SMTP id 5b1f17b1804b1-41fea931b89mr175502595e9.8.1715844027131; Thu, 16 May 2024 00:20:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGUEr+g+ZxD2yERPRO+XyldEDV6ziNshcEP81dAYKveigt3wsfp2GZON3XeJaysoT6zPY3heg== X-Received: by 2002:a05:600c:4e8c:b0:419:f241:633b with SMTP id 5b1f17b1804b1-41fea931b89mr175502355e9.8.1715844026763; Thu, 16 May 2024 00:20:26 -0700 (PDT) Received: from positronik4lide.. ([87.122.57.114]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-41f9bc3b12fsm151059505e9.0.2024.05.16.00.20.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 00:20:26 -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: Thu, 16 May 2024 09:20:14 +0200 Message-Id: <20240516072015.18455-6-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240516072015.18455-1-jmeng@redhat.com> References: <20240516072015.18455-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v10 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 | 44 +++++++++++++++++++++++++++++-------------- tests/ovs-vswitchd.at | 13 +++++++++++++ 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/NEWS b/NEWS index 0d41061d3..479310d49 100644 --- a/NEWS +++ b/NEWS @@ -4,6 +4,7 @@ Post-v3.3.0 * 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. + * 'list-commands' now supports output in JSON format. - Python: * Added support for different output formats like 'json' to appctl.py and Python's unixctl classes. diff --git a/lib/unixctl.c b/lib/unixctl.c index c430eac0b..c84ca125f 100644 --- a/lib/unixctl.c +++ b/lib/unixctl.c @@ -107,24 +107,40 @@ 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) == UNIXCTL_OUTPUT_FMT_JSON) { + struct json *json_commands = json_object_create(); + 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) { + json_object_put_string(json_commands, node->name, + command->usage); + } } - } - 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 2db8138f1..4f909603a 100644 --- a/tests/ovs-vswitchd.at +++ b/tests/ovs-vswitchd.at @@ -281,3 +281,16 @@ 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]) +# Check that ovs-appctl prints a single line without a trailing newline. +AT_CHECK([wc -l stdout], [0], [0 stdout +]) +# Check that ovs-appctl prints text which roughly looks like a JSON object. +AT_CHECK([grep -E '^\{"autoattach/show-isid":.*\}$' stdout], [0], [ignore]) + +AT_CLEANUP