Skip to content

Commit 705f711

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 56cf51b commit 705f711

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
@@ -399,6 +399,7 @@ static struct print_ops binary_print_ops = {
399399
.show_perror = NULL,
400400
.show_status = NULL,
401401
.show_error_status = NULL,
402+
.show_key_value = NULL,
402403
};
403404

404405
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
@@ -4587,6 +4587,21 @@ static void json_output_perror(const char *msg)
45874587
json_output_object(r);
45884588
}
45894589

4590+
static void json_output_key_value(bool space, bool eol, const char *key, const char *val,
4591+
va_list ap)
4592+
{
4593+
struct json_object *r = json_r ? json_r : json_create_object();
4594+
4595+
_cleanup_free_ char *value = NULL;
4596+
4597+
if (vasprintf(&value, val, ap) < 0)
4598+
value = NULL;
4599+
4600+
obj_add_str(r, key, value ? value : "Could not allocate string");
4601+
4602+
obj_print(r);
4603+
}
4604+
45904605
void json_show_init(void)
45914606
{
45924607
json_r = json_create_object();
@@ -4736,6 +4751,7 @@ static struct print_ops json_print_ops = {
47364751
.show_perror = json_output_perror,
47374752
.show_status = json_output_status,
47384753
.show_error_status = json_output_error_status,
4754+
.show_key_value = json_output_key_value,
47394755
};
47404756

47414757
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
@@ -5491,6 +5491,17 @@ static void stdout_perror(const char *msg)
54915491
perror(msg);
54925492
}
54935493

5494+
static void stdout_key_value(bool space, bool eol, const char *key, const char *val, va_list ap)
5495+
{
5496+
_cleanup_free_ char *value = NULL;
5497+
5498+
if (vasprintf(&value, val, ap) < 0)
5499+
value = NULL;
5500+
5501+
printf("%s:%s%s%s", key, space ? " " : "", value ? value : "Could not allocate string",
5502+
eol ? "\n" : " ");
5503+
}
5504+
54945505
static void stdout_discovery_log(struct nvmf_discovery_log *log, int numrec)
54955506
{
54965507
int i;
@@ -5660,6 +5671,7 @@ static struct print_ops stdout_print_ops = {
56605671
.show_perror = stdout_perror,
56615672
.show_status = stdout_status,
56625673
.show_error_status = stdout_error_status,
5674+
.show_key_value = stdout_key_value,
56635675
};
56645676

56655677
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
@@ -1472,6 +1472,22 @@ void nvme_show_perror(const char *msg)
14721472
ops->show_perror(msg);
14731473
}
14741474

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

nvme-print.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ struct print_ops {
104104
void (*show_perror)(const char *msg);
105105
void (*show_status)(int status);
106106
void (*show_error_status)(int status, const char *msg, va_list ap);
107+
void (*show_key_value)(bool space, bool eol, const char *key, const char *val, va_list ap);
107108

108109
nvme_print_flags_t flags;
109110
};
@@ -323,6 +324,7 @@ void nvme_show_perror(const char *msg);
323324
void nvme_show_error_status(int status, const char *msg, ...);
324325
void nvme_show_init(void);
325326
void nvme_show_finish(void);
327+
void nvme_show_key_value(bool space, bool eol, const char *key, const char *value, ...);
326328
bool nvme_is_fabrics_reg(int offset);
327329
bool nvme_is_fabrics_optional_reg(int offset);
328330
bool nvme_registers_cmbloc_support(__u32 cmbsz);

0 commit comments

Comments
 (0)