mirror of
https://git.proxmox.com/git/mirror_iproute2
synced 2025-08-08 05:30:55 +00:00
tipc: JSON support for showing nametable
Add json output support for nametable show Example output: $tipc -j -p nametable show [ { "type": 0, "lower": 16781313, "upper": 16781313, "scope": "zone", "port": 0, "node": "" },{ "type": 0, "lower": 16781416, "upper": 16781416, "scope": "cluster", "port": 0, "node": "" } ] v2: Replace variable 'json_flag' by 'json' declared in include/utils.h Add new parameter '-pretty' to support pretty output v3: Update manual page Acked-by: Jon Maloy <jon.maloy@ericsson.com> Signed-off-by: Hoang Le <hoang.h.le@dektech.com.au> Signed-off-by: David Ahern <dsahern@gmail.com>
This commit is contained in:
parent
ee095a417e
commit
1304f50a5b
@ -18,6 +18,16 @@ tipc-nametable \- show TIPC nametable
|
|||||||
Options (flags) that can be passed anywhere in the command chain.
|
Options (flags) that can be passed anywhere in the command chain.
|
||||||
.TP
|
.TP
|
||||||
.BR "\-h" , " --help"
|
.BR "\-h" , " --help"
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.BR "\-j", " \-json"
|
||||||
|
Output results in JavaScript Object Notation (JSON).
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.BR "\-p", " \-pretty"
|
||||||
|
The default JSON format is compact and more efficient to parse but hard for most users to read.
|
||||||
|
This flag adds indentation for readability.
|
||||||
|
|
||||||
Show help about last valid command. For example
|
Show help about last valid command. For example
|
||||||
.B tipc nametable --help
|
.B tipc nametable --help
|
||||||
will show nametable help and
|
will show nametable help and
|
||||||
|
@ -40,6 +40,15 @@ will show bearer help and
|
|||||||
.B tipc --help
|
.B tipc --help
|
||||||
will show general help. The position of the option in the string is irrelevant.
|
will show general help. The position of the option in the string is irrelevant.
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.BR "\-j", " \-json"
|
||||||
|
Output results in JavaScript Object Notation (JSON).
|
||||||
|
|
||||||
|
.TP
|
||||||
|
.BR "\-p", " \-pretty"
|
||||||
|
The default JSON format is compact and more efficient to parse but hard for most users to read.
|
||||||
|
This flag adds indentation for readability.
|
||||||
|
|
||||||
.SH COMMANDS
|
.SH COMMANDS
|
||||||
|
|
||||||
.TP
|
.TP
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
#include "msg.h"
|
#include "msg.h"
|
||||||
#include "nametable.h"
|
#include "nametable.h"
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
#define PORTID_STR_LEN 45 /* Four u32 and five delimiter chars */
|
#define PORTID_STR_LEN 45 /* Four u32 and five delimiter chars */
|
||||||
|
|
||||||
@ -46,7 +47,7 @@ static int nametable_show_cb(const struct nlmsghdr *nlh, void *data)
|
|||||||
if (!publ[TIPC_NLA_NAME_TABLE_PUBL])
|
if (!publ[TIPC_NLA_NAME_TABLE_PUBL])
|
||||||
return MNL_CB_ERROR;
|
return MNL_CB_ERROR;
|
||||||
|
|
||||||
if (!*iteration)
|
if (!*iteration && !is_json_context())
|
||||||
printf("%-10s %-10s %-10s %-8s %-10s %-33s\n",
|
printf("%-10s %-10s %-10s %-8s %-10s %-33s\n",
|
||||||
"Type", "Lower", "Upper", "Scope", "Port",
|
"Type", "Lower", "Upper", "Scope", "Port",
|
||||||
"Node");
|
"Node");
|
||||||
@ -54,13 +55,20 @@ static int nametable_show_cb(const struct nlmsghdr *nlh, void *data)
|
|||||||
|
|
||||||
hash2nodestr(mnl_attr_get_u32(publ[TIPC_NLA_PUBL_NODE]), str);
|
hash2nodestr(mnl_attr_get_u32(publ[TIPC_NLA_PUBL_NODE]), str);
|
||||||
|
|
||||||
printf("%-10u %-10u %-10u %-8s %-10u %s\n",
|
open_json_object(NULL);
|
||||||
mnl_attr_get_u32(publ[TIPC_NLA_PUBL_TYPE]),
|
print_uint(PRINT_ANY, "type", "%-10u",
|
||||||
mnl_attr_get_u32(publ[TIPC_NLA_PUBL_LOWER]),
|
mnl_attr_get_u32(publ[TIPC_NLA_PUBL_TYPE]));
|
||||||
mnl_attr_get_u32(publ[TIPC_NLA_PUBL_UPPER]),
|
print_uint(PRINT_ANY, "lower", "%-10u",
|
||||||
scope[mnl_attr_get_u32(publ[TIPC_NLA_PUBL_SCOPE])],
|
mnl_attr_get_u32(publ[TIPC_NLA_PUBL_LOWER]));
|
||||||
mnl_attr_get_u32(publ[TIPC_NLA_PUBL_REF]),
|
print_uint(PRINT_ANY, "upper", "%-10u",
|
||||||
str);
|
mnl_attr_get_u32(publ[TIPC_NLA_PUBL_UPPER]));
|
||||||
|
print_string(PRINT_ANY, "scope", "%-8s",
|
||||||
|
scope[mnl_attr_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]);
|
||||||
|
print_uint(PRINT_ANY, "port", "%-10u",
|
||||||
|
mnl_attr_get_u32(publ[TIPC_NLA_PUBL_REF]));
|
||||||
|
print_string(PRINT_ANY, "node", "%s", str);
|
||||||
|
print_string(PRINT_FP, NULL, "\n", "");
|
||||||
|
close_json_object();
|
||||||
|
|
||||||
return MNL_CB_OK;
|
return MNL_CB_OK;
|
||||||
}
|
}
|
||||||
@ -70,6 +78,7 @@ static int cmd_nametable_show(struct nlmsghdr *nlh, const struct cmd *cmd,
|
|||||||
{
|
{
|
||||||
int iteration = 0;
|
int iteration = 0;
|
||||||
char buf[MNL_SOCKET_BUFFER_SIZE];
|
char buf[MNL_SOCKET_BUFFER_SIZE];
|
||||||
|
int rc = 0;
|
||||||
|
|
||||||
if (help_flag) {
|
if (help_flag) {
|
||||||
fprintf(stderr, "Usage: %s nametable show\n", cmdl->argv[0]);
|
fprintf(stderr, "Usage: %s nametable show\n", cmdl->argv[0]);
|
||||||
@ -81,7 +90,11 @@ static int cmd_nametable_show(struct nlmsghdr *nlh, const struct cmd *cmd,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return msg_dumpit(nlh, nametable_show_cb, &iteration);
|
new_json_obj(json);
|
||||||
|
rc = msg_dumpit(nlh, nametable_show_cb, &iteration);
|
||||||
|
delete_json_obj();
|
||||||
|
|
||||||
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmd_nametable_help(struct cmdl *cmdl)
|
void cmd_nametable_help(struct cmdl *cmdl)
|
||||||
|
20
tipc/tipc.c
20
tipc/tipc.c
@ -24,6 +24,8 @@
|
|||||||
#include "cmdl.h"
|
#include "cmdl.h"
|
||||||
|
|
||||||
int help_flag;
|
int help_flag;
|
||||||
|
int json;
|
||||||
|
int pretty;
|
||||||
|
|
||||||
static void about(struct cmdl *cmdl)
|
static void about(struct cmdl *cmdl)
|
||||||
{
|
{
|
||||||
@ -33,6 +35,8 @@ static void about(struct cmdl *cmdl)
|
|||||||
"\n"
|
"\n"
|
||||||
"Options:\n"
|
"Options:\n"
|
||||||
" -h, --help \t\tPrint help for last given command\n"
|
" -h, --help \t\tPrint help for last given command\n"
|
||||||
|
" -j, --json \t\tJson format printouts\n"
|
||||||
|
" -p, --pretty \t\tpretty print\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Commands:\n"
|
"Commands:\n"
|
||||||
" bearer - Show or modify bearers\n"
|
" bearer - Show or modify bearers\n"
|
||||||
@ -53,6 +57,8 @@ int main(int argc, char *argv[])
|
|||||||
const struct cmd cmd = {"tipc", NULL, about};
|
const struct cmd cmd = {"tipc", NULL, about};
|
||||||
struct option long_options[] = {
|
struct option long_options[] = {
|
||||||
{"help", no_argument, 0, 'h'},
|
{"help", no_argument, 0, 'h'},
|
||||||
|
{"json", no_argument, 0, 'j'},
|
||||||
|
{"pretty", no_argument, 0, 'p'},
|
||||||
{0, 0, 0, 0}
|
{0, 0, 0, 0}
|
||||||
};
|
};
|
||||||
const struct cmd cmds[] = {
|
const struct cmd cmds[] = {
|
||||||
@ -69,7 +75,7 @@ int main(int argc, char *argv[])
|
|||||||
do {
|
do {
|
||||||
int option_index = 0;
|
int option_index = 0;
|
||||||
|
|
||||||
i = getopt_long(argc, argv, "h", long_options, &option_index);
|
i = getopt_long(argc, argv, "hjp", long_options, &option_index);
|
||||||
|
|
||||||
switch (i) {
|
switch (i) {
|
||||||
case 'h':
|
case 'h':
|
||||||
@ -79,6 +85,18 @@ int main(int argc, char *argv[])
|
|||||||
*/
|
*/
|
||||||
help_flag = 1;
|
help_flag = 1;
|
||||||
break;
|
break;
|
||||||
|
case 'j':
|
||||||
|
/*
|
||||||
|
* Enable json format printouts
|
||||||
|
*/
|
||||||
|
json = 1;
|
||||||
|
break;
|
||||||
|
case 'p':
|
||||||
|
/*
|
||||||
|
* Enable json pretty output
|
||||||
|
*/
|
||||||
|
pretty = 1;
|
||||||
|
break;
|
||||||
case -1:
|
case -1:
|
||||||
/* End of options */
|
/* End of options */
|
||||||
break;
|
break;
|
||||||
|
Loading…
Reference in New Issue
Block a user