From patchwork Thu May 16 10:19:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Meng X-Patchwork-Id: 1935928 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=hs9rvJv2; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.137; helo=smtp4.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp4.osuosl.org (smtp4.osuosl.org [140.211.166.137]) (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 4Vg5gk4tCsz1ymw for ; Thu, 16 May 2024 20:20:02 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id DE71A4139E; Thu, 16 May 2024 10:19:59 +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 U19xTCeVaIpi; Thu, 16 May 2024 10:19:58 +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 smtp4.osuosl.org B46E2409D3 Authentication-Results: smtp4.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=hs9rvJv2 Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp4.osuosl.org (Postfix) with ESMTPS id B46E2409D3; Thu, 16 May 2024 10:19:57 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 82DABC0DCF; Thu, 16 May 2024 10:19:57 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) by lists.linuxfoundation.org (Postfix) with ESMTP id A44E8C0037 for ; Thu, 16 May 2024 10:19:53 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 865C082531 for ; Thu, 16 May 2024 10:19:53 +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 cTR9y5FxFkfr for ; Thu, 16 May 2024 10:19:52 +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 91FB4824CB 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 91FB4824CB Authentication-Results: smtp1.osuosl.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=hs9rvJv2 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 91FB4824CB for ; Thu, 16 May 2024 10:19:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715854791; 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=tNrC/6nVYfgYLAH9jEKTogyQpvwqeisqFR9ieocCZfc=; b=hs9rvJv2iXolykTybab4S7n7DT4tXuDAbNLHbzhSrL6Hxq4nGdkJLWIxeuE9SFepf+WJK2 h0c1HBWNMMfNGeEmmQnP4OlaydOCwZgqUjwBdMmdH1h6/qnttkALS+r6PBHz+fiUcDL1vQ NBr3TFNah3QQkf6oVd4hjl6V6TNbosQ= 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-39-hS_XwCG5Pn29yv4ADAMgvQ-1; Thu, 16 May 2024 06:19:49 -0400 X-MC-Unique: hS_XwCG5Pn29yv4ADAMgvQ-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4200efb9ac6so29446335e9.2 for ; Thu, 16 May 2024 03:19:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715854788; x=1716459588; 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=tNrC/6nVYfgYLAH9jEKTogyQpvwqeisqFR9ieocCZfc=; b=mkFJo3WvNC+i+ouBsvnxeKHdZC+0rcQolvCuSZS2wcZarGR8/uPWRkZOEDE/3n2gXD YkbF0FNAx3eX+w4Ib1edsb/UNi+fXb3yK/xWhbr7cD08zKkmNZ4D4OaCPcCUn9kjkUls yDmW+/UIQmz9kfJC4U+s63jps3U8Nrlzkqko0DnN/LlSECWe1L/Xk0mhlz5DiQm+9eo/ jKnpmmdN+2MRS8ssD2dzxH4hhxqcXhMcfDyKpbuNMAk1sl78BtQf/2htNYK1NELYlINs i4wmHlZHfa8p7tjXnfLX28zwfk3G0tuu4/sBa4JuikmMf/T5FGVpK0zAXq0+L4XDvdun stOw== X-Gm-Message-State: AOJu0YytEqhp6WycqBBQNPRiKyXQzwkZ6e/EGb/g0rwCfcmP1aIX93RU lC+BncCb2BWXPeZAj4Dpz1ESwAw4oVrKmX6Bp8rFXyDp8TIpveP23YCRplb6GGSikmFQ7VuDB8g ctkd3UDIgo7OnvJFwAitXdTphbnqITkJyrOF7+hGwvrF1CSF04CK0jvjfPBGC0qNxAEbbJFq6uv PimdVTINtB7+yK40+V0BYGNse3uhLa X-Received: by 2002:a05:600c:3ca0:b0:41e:ae29:c807 with SMTP id 5b1f17b1804b1-41feac59cbfmr120880215e9.29.1715854788739; Thu, 16 May 2024 03:19:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IExNngxtajMCWFIpJPAYchPMo/2c74YVULaIZQUhN8fVef9Z3Pcf7eDDkbrAGyGyan4qPx2cg== X-Received: by 2002:a05:600c:3ca0:b0:41e:ae29:c807 with SMTP id 5b1f17b1804b1-41feac59cbfmr120880095e9.29.1715854788403; Thu, 16 May 2024 03:19:48 -0700 (PDT) Received: from positronik4lide.redhat.com ([87.122.57.114]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-41fccee934csm265507145e9.38.2024.05.16.03.19.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 03:19:48 -0700 (PDT) From: jmeng@redhat.com To: dev@openvswitch.org, i.maximets@ovn.org Date: Thu, 16 May 2024 12:19:36 +0200 Message-Id: <20240516101939.54900-4-jmeng@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240516101939.54900-1-jmeng@redhat.com> References: <20240516101939.54900-1-jmeng@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v11 3/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 With the '--pretty' option, ovs-appctl will now print JSON output in a more readable fashion, i.e. with additional line breaks, spaces and sorted dictionary keys. Signed-off-by: Jakob Meng --- Documentation/ref/ovs-appctl.8.rst | 7 ++++++ NEWS | 1 + tests/ovs-vswitchd.at | 5 +++++ utilities/ovs-appctl.c | 34 ++++++++++++++++++++++++------ 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/Documentation/ref/ovs-appctl.8.rst b/Documentation/ref/ovs-appctl.8.rst index 9619c1226..db6c52b42 100644 --- a/Documentation/ref/ovs-appctl.8.rst +++ b/Documentation/ref/ovs-appctl.8.rst @@ -79,6 +79,13 @@ In normal use only a single option is accepted: ``{"reply-format":"plain","reply":"$PLAIN_TEXT_HERE"}`` +* ``--pretty`` + + By default, JSON output is printed as compactly as possible. This option + causes JSON in output to be printed in a more readable fashion. For example, + members of objects and elements of arrays are printed one per line, with + indentation. Requires ``--format=json``. + Common Commands =============== diff --git a/NEWS b/NEWS index 7076939c5..0d41061d3 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,7 @@ Post-v3.3.0 - ovs-appctl: * 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. - Python: * Added support for different output formats like 'json' to appctl.py and Python's unixctl classes. diff --git a/tests/ovs-vswitchd.at b/tests/ovs-vswitchd.at index 1ae7fcc32..2db8138f1 100644 --- a/tests/ovs-vswitchd.at +++ b/tests/ovs-vswitchd.at @@ -275,4 +275,9 @@ ovs_version=$(ovs-appctl version) AT_CHECK_UNQUOTED([ovs-appctl --format json version], [0], [dnl {"reply":"$ovs_version","reply-format":"plain"}]) +AT_CHECK_UNQUOTED([ovs-appctl --format json --pretty version], [0], [dnl +{ + "reply": "$ovs_version", + "reply-format": "plain"}]) + AT_CLEANUP diff --git a/utilities/ovs-appctl.c b/utilities/ovs-appctl.c index a359a14f1..2ade64336 100644 --- a/utilities/ovs-appctl.c +++ b/utilities/ovs-appctl.c @@ -40,13 +40,15 @@ static void usage(void); /* Parsed command line args. */ struct cmdl_args { enum unixctl_output_fmt format; + unsigned int format_flags; char *target; }; static struct cmdl_args *cmdl_args_create(void); static struct cmdl_args *parse_command_line(int argc, char *argv[]); static struct jsonrpc *connect_to_target(const char *target); -static char * reply_to_string(struct json *reply, enum unixctl_output_fmt fmt); +static char * reply_to_string(struct json *reply, enum unixctl_output_fmt fmt, + unsigned int fmt_flags); int main(int argc, char *argv[]) @@ -84,7 +86,7 @@ main(int argc, char *argv[]) if (cmd_error) { jsonrpc_close(client); - msg = reply_to_string(cmd_error, UNIXCTL_OUTPUT_FMT_TEXT); + msg = reply_to_string(cmd_error, UNIXCTL_OUTPUT_FMT_TEXT, 0); fputs(msg, stderr); ovs_error(0, "%s: server returned an error", args->target); exit(2); @@ -107,12 +109,12 @@ main(int argc, char *argv[]) if (cmd_error) { jsonrpc_close(client); - msg = reply_to_string(cmd_error, UNIXCTL_OUTPUT_FMT_TEXT); + msg = reply_to_string(cmd_error, UNIXCTL_OUTPUT_FMT_TEXT, 0); fputs(msg, stderr); ovs_error(0, "%s: server returned an error", args->target); exit(2); } else if (cmd_result) { - msg = reply_to_string(cmd_result, args->format); + msg = reply_to_string(cmd_result, args->format, args->format_flags); fputs(msg, stdout); } else { OVS_NOT_REACHED(); @@ -149,6 +151,8 @@ 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\ (default: text)\n\ + --pretty Format the output in a more readable fashion.\n\ + Requires: --format=json.\n\ -h, --help Print this helpful information\n\ -V, --version Display ovs-appctl version information\n", program_name, program_name); @@ -161,6 +165,7 @@ cmdl_args_create(void) struct cmdl_args *args = xmalloc(sizeof *args); args->format = UNIXCTL_OUTPUT_FMT_TEXT; + args->format_flags = 0; args->target = NULL; return args; @@ -171,7 +176,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'}, @@ -179,6 +185,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, @@ -188,6 +195,7 @@ parse_command_line(int argc, char *argv[]) char *short_options = xasprintf("+%s", short_options_); struct cmdl_args *args = cmdl_args_create(); unsigned int timeout = 0; + bool pretty = false; int e_options; e_options = 0; @@ -230,6 +238,10 @@ parse_command_line(int argc, char *argv[]) ovs_cmdl_print_options(long_options); exit(EXIT_SUCCESS); + case OPT_PRETTY: + pretty = true; + break; + case 'T': if (!str_to_uint(optarg, 10, &timeout) || !timeout) { ovs_fatal(0, "value %s on -T or --timeout is invalid", optarg); @@ -259,6 +271,13 @@ parse_command_line(int argc, char *argv[]) "(use --help for help)"); } + if (pretty) { + if (args->format != UNIXCTL_OUTPUT_FMT_JSON) { + ovs_fatal(0, "--pretty is supported with --format json only"); + } + args->format_flags |= JSSF_PRETTY; + } + if (!args->target) { args->target = "ovs-vswitchd"; } @@ -307,7 +326,8 @@ connect_to_target(const char *target) /* The caller is responsible for freeing the returned string, with free(), when * it is no longer needed. */ static char * -reply_to_string(struct json *reply, enum unixctl_output_fmt fmt) +reply_to_string(struct json *reply, enum unixctl_output_fmt fmt, + unsigned int fmt_flags) { ovs_assert(reply); @@ -326,5 +346,5 @@ reply_to_string(struct json *reply, enum unixctl_output_fmt fmt) json_type_to_string(reply->type)); } - return json_to_string(reply, JSSF_SORT); + return json_to_string(reply, JSSF_SORT | fmt_flags); }