From patchwork Thu Nov 16 10:41:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1864683 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=UrSGM+l0; 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 4SWGn90vT1z1yRR for ; Thu, 16 Nov 2023 21:42:05 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 96290833DB; Thu, 16 Nov 2023 10:42:01 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org 96290833DB 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=UrSGM+l0 X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id irOaTV3T74sp; Thu, 16 Nov 2023 10:41:59 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp1.osuosl.org (Postfix) with ESMTPS id C5A558330B; Thu, 16 Nov 2023 10:41:55 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org C5A558330B Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 8883FC0DE4; Thu, 16 Nov 2023 10:41:51 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp4.osuosl.org (smtp4.osuosl.org [IPv6:2605:bc80:3010::137]) by lists.linuxfoundation.org (Postfix) with ESMTP id EB265C0DDA for ; Thu, 16 Nov 2023 10:41:49 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id B889B42202 for ; Thu, 16 Nov 2023 10:41:49 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp4.osuosl.org B889B42202 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=UrSGM+l0 X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp4.osuosl.org ([127.0.0.1]) by localhost (smtp4.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PwQpRjgpwG2j for ; Thu, 16 Nov 2023 10:41:49 +0000 (UTC) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by smtp4.osuosl.org (Postfix) with ESMTPS id CCC1441BDD for ; Thu, 16 Nov 2023 10:41:48 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp4.osuosl.org CCC1441BDD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700131307; 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=gG3KqaBNj1bPWp8hgOfMJ1oxOY+1zYnkjfejuLoexck=; b=UrSGM+l07QkQIdEYdrfNebjqVesayqeK67bZ6AiTO5s06GhtJpUetMbgqKJVAKC6kmXPU/ EC1UetW+uWs0lCvlPSesFjHybX/k9nLxgSHRSU7kypfxbpDRU0iZ/9nmFH/zib0+CMNAD2 ICPtcEzWg1uQ/dLL5nw6TOBhDCpfirE= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-607-w898wKbRNsGR1fm0AXn53g-1; Thu, 16 Nov 2023 05:41:46 -0500 X-MC-Unique: w898wKbRNsGR1fm0AXn53g-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-40a5290e259so3425715e9.2 for ; Thu, 16 Nov 2023 02:41:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700131305; x=1700736105; 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=gG3KqaBNj1bPWp8hgOfMJ1oxOY+1zYnkjfejuLoexck=; b=GTXJ4QT/xKdhNsdP4qsqznE3u70Xws/4jUyFD0jeBZ/yzsU0xaqtr1LFPaCnSn7w75 6FeICpg6MEfw9fxm2HjFsgPPnCev1yj5+SdivIs1h47wqpzu4WppqScyheeSGHPbIoO7 7Dy9qDrxWzB3Xs72wuhvIOwUF79AIYyPdi15JkIL9kgGt7Lmpk3Ce0YYN8pxocCPUB7v Zdb1Lmo7EJ7SCJKo4OHeh5c3KSyJSpE/kora+MUfFMcx8IQ3uOcVpR83MrEjqMvxI6lk IjrXwUAi3Uo0Uok33rnTVcZ19dumZ4IRc2C6ByRx4h9l8Vi33VeBUP9L/SWHKdR4nCMv uDwg== X-Gm-Message-State: AOJu0Yw2j+A/IhvrH85UGjhYH+cIP0kLlzVcgdHdBTNA1tEjr4LVq4hj Md5grHdJuc6sO6X7n4oAYDzep/LW7kQ4LEE4NMxwgV6GIXqrt8fq54tO0TkSmqt8Ul7zUlI8bjR w4pYir1ZN3k+HxFI7Ow3/R42qHVr/GaA6++y5jcXx2w75yhozK9WnHmUnHbEVcepE2iA= X-Received: by 2002:a05:600c:202:b0:404:4b6f:d705 with SMTP id 2-20020a05600c020200b004044b6fd705mr13020886wmi.17.1700131305071; Thu, 16 Nov 2023 02:41:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IF6GdnJOAfHruFvm9oKNNFGSPwCfJ56SP/qczlSpFOEiPy5m/LsogkrjcNEQUfo8ayb6Ns1Dg== X-Received: by 2002:a05:600c:202:b0:404:4b6f:d705 with SMTP id 2-20020a05600c020200b004044b6fd705mr13020862wmi.17.1700131304589; Thu, 16 Nov 2023 02:41:44 -0800 (PST) Received: from positronik4lide.redhat.com ([87.122.56.153]) by smtp.gmail.com with ESMTPSA id n10-20020a05600c304a00b004080f0376a0sm2937210wmh.42.2023.11.16.02.41.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Nov 2023 02:41:44 -0800 (PST) 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 Nov 2023 11:41:19 +0100 Message-Id: <20231116104120.25676-6-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231116104120.25676-1-jmeng@redhat.com> References: <20231116104120.25676-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v4 5/6] appctl: Add option '--pretty' for pretty-printing JSON output. 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 Signed-off-by: Jakob Meng --- NEWS | 3 +++ lib/unixctl.c | 4 ++-- lib/unixctl.h | 2 +- tests/pmd.at | 2 +- utilities/ovs-appctl.c | 19 +++++++++++++++++-- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/NEWS b/NEWS index fc77a1613..1796895a7 100644 --- a/NEWS +++ b/NEWS @@ -15,6 +15,9 @@ Post-v3.2.0 Reported names adjusted accordingly. * 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. + E.g. members of objects and elements of arrays are printed one per line, + with indentation. - Python: * Added support for choosing the output format, e.g. 'json' or 'text'. diff --git a/lib/unixctl.c b/lib/unixctl.c index c82a5e92d..e7edbb154 100644 --- a/lib/unixctl.c +++ b/lib/unixctl.c @@ -553,7 +553,7 @@ unixctl_client_create(const char *path, struct jsonrpc **client) * '*err' if not NULL. */ int unixctl_client_transact(struct jsonrpc *client, const char *command, int argc, - char *argv[], enum ovs_output_fmt fmt, + char *argv[], enum ovs_output_fmt fmt, int fmt_flags, char **result, char **err) { struct jsonrpc_msg *request, *reply; @@ -622,7 +622,7 @@ unixctl_client_transact(struct jsonrpc *client, const char *command, int argc, *result = xstrdup(json_string(reply->result)); } else if (reply->result->type == JSON_OBJECT || reply->result->type == JSON_ARRAY) { - *result = json_to_string(reply->result, 0); + *result = json_to_string(reply->result, fmt_flags); } else { VLOG_WARN("%s: unexpected result type in JSON rpc reply: %s", jsonrpc_get_name(client), diff --git a/lib/unixctl.h b/lib/unixctl.h index 4b8193d9d..33d0152e5 100644 --- a/lib/unixctl.h +++ b/lib/unixctl.h @@ -39,7 +39,7 @@ int unixctl_client_create(const char *path, struct jsonrpc **client); int unixctl_client_transact(struct jsonrpc *client, const char *command, int argc, char *argv[], - enum ovs_output_fmt fmt, + enum ovs_output_fmt fmt, int fmt_flags, char **result, char **error); /* Command registration. */ diff --git a/tests/pmd.at b/tests/pmd.at index de2dc0974..21d59bf63 100644 --- a/tests/pmd.at +++ b/tests/pmd.at @@ -105,7 +105,7 @@ dummy@ovs-dummy: hit:0 missed:0 p0 1/1: (dummy-pmd: n_rxq=1, n_txq=1, numa_id=0) ]) -AT_CHECK([ovs-appctl --format json dpif/show], [0], [dnl +AT_CHECK([ovs-appctl --format json --pretty dpif/show], [0], [dnl [[ { "name": "dummy@ovs-dummy", diff --git a/utilities/ovs-appctl.c b/utilities/ovs-appctl.c index feff1be0b..606f293c5 100644 --- a/utilities/ovs-appctl.c +++ b/utilities/ovs-appctl.c @@ -26,6 +26,7 @@ #include "daemon.h" #include "dirs.h" #include "openvswitch/dynamic-string.h" +#include "openvswitch/json.h" #include "jsonrpc.h" #include "process.h" #include "timeval.h" @@ -38,6 +39,7 @@ static void usage(void); /* Parsed command line args. */ struct cmdl_args { enum ovs_output_fmt format; + int format_flags; char *target; }; @@ -67,7 +69,8 @@ main(int argc, char *argv[]) cmd_argc = argc - optind; cmd_argv = cmd_argc ? argv + optind : NULL; error = unixctl_client_transact(client, cmd, cmd_argc, cmd_argv, - args->format, &cmd_result, &cmd_error); + args->format, args->format_flags, + &cmd_result, &cmd_error); if (error) { ovs_fatal(error, "%s: transaction error", args->target); } @@ -113,6 +116,11 @@ Other options:\n\ --timeout=SECS wait at most SECS seconds for a response\n\ -f, --format=FMT Output format. One of: 'json', or 'text'\n\ ('text', by default)\n\ + --pretty By default, JSON in output is printed as compactly as\n\ + possible. This option causes JSON in output to be\n\ + printed in a more readable fashion. Members of objects\n\ + and elements of arrays are printed one per line, with\n\ + indentation.\n\ -h, --help Print this helpful information\n\ -V, --version Display ovs-appctl version information\n", program_name, program_name); @@ -124,6 +132,7 @@ cmdl_args_create(void) { struct cmdl_args *args = xmalloc(sizeof *args); args->format = OVS_OUTPUT_FMT_TEXT; + args->format_flags = 0; args->target = NULL; return args; @@ -143,7 +152,8 @@ parse_command_line(int argc, char *argv[]) { enum { OPT_START = UCHAR_MAX + 1, - VLOG_OPTION_ENUMS + OPT_PRETTY, + VLOG_OPTION_ENUMS, }; static const struct option long_options[] = { {"target", required_argument, NULL, 't'}, @@ -151,6 +161,7 @@ parse_command_line(int argc, char *argv[]) {"format", required_argument, NULL, 'f'}, {"help", no_argument, NULL, 'h'}, {"option", no_argument, NULL, 'o'}, + {"pretty", no_argument, NULL, OPT_PRETTY}, {"version", no_argument, NULL, 'V'}, {"timeout", required_argument, NULL, 'T'}, VLOG_LONG_OPTIONS, @@ -203,6 +214,10 @@ parse_command_line(int argc, char *argv[]) ovs_cmdl_print_options(long_options); exit(EXIT_SUCCESS); + case OPT_PRETTY: + args->format_flags |= JSSF_PRETTY | JSSF_SORT; + break; + case 'T': if (!str_to_uint(optarg, 10, &timeout) || !timeout) { ovs_fatal(0, "value %s on -T or --timeout is invalid", optarg);