From patchwork Thu Apr 11 14:47:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1922616 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=bhGkSLv8; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::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 4VFjJK6mY1z1yYB for ; Fri, 12 Apr 2024 00:49:04 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 08B8680FB3; Thu, 11 Apr 2024 14:49:02 +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 px_T4uA8A79a; Thu, 11 Apr 2024 14:49:00 +0000 (UTC) X-Comment: SPF check N/A for local connections - client-ip=140.211.9.56; helo=lists.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver= DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org C443280F7E 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=bhGkSLv8 Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp1.osuosl.org (Postfix) with ESMTPS id C443280F7E; Thu, 11 Apr 2024 14:49:00 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id A3ADFC0077; Thu, 11 Apr 2024 14:49:00 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp4.osuosl.org (smtp4.osuosl.org [140.211.166.137]) by lists.linuxfoundation.org (Postfix) with ESMTP id A2E1EC0037 for ; Thu, 11 Apr 2024 14:48:58 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id E9A7B41F67 for ; Thu, 11 Apr 2024 14:48:07 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp4.osuosl.org ([127.0.0.1]) by localhost (smtp4.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id z8a2ZcfBrpWr for ; Thu, 11 Apr 2024 14:48:06 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=170.10.133.124; helo=us-smtp-delivery-124.mimecast.com; envelope-from=jmeng@redhat.com; receiver= DMARC-Filter: OpenDMARC Filter v1.4.2 smtp4.osuosl.org 12B6341F46 Authentication-Results: smtp4.osuosl.org; dmarc=pass (p=none dis=none) header.from=redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp4.osuosl.org 12B6341F46 Authentication-Results: smtp4.osuosl.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=bhGkSLv8 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by smtp4.osuosl.org (Postfix) with ESMTPS id 12B6341F46 for ; Thu, 11 Apr 2024 14:48:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712846884; 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=iXPCsmFDwYfu1E99ZDI4H1ahYkCGElTk2SFuUGhprd8=; b=bhGkSLv84sR8yrsWPsWVq/ShX3xIkTDXyGOOQDUQ47gExO8r7q9xrUy+qEOBaKSyS+NoHB gyA0DL9WNkva1E43o5CFiJw11FCG5aajmFDKfFGBhzriN3S1XqXpxyMVgyDgJhoekZcfO1 agjXSFgZPnwd1/LG1mkLVhvOxKBlt/s= 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-460-rhLQULrfMPqHe-irsg4SRA-1; Thu, 11 Apr 2024 10:47:53 -0400 X-MC-Unique: rhLQULrfMPqHe-irsg4SRA-1 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-a467a6d4e3eso382865666b.3 for ; Thu, 11 Apr 2024 07:47:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712846855; x=1713451655; 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=iXPCsmFDwYfu1E99ZDI4H1ahYkCGElTk2SFuUGhprd8=; b=oAg23d0LpTSDMlQriflE7its1Jh6aOJSV7ckKjGpgwgKwzYl3T+JkTOw8WYizhE+Wu UA8qrCaJmcTuB3HQhgRCllSOL+9HcSQ/4xSqb4y/0Ab5tntVGn7mz1zF9cgIj6wq3xEe zjoovvqo2pyV9VQGnIc/WZxtM201Jy6pAqcxwswTlZWtNW08cT1rUkCtMuwwQEGF65hP uWnh2mmRLMLShAOt83jLCnuj13/EVQ2f+6oB4+M2W2gY768P+OFnYGe8RHmLOiCVFt43 5NY83Yfd3Rl58WaUhoU5IB02hRfGmtKtwOhpdruj/TVcRG3THBxDIuV7DY/kvxj0WBQW 1SXA== X-Gm-Message-State: AOJu0Yw5Z6dRU13ncmDh5GED+PdB98loGWvFpq/J3wfzBXng0NFhgiag cIxt1MPCx/tcPL1f3NFmS/o8jlzP4iqdi+sg5pUrkLjz0aBmTqlthw85o4nrE0kpTdZmHFj+OA3 Pgt66sGkuJCZYZN2kdCohOzMMLjC+g/Jqu2ftv2kUIhcqzlUfaoLvHsLWWJemmGVuiZRnkD3oTa uM9TnfqYt5NfXdEnQ1RqaeWckckwl8 X-Received: by 2002:a17:906:f44:b0:a47:5182:3b83 with SMTP id h4-20020a1709060f4400b00a4751823b83mr3089149ejj.61.1712846855194; Thu, 11 Apr 2024 07:47:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFo0RYitTXXnZl8hDKYMckhXmOaonVTZbUOObvyybeTKJDLAVUI4K3NnZRtRxQyPsqGJPhBKQ== X-Received: by 2002:a17:906:f44:b0:a47:5182:3b83 with SMTP id h4-20020a1709060f4400b00a4751823b83mr3089132ejj.61.1712846854859; Thu, 11 Apr 2024 07:47:34 -0700 (PDT) Received: from positronik4lide.. ([87.122.56.49]) by smtp.gmail.com with ESMTPSA id lo17-20020a170906fa1100b00a466af74ef2sm816835ejb.2.2024.04.11.07.47.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 07:47:34 -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, 11 Apr 2024 16:47:17 +0200 Message-Id: <20240411144718.1921869-6-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240411144718.1921869-1-jmeng@redhat.com> References: <20240411144718.1921869-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v8 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 4bb6bbe7f..3c611a21d 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