From patchwork Thu May 16 15:41:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1936055 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=habO/jRL; 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 4VgDq03VSRz1ymw for ; Fri, 17 May 2024 01:41:48 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id A99D883A67; Thu, 16 May 2024 15:41:45 +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 5qqL7WZpF1Yz; Thu, 16 May 2024 15:41:42 +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 6CE02837B9 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=habO/jRL Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp1.osuosl.org (Postfix) with ESMTPS id 6CE02837B9; Thu, 16 May 2024 15:41:38 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 12EEDC0DD9; Thu, 16 May 2024 15:41:38 +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 87F43C0DCF for ; Thu, 16 May 2024 15:41:33 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 71A3F41D2E for ; Thu, 16 May 2024 15:41:33 +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 m4JEF9rFEx0j for ; Thu, 16 May 2024 15:41:32 +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 smtp2.osuosl.org 35A2441D23 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 35A2441D23 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=habO/jRL Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by smtp2.osuosl.org (Postfix) with ESMTPS id 35A2441D23 for ; Thu, 16 May 2024 15:41:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715874091; 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=habO/jRLxFBDexmc6tzHzHonMzEomXQvCnfz4PMclHZRX8U/Vvapl68Ppgd7xk2tU7y02u uipccd60SvJaVFgT4F9dVcC3e4TAyyNAo8fVH3RmQ+1qLwMrkaJ7oQwgBIAon+hwTcRktA /UnleKCmIf1aZ7/iSe5wARgrVa7r/Hg= Received: from mail-lf1-f72.google.com (mail-lf1-f72.google.com [209.85.167.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-544-oo-rIzDfNFKonDdllfp_sA-1; Thu, 16 May 2024 11:41:30 -0400 X-MC-Unique: oo-rIzDfNFKonDdllfp_sA-1 Received: by mail-lf1-f72.google.com with SMTP id 2adb3069b0e04-51f6fb04b82so7296861e87.2 for ; Thu, 16 May 2024 08:41:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715874088; x=1716478888; 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=vY75utwygzGUWxqZb5fTBIYB+cJwdLQKK1Ctbdy6bOdLxhcm2f8qDETOcFphRFozlG 3NlnK0acjTUoCmoRfa8Wzx1PPorVgyWH08c6MXXHneOytqV1UKccrtXnR9D6RpoavN1u uUxRtjtLlKNOG6N8PDslqUw0fwT37hJ1GL4shDGgw+HZ9iu7/XHNNj4uaAjN31QGneHc 0X9DEVxexGqbWhAA6EP9tzWkq/uy/OkwmYvTmXajL6I81PfY5hMFs3dSrQs1ukgxz8Zx OIsK/iey+l3BOPJxiG7clo0WBhSN1tF/ATGE+t/17esxmkzAoMwZ8na6z/nvrF/t+FQi 6cyg== X-Gm-Message-State: AOJu0YyXEyp48eMzcDJg1rVjzQAZZ849vn0ZbmlTf1BzQu5b8ZhxcVMM D6J3ZPvUlK6dgxnbSP9OEzPDbOCeedatDrvLmiTN9oZOLS7eKn+5J/taN14Ze+IPTZwpfydxs8j 3TOyiw0+4VN5eGI/JVBmqdVUKTZdYebiZMShnf9TWrlOasZjH3y+/KdoUZvQRa8NbNMyoe5Q0nU asKFiKYtCkS2oFDjuTOI21nLE6eNty X-Received: by 2002:ac2:424d:0:b0:521:b2b5:60ea with SMTP id 2adb3069b0e04-5220fb6b131mr12344012e87.23.1715874088622; Thu, 16 May 2024 08:41:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFXPbJbJ1lm7qBVHXy9bB2C3ITZXXkrpwxB60h+5NZhLm01ayLLhsWiieUWXnBpl7gu3PB+Nw== X-Received: by 2002:ac2:424d:0:b0:521:b2b5:60ea with SMTP id 2adb3069b0e04-5220fb6b131mr12343940e87.23.1715874086289; Thu, 16 May 2024 08:41:26 -0700 (PDT) Received: from positronik4lide.redhat.com ([87.122.57.114]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3502bbbbf9esm19177266f8f.91.2024.05.16.08.41.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 08:41:26 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org Date: Thu, 16 May 2024 17:41:16 +0200 Message-Id: <20240516154117.2306789-6-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240516154117.2306789-1-jmeng@redhat.com> References: <20240516154117.2306789-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v12 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