Skip to content

Commit cc87270

Browse files
martin-gpyigaw
authored andcommitted
netapp: segregate the print routines
Segregate printing the respective regular/normal and json outputs into separate routines for better readability. Signed-off-by: Martin George <marting@netapp.com>
1 parent 7bb2940 commit cc87270

File tree

1 file changed

+56
-37
lines changed

1 file changed

+56
-37
lines changed

plugins/netapp/netapp-nvme.c

+56-37
Original file line numberDiff line numberDiff line change
@@ -316,64 +316,77 @@ static void netapp_smdevices_print(struct smdevice_info *devices, int count, int
316316
}
317317
}
318318

319-
static void netapp_ontapdevices_print(struct ontapdevice_info *devices,
319+
static void netapp_ontapdevices_print_regular(struct ontapdevice_info *devices,
320320
int count, int format)
321321
{
322-
struct json_object *root = NULL;
323-
struct json_object *json_devices = NULL;
324322
char vsname[ONTAP_LABEL_LEN] = " ";
325323
char nspath[ONTAP_NS_PATHLEN] = " ";
326324
unsigned long long lba;
327325
char size[128];
328326
char uuid_str[37] = " ";
329327
int i;
330328

331-
char basestr[] = "%s, Vserver %s, Namespace Path %s, NSID %d, UUID %s, %s\n";
329+
char *formatstr = NULL;
330+
char basestr[] =
331+
"%s, Vserver %s, Namespace Path %s, NSID %d, UUID %s, %s\n";
332332
char columnstr[] = "%-16s %-25s %-50s %-4d %-38s %-9s\n";
333333

334-
/* default to 'normal' output format */
335-
char *formatstr = basestr;
336-
337-
if (format == NCOLUMN) {
338-
/* change output string and print column headers */
339-
formatstr = columnstr;
334+
if (format == NNORMAL)
335+
formatstr = basestr;
336+
else if (format == NCOLUMN) {
340337
printf("%-16s %-25s %-50s %-4s %-38s %-9s\n",
341-
"Device", "Vserver", "Namespace Path",
342-
"NSID", "UUID", "Size");
338+
"Device", "Vserver", "Namespace Path",
339+
"NSID", "UUID", "Size");
343340
printf("%-16s %-25s %-50s %-4s %-38s %-9s\n",
344-
"----------------", "-------------------------",
345-
"--------------------------------------------------",
346-
"----", "--------------------------------------",
347-
"---------");
348-
} else if (format == NJSON) {
349-
/* prepare for json output */
350-
root = json_create_object();
351-
json_devices = json_create_array();
341+
"----------------", "-------------------------",
342+
"--------------------------------------------------",
343+
"----", "--------------------------------------",
344+
"---------");
345+
formatstr = columnstr;
352346
}
353347

354348
for (i = 0; i < count; i++) {
355-
356349
netapp_get_ns_size(size, &lba, &devices[i].ns);
357350
nvme_uuid_to_string(devices[i].uuid, uuid_str);
358351
netapp_get_ontap_labels(vsname, nspath, devices[i].log_data);
359352

360-
if (format == NJSON) {
361-
netapp_ontapdevice_json(json_devices, devices[i].dev,
362-
vsname, nspath, devices[i].nsid,
363-
uuid_str, size, lba,
364-
le64_to_cpu(devices[i].ns.nsze));
365-
} else
366-
printf(formatstr, devices[i].dev, vsname, nspath,
367-
devices[i].nsid, uuid_str, size);
353+
printf(formatstr, devices[i].dev, vsname, nspath,
354+
devices[i].nsid, uuid_str, size);
368355
}
356+
}
369357

370-
if (format == NJSON) {
371-
/* complete the json output */
372-
json_object_add_value_array(root, "ONTAPdevices", json_devices);
373-
json_print_object(root, NULL);
374-
printf("\n");
375-
json_free_object(root);
358+
static void netapp_ontapdevices_print_json(struct ontapdevice_info *devices,
359+
int count)
360+
{
361+
struct json_object *root = NULL;
362+
struct json_object *json_devices = NULL;
363+
char vsname[ONTAP_LABEL_LEN] = " ";
364+
char nspath[ONTAP_NS_PATHLEN] = " ";
365+
unsigned long long lba;
366+
char size[128];
367+
char uuid_str[37] = " ";
368+
int i;
369+
370+
/* prepare for the json output */
371+
root = json_create_object();
372+
json_devices = json_create_array();
373+
374+
for (i = 0; i < count; i++) {
375+
netapp_get_ns_size(size, &lba, &devices[i].ns);
376+
nvme_uuid_to_string(devices[i].uuid, uuid_str);
377+
netapp_get_ontap_labels(vsname, nspath, devices[i].log_data);
378+
379+
netapp_ontapdevice_json(json_devices, devices[i].dev,
380+
vsname, nspath, devices[i].nsid,
381+
uuid_str, size, lba,
382+
le64_to_cpu(devices[i].ns.nsze));
376383
}
384+
385+
/* complete the json output */
386+
json_object_add_value_array(root, "ONTAPdevices", json_devices);
387+
json_print_object(root, NULL);
388+
printf("\n");
389+
json_free_object(root);
377390
}
378391

379392
static int nvme_get_ontap_c2_log(int fd, __u32 nsid, void *buf, __u32 buflen)
@@ -666,8 +679,14 @@ static int netapp_ontapdevices(int argc, char **argv, struct command *command,
666679
close(fd);
667680
}
668681

669-
if (num_ontapdevices)
670-
netapp_ontapdevices_print(ontapdevices, num_ontapdevices, fmt);
682+
if (num_ontapdevices) {
683+
if (fmt == NNORMAL || fmt == NCOLUMN)
684+
netapp_ontapdevices_print_regular(ontapdevices,
685+
num_ontapdevices, fmt);
686+
else if (fmt == NJSON)
687+
netapp_ontapdevices_print_json(ontapdevices,
688+
num_ontapdevices);
689+
}
671690

672691
for (i = 0; i < num; i++)
673692
free(devices[i]);

0 commit comments

Comments
 (0)