Skip to content

Commit 9eb886c

Browse files
committed
nvme-print: add show_key_value print_ops
This is to output a command result as json key and value. Signed-off-by: Tokunori Ikegami <[email protected]>
1 parent 21516ec commit 9eb886c

5 files changed

Lines changed: 47 additions & 0 deletions

File tree

nvme-print-binary.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ static struct print_ops binary_print_ops = {
429429
.show_perror = NULL,
430430
.show_status = NULL,
431431
.show_error_status = NULL,
432+
.show_key_value = NULL,
432433
};
433434

434435
struct print_ops *nvme_get_binary_print_ops(nvme_print_flags_t flags)

nvme-print-json.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4640,6 +4640,21 @@ static void json_output_perror(const char *msg)
46404640
json_output_object(r);
46414641
}
46424642

4643+
static void json_output_key_value(bool space, bool eol, const char *key, const char *val,
4644+
va_list ap)
4645+
{
4646+
struct json_object *r = json_r ? json_r : json_create_object();
4647+
4648+
_cleanup_free_ char *value = NULL;
4649+
4650+
if (vasprintf(&value, val, ap) < 0)
4651+
value = NULL;
4652+
4653+
obj_add_str(r, key, value ? value : "Could not allocate string");
4654+
4655+
obj_print(r);
4656+
}
4657+
46434658
void json_show_init(void)
46444659
{
46454660
json_r = json_create_object();
@@ -4992,6 +5007,7 @@ static struct print_ops json_print_ops = {
49925007
.show_perror = json_output_perror,
49935008
.show_status = json_output_status,
49945009
.show_error_status = json_output_error_status,
5010+
.show_key_value = json_output_key_value,
49955011
};
49965012

49975013
struct print_ops *nvme_get_json_print_ops(nvme_print_flags_t flags)

nvme-print-stdout.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5589,6 +5589,17 @@ static void stdout_perror(const char *msg)
55895589
perror(msg);
55905590
}
55915591

5592+
static void stdout_key_value(bool space, bool eol, const char *key, const char *val, va_list ap)
5593+
{
5594+
_cleanup_free_ char *value = NULL;
5595+
5596+
if (vasprintf(&value, val, ap) < 0)
5597+
value = NULL;
5598+
5599+
printf("%s:%s%s%s", key, space ? " " : "", value ? value : "Could not allocate string",
5600+
eol ? "\n" : " ");
5601+
}
5602+
55925603
static void stdout_discovery_log(struct nvmf_discovery_log *log, int numrec)
55935604
{
55945605
int i;
@@ -5924,6 +5935,7 @@ static struct print_ops stdout_print_ops = {
59245935
.show_perror = stdout_perror,
59255936
.show_status = stdout_status,
59265937
.show_error_status = stdout_error_status,
5938+
.show_key_value = stdout_key_value,
59275939
};
59285940

59295941
struct print_ops *nvme_get_stdout_print_ops(nvme_print_flags_t flags)

nvme-print.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,22 @@ void nvme_show_perror(const char *msg)
14711471
ops->show_perror(msg);
14721472
}
14731473

1474+
void nvme_show_key_value(bool space, bool eol, const char *key, const char *val, ...)
1475+
{
1476+
struct print_ops *ops = nvme_print_ops(NORMAL);
1477+
va_list ap;
1478+
1479+
va_start(ap, val);
1480+
1481+
if (nvme_is_output_format_json())
1482+
ops = nvme_print_ops(JSON);
1483+
1484+
if (ops && ops->show_key_value)
1485+
ops->show_key_value(space, eol, key, val, ap);
1486+
1487+
va_end(ap);
1488+
}
1489+
14741490
void nvme_show_discovery_log(struct nvmf_discovery_log *log, uint64_t numrec,
14751491
nvme_print_flags_t flags)
14761492
{

nvme-print.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ struct print_ops {
110110
void (*show_perror)(const char *msg);
111111
void (*show_status)(int status);
112112
void (*show_error_status)(int status, const char *msg, va_list ap);
113+
void (*show_key_value)(bool space, bool eol, const char *key, const char *val, va_list ap);
113114

114115
nvme_print_flags_t flags;
115116
};
@@ -329,6 +330,7 @@ void nvme_show_perror(const char *msg);
329330
void nvme_show_error_status(int status, const char *msg, ...);
330331
void nvme_show_init(void);
331332
void nvme_show_finish(void);
333+
void nvme_show_key_value(bool space, bool eol, const char *key, const char *value, ...);
332334
bool nvme_is_fabrics_reg(int offset);
333335
bool nvme_is_fabrics_optional_reg(int offset);
334336
bool nvme_registers_cmbloc_support(__u32 cmbsz);

0 commit comments

Comments
 (0)