]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
netapp: segregate the print routines
authorMartin George <marting@netapp.com>
Fri, 16 Aug 2024 12:42:04 +0000 (18:12 +0530)
committerDaniel Wagner <wagi@monom.org>
Mon, 19 Aug 2024 08:56:59 +0000 (10:56 +0200)
Segregate printing the respective regular/normal and json outputs
into separate routines for better readability.

Signed-off-by: Martin George <marting@netapp.com>
plugins/netapp/netapp-nvme.c

index fb1b5679acaef48b0f822cbde9925c863c3c7777..4694cc9d4b96cf502a8a0524ab2ec7844f852314 100644 (file)
@@ -316,11 +316,9 @@ static void netapp_smdevices_print(struct smdevice_info *devices, int count, int
        }
 }
 
-static void netapp_ontapdevices_print(struct ontapdevice_info *devices,
+static void netapp_ontapdevices_print_regular(struct ontapdevice_info *devices,
                int count, int format)
 {
-       struct json_object *root = NULL;
-       struct json_object *json_devices = NULL;
        char vsname[ONTAP_LABEL_LEN] = " ";
        char nspath[ONTAP_NS_PATHLEN] = " ";
        unsigned long long lba;
@@ -328,52 +326,67 @@ static void netapp_ontapdevices_print(struct ontapdevice_info *devices,
        char uuid_str[37] = " ";
        int i;
 
-       char basestr[] = "%s, Vserver %s, Namespace Path %s, NSID %d, UUID %s, %s\n";
+       char *formatstr = NULL;
+       char basestr[] =
+               "%s, Vserver %s, Namespace Path %s, NSID %d, UUID %s, %s\n";
        char columnstr[] = "%-16s %-25s %-50s %-4d %-38s %-9s\n";
 
-       /* default to 'normal' output format */
-       char *formatstr = basestr;
-
-       if (format == NCOLUMN) {
-               /* change output string and print column headers */
-               formatstr = columnstr;
+       if (format == NNORMAL)
+               formatstr = basestr;
+       else if (format == NCOLUMN) {
                printf("%-16s %-25s %-50s %-4s %-38s %-9s\n",
-                               "Device", "Vserver", "Namespace Path",
-                               "NSID", "UUID", "Size");
+                       "Device", "Vserver", "Namespace Path",
+                       "NSID", "UUID", "Size");
                printf("%-16s %-25s %-50s %-4s %-38s %-9s\n",
-                               "----------------", "-------------------------",
-                               "--------------------------------------------------",
-                               "----", "--------------------------------------",
-                               "---------");
-       } else if (format == NJSON) {
-               /* prepare for json output */
-               root = json_create_object();
-               json_devices = json_create_array();
+                       "----------------", "-------------------------",
+                       "--------------------------------------------------",
+                       "----", "--------------------------------------",
+                       "---------");
+               formatstr = columnstr;
        }
 
        for (i = 0; i < count; i++) {
-
                netapp_get_ns_size(size, &lba, &devices[i].ns);
                nvme_uuid_to_string(devices[i].uuid, uuid_str);
                netapp_get_ontap_labels(vsname, nspath, devices[i].log_data);
 
-               if (format == NJSON) {
-                       netapp_ontapdevice_json(json_devices, devices[i].dev,
-                                       vsname, nspath, devices[i].nsid,
-                                       uuid_str, size, lba,
-                                       le64_to_cpu(devices[i].ns.nsze));
-               } else
-                       printf(formatstr, devices[i].dev, vsname, nspath,
-                                       devices[i].nsid, uuid_str, size);
+               printf(formatstr, devices[i].dev, vsname, nspath,
+                               devices[i].nsid, uuid_str, size);
        }
+}
 
-       if (format == NJSON) {
-               /* complete the json output */
-               json_object_add_value_array(root, "ONTAPdevices", json_devices);
-               json_print_object(root, NULL);
-               printf("\n");
-               json_free_object(root);
+static void netapp_ontapdevices_print_json(struct ontapdevice_info *devices,
+               int count)
+{
+       struct json_object *root = NULL;
+       struct json_object *json_devices = NULL;
+       char vsname[ONTAP_LABEL_LEN] = " ";
+       char nspath[ONTAP_NS_PATHLEN] = " ";
+       unsigned long long lba;
+       char size[128];
+       char uuid_str[37] = " ";
+       int i;
+
+       /* prepare for the json output */
+       root = json_create_object();
+       json_devices = json_create_array();
+
+       for (i = 0; i < count; i++) {
+               netapp_get_ns_size(size, &lba, &devices[i].ns);
+               nvme_uuid_to_string(devices[i].uuid, uuid_str);
+               netapp_get_ontap_labels(vsname, nspath, devices[i].log_data);
+
+               netapp_ontapdevice_json(json_devices, devices[i].dev,
+                               vsname, nspath, devices[i].nsid,
+                               uuid_str, size, lba,
+                               le64_to_cpu(devices[i].ns.nsze));
        }
+
+       /* complete the json output */
+       json_object_add_value_array(root, "ONTAPdevices", json_devices);
+       json_print_object(root, NULL);
+       printf("\n");
+       json_free_object(root);
 }
 
 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,
                close(fd);
        }
 
-       if (num_ontapdevices)
-               netapp_ontapdevices_print(ontapdevices, num_ontapdevices, fmt);
+       if (num_ontapdevices) {
+               if (fmt == NNORMAL || fmt == NCOLUMN)
+                       netapp_ontapdevices_print_regular(ontapdevices,
+                                       num_ontapdevices, fmt);
+               else if (fmt == NJSON)
+                       netapp_ontapdevices_print_json(ontapdevices,
+                                       num_ontapdevices);
+       }
 
        for (i = 0; i < num; i++)
                free(devices[i]);