From: Stephen Hemminger <stephen@networkplumber.org>
To: netdev@vger.kernel.org
Cc: Stephen Hemminger <sthemmin@microsoft.com>,
Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH v2 iproute2-next 1/5] bridge: implement json pretty print flag
Date: Tue, 20 Feb 2018 11:24:04 -0800 [thread overview]
Message-ID: <20180220192408.19763-2-stephen@networkplumber.org> (raw)
In-Reply-To: <20180220192408.19763-1-stephen@networkplumber.org>
From: Stephen Hemminger <sthemmin@microsoft.com>
Make bridge work like other iproute2 commands and accept
same json and pretty flags.
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
bridge/br_common.h | 2 +-
bridge/bridge.c | 8 +++++---
bridge/fdb.c | 10 ++++++----
bridge/mdb.c | 12 ++++++++++--
bridge/vlan.c | 11 +++++++----
man/man8/bridge.8 | 6 ++++++
6 files changed, 35 insertions(+), 14 deletions(-)
diff --git a/bridge/br_common.h b/bridge/br_common.h
index f07c7d1c9090..b25f61e50e05 100644
--- a/bridge/br_common.h
+++ b/bridge/br_common.h
@@ -26,5 +26,5 @@ extern int show_stats;
extern int show_details;
extern int timestamp;
extern int compress_vlans;
-extern int json_output;
+extern int json;
extern struct rtnl_handle rth;
diff --git a/bridge/bridge.c b/bridge/bridge.c
index 48fe1c8b2ef1..4b112e3b8da9 100644
--- a/bridge/bridge.c
+++ b/bridge/bridge.c
@@ -23,7 +23,7 @@ int oneline;
int show_stats;
int show_details;
int compress_vlans;
-int json_output;
+int json;
int timestamp;
char *batch_file;
int force;
@@ -39,7 +39,7 @@ static void usage(void)
"where OBJECT := { link | fdb | mdb | vlan | monitor }\n"
" OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] |\n"
" -o[neline] | -t[imestamp] | -n[etns] name |\n"
-" -c[ompressvlans] -j{son} }\n");
+" -c[ompressvlans] -p[retty] -j{son} }\n");
exit(-1);
}
@@ -175,7 +175,9 @@ main(int argc, char **argv)
} else if (matches(opt, "-force") == 0) {
++force;
} else if (matches(opt, "-json") == 0) {
- ++json_output;
+ ++json;
+ } else if (matches(opt, "-pretty") == 0) {
+ ++pretty;
} else if (matches(opt, "-batch") == 0) {
argc--;
argv++;
diff --git a/bridge/fdb.c b/bridge/fdb.c
index 8b133f9c1c8d..93b5b2e694e3 100644
--- a/bridge/fdb.c
+++ b/bridge/fdb.c
@@ -132,10 +132,8 @@ int print_fdb(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
if (filter_vlan && filter_vlan != vid)
return 0;
- if (jw_global) {
- jsonw_pretty(jw_global, 1);
+ if (jw_global)
jsonw_start_object(jw_global);
- }
if (n->nlmsg_type == RTM_DELNEIGH) {
if (jw_global)
@@ -388,14 +386,18 @@ static int fdb_show(int argc, char **argv)
exit(1);
}
- if (json_output) {
+ if (json) {
jw_global = jsonw_new(stdout);
if (!jw_global) {
fprintf(stderr, "Error allocation json object\n");
exit(1);
}
+ if (pretty)
+ jsonw_pretty(jw_global, 1);
+
jsonw_start_array(jw_global);
}
+
if (rtnl_dump_filter(&rth, print_fdb, stdout) < 0) {
fprintf(stderr, "Dump terminated\n");
exit(1);
diff --git a/bridge/mdb.c b/bridge/mdb.c
index 62dc8a0c58c3..da0282fdc91c 100644
--- a/bridge/mdb.c
+++ b/bridge/mdb.c
@@ -325,7 +325,7 @@ static int mdb_show(int argc, char **argv)
return -1;
}
- if (!json_output) {
+ if (!json) {
/* Normal output */
if (rtnl_dump_filter(&rth, print_mdb, stdout) < 0) {
fprintf(stderr, "Dump terminated\n");
@@ -333,9 +333,17 @@ static int mdb_show(int argc, char **argv)
}
return 0;
}
+
/* Json output */
jw_global = jsonw_new(stdout);
- jsonw_pretty(jw_global, 1);
+ if (!jw_global) {
+ fprintf(stderr, "Error allocation json object\n");
+ exit(1);
+ }
+
+ if (pretty)
+ jsonw_pretty(jw_global, 1);
+
jsonw_start_object(jw_global);
jsonw_name(jw_global, "mdb");
jsonw_start_array(jw_global);
diff --git a/bridge/vlan.c b/bridge/vlan.c
index f42d7e6ba244..7c8b3ad54857 100644
--- a/bridge/vlan.c
+++ b/bridge/vlan.c
@@ -258,7 +258,6 @@ static int filter_vlan_check(__u16 vid, __u16 flags)
static void print_vlan_port(FILE *fp, int ifi_index)
{
if (jw_global) {
- jsonw_pretty(jw_global, 1);
jsonw_name(jw_global,
ll_index_to_name(ifi_index));
jsonw_start_array(jw_global);
@@ -578,17 +577,21 @@ static int vlan_show(int argc, char **argv)
if (!show_stats) {
if (rtnl_wilddump_req_filter(&rth, PF_BRIDGE, RTM_GETLINK,
(compress_vlans ?
- RTEXT_FILTER_BRVLAN_COMPRESSED :
- RTEXT_FILTER_BRVLAN)) < 0) {
+ RTEXT_FILTER_BRVLAN_COMPRESSED :
+ RTEXT_FILTER_BRVLAN)) < 0) {
perror("Cannont send dump request");
exit(1);
}
- if (json_output) {
+
+ if (json) {
jw_global = jsonw_new(stdout);
if (!jw_global) {
fprintf(stderr, "Error allocation json object\n");
exit(1);
}
+ if (pretty)
+ jsonw_pretty(jw_global, 1);
+
jsonw_start_object(jw_global);
} else {
if (show_vlan_tunnel_info)
diff --git a/man/man8/bridge.8 b/man/man8/bridge.8
index d6baa819020a..490ef58da16c 100644
--- a/man/man8/bridge.8
+++ b/man/man8/bridge.8
@@ -22,6 +22,7 @@ bridge \- show / manipulate bridge addresses and devices
\fB\-s\fR[\fItatistics\fR] |
\fB\-n\fR[\fIetns\fR] name |
\fB\-b\fR[\fIatch\fR] filename |
+\fB\-p\fR[\fIretty\fR] |
\fB\-j\fR[\fIson\fR] }
.ti -8
@@ -171,6 +172,11 @@ return code will be non zero.
.BR "\-json"
Display results in JSON format. Currently available for vlan and fdb.
+.TP
+.BR "\-pretty"
+When combined with -j generate a pretty JSON output.
+
+
.SH BRIDGE - COMMAND SYNTAX
.SS
--
2.16.1
next prev parent reply other threads:[~2018-02-20 19:24 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-20 19:24 [PATCH v2 iproute2-next 0/5] bridge: json and color support Stephen Hemminger
2018-02-20 19:24 ` Stephen Hemminger [this message]
2018-02-20 19:24 ` [PATCH v2 iproute2-next 2/5] bridge: colorize output and use JSON print library Stephen Hemminger
2018-07-15 1:41 ` Roopa Prabhu
2018-08-29 1:17 ` Roopa Prabhu
2018-08-29 15:04 ` Stephen Hemminger
2018-02-20 19:24 ` [PATCH v2 iproute2-next 3/5] bridge: add json support for link command Stephen Hemminger
2018-02-20 19:24 ` [PATCH v2 iproute2-next 4/5] bridge: update man page for new color and json changes Stephen Hemminger
2018-02-20 19:24 ` [PATCH v2 iproute2-next 5/5] ip: always print interface name in color Stephen Hemminger
2018-02-21 16:48 ` [PATCH v2 iproute2-next 0/5] bridge: json and color support David Ahern
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180220192408.19763-2-stephen@networkplumber.org \
--to=stephen@networkplumber.org \
--cc=netdev@vger.kernel.org \
--cc=sthemmin@microsoft.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.