]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme-print-json: Add json output status function
authorTokunori Ikegami <ikegami.t@gmail.com>
Sat, 25 Mar 2023 02:36:10 +0000 (11:36 +0900)
committerDaniel Wagner <wagi@monom.org>
Mon, 27 Mar 2023 09:28:38 +0000 (11:28 +0200)
For the json output add json argconfig option also.

Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
nvme-print-json.c
nvme-print-json.h
nvme-print.c
util/argconfig.c
util/argconfig.h

index 40dbb2f8f2d21bdc32408734f65c0145c8565a94..eb7e484f4bb69cf17d1355771ffa6074cf69b7b2 100644 (file)
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 
 #include <assert.h>
+#include <errno.h>
 
 #include "nvme-print-json.h"
 
@@ -2813,3 +2814,43 @@ void json_connect_msg(nvme_ctrl_t c)
        printf("\n");
        json_free_object(root);
 }
+
+static void json_output_object(struct json_object *root)
+{
+       json_print_object(root, NULL);
+       printf("\n");
+       json_free_object(root);
+}
+
+void json_output_status(int status)
+{
+       struct json_object *root = json_create_object();
+       int val;
+       int type;
+
+       if (status < 0) {
+               json_object_add_value_string(root, "error", nvme_strerror(errno));
+               return json_output_object(root);
+       }
+
+       val = nvme_status_get_value(status);
+       type = nvme_status_get_type(status);
+
+       switch (type) {
+       case NVME_STATUS_TYPE_NVME:
+               json_object_add_value_string(root, "error", nvme_status_to_string(val, false));
+               json_object_add_value_string(root, "type", "nvme");
+               break;
+       case NVME_STATUS_TYPE_MI:
+               json_object_add_value_string(root, "error", nvme_mi_status_to_string(val));
+               json_object_add_value_string(root, "type", "nvme-mi");
+               break;
+       default:
+               json_object_add_value_string(root, "type", "unknow");
+               break;
+       }
+
+       json_object_add_value_int(root, "value", val);
+
+       json_output_object(root);
+}
index 6720d454cb44c8b411c41175f9308bd7eedd669d..3641c80b1e4f6df393426f178b740658e6543fb8 100644 (file)
@@ -78,6 +78,7 @@ void json_predictable_latency_event_agg_log(
 void json_predictable_latency_per_nvmset(
        struct nvme_nvmset_predictable_lat_log *plpns_log,
        __u16 nvmset_id);
+void json_output_status(int status);
 
 /* fabrics.c */
 void json_discovery_log(struct nvmf_discovery_log *log, int numrec);
@@ -136,6 +137,7 @@ void json_connect_msg(nvme_ctrl_t c);
 #define json_persistent_event_log(pevent_log_info, size)
 #define json_predictable_latency_event_agg_log(pea_log, log_entries)
 #define json_predictable_latency_per_nvmset(plpns_log, nvmset_id)
+#define json_output_status(status)
 
 /* fabrics.c */
 #define json_discovery_log(log, numrec)
index ce4abe07506daab78dc8e14ce552da9643765666..356fb4405eb6c25a82793efbfe586b44da7bdcec 100644 (file)
@@ -1744,6 +1744,9 @@ void nvme_show_status(int status)
        int val;
        int type;
 
+       if (argconfig_output_format_json(false))
+               return json_output_status(status);
+
        /*
         * Callers should be checking for negative values first, but provide a
         * sensible fallback anyway
index a891dbbea3899608580cb35b18b81157f53e7f85..3eb885faccdeceb8b61cc4142f89fda8ae4b46ff 100644 (file)
@@ -285,6 +285,16 @@ static int argconfig_parse_type(struct argconfig_commandline_options *s, struct
        return ret;
 }
 
+bool argconfig_output_format_json(bool set)
+{
+       static bool output_format_json = false;
+
+       if (set)
+               output_format_json = true;
+
+       return output_format_json;
+}
+
 int argconfig_parse(int argc, char *argv[], const char *program_desc,
                    struct argconfig_commandline_options *options)
 {
@@ -298,8 +308,8 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc,
        for (s = options; s->option; s++)
                options_count++;
 
-       long_opts = calloc(1, sizeof(struct option) * (options_count + 2));
-       short_opts = calloc(1, sizeof(*short_opts) * (options_count * 3 + 4));
+       long_opts = calloc(1, sizeof(struct option) * (options_count + 3));
+       short_opts = calloc(1, sizeof(*short_opts) * (options_count * 3 + 5));
 
        if (!long_opts || !short_opts) {
                fprintf(stderr, "failed to allocate memory for opts: %s\n", strerror(errno));
@@ -325,10 +335,14 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc,
        }
 
        long_opts[option_index].name = "help";
-       long_opts[option_index].val = 'h';
+       long_opts[option_index++].val = 'h';
+
+       long_opts[option_index].name = "json";
+       long_opts[option_index].val = 'j';
 
        short_opts[short_index++] = '?';
-       short_opts[short_index] = 'h';
+       short_opts[short_index++] = 'h';
+       short_opts[short_index] = 'j';
 
        optind = 0;
        while ((c = getopt_long_only(argc, argv, short_opts, long_opts, &option_index)) != -1) {
@@ -338,6 +352,8 @@ int argconfig_parse(int argc, char *argv[], const char *program_desc,
                                ret = -EINVAL;
                                break;
                        }
+                       if (c == 'j')
+                               argconfig_output_format_json(true);
                        for (option_index = 0; option_index < options_count; option_index++) {
                                if (c == options[option_index].short_option)
                                        break;
index b691159f5143161f69196a045e6fe65ea0d60088..81eaaf405e53d78d8a72b8db21623f25c9f6755c 100644 (file)
@@ -137,4 +137,5 @@ void argconfig_register_help_func(argconfig_help_func * f);
 void print_word_wrapped(const char *s, int indent, int start, FILE *stream);
 bool argconfig_parse_seen(struct argconfig_commandline_options *options,
                          const char *option);
+bool argconfig_output_format_json(bool set);
 #endif