]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
losetup: add --json
authorKarel Zak <kzak@redhat.com>
Fri, 5 Jun 2015 08:30:52 +0000 (10:30 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 5 Jun 2015 08:30:52 +0000 (10:30 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/src/table_print.c
sys-utils/losetup.8
sys-utils/losetup.c

index 37d75c9627b5d41055e8f38eef690c697f1a009b..731879cae6fd29bd672096fe2ebf7424f16d7aec 100644 (file)
@@ -539,6 +539,7 @@ static int print_header(struct libscols_table *tb, struct libscols_buffer *buf)
 
        if (scols_table_is_noheadings(tb) ||
            scols_table_is_export(tb) ||
+           scols_table_is_json(tb) ||
            list_empty(&tb->tb_lines))
                return 0;
 
@@ -942,6 +943,12 @@ int scols_print_table(struct libscols_table *tb)
                return -EINVAL;
 
        DBG(TAB, ul_debugobj(tb, "printing"));
+
+       if (list_empty(&tb->tb_lines)) {
+               DBG(TAB, ul_debugobj(tb, "ignore -- epmty table"));
+               return 0;
+       }
+
        if (!tb->symbols)
                scols_table_set_symbols(tb, NULL);      /* use default */
 
@@ -964,7 +971,7 @@ int scols_print_table(struct libscols_table *tb)
        if (!buf)
                return -ENOMEM;
 
-       if (!(scols_table_is_raw(tb) || scols_table_is_export(tb))) {
+       if (tb->format == SCOLS_FMT_HUMAN) {
                rc = recount_widths(tb, buf);
                if (rc != 0)
                        goto done;
@@ -972,11 +979,9 @@ int scols_print_table(struct libscols_table *tb)
 
        fput_table_open(tb);
 
-       if (!scols_table_is_json(tb)) {
-               rc = print_header(tb, buf);
-               if (rc)
-                       goto done;
-       }
+       rc = print_header(tb, buf);
+       if (rc)
+               goto done;
 
        if (scols_table_is_tree(tb))
                rc = print_tree(tb, buf);
index 6f18e540f85063783200ee6317d64eaa38aab5ae..41661e553b94f9e39ea8a0f48282b314104e2f36 100644 (file)
@@ -100,11 +100,14 @@ Otherwise, just print its name.
 Show the status of all loop devices associated with the given
 .IR file .
 .TP
+.IP "\fB\-J, \fB\-\-json\fP"
+Use JSON format for \fB\-\-list\fP output.
+.TP
 .BR \-l , " \-\-list"
 If a loop device or the \fB-a\fR option is specified, print the default columns
 for either the specified loop device or all loop devices; the default is to
 print info about all devices.  See also \fB\-\-output\fP, \fB\-\-noheadings\fP
-and \fB\-\-raw\fP.
+\fB\-\-json\fP and \fB\-\-raw\fP.
 .TP
 .BR \-n , " \-\-noheadings"
 Don't print headings for \fB\-\-list\fP output format.
index 9201782ad64069e895d9e3cf73ce2385654ad9a2..a68b222295df9601c3cf1a60ff2835c8d8da0422 100644 (file)
@@ -53,6 +53,7 @@ enum {
 /* basic output flags */
 static int no_headings;
 static int raw;
+static int json;
 
 struct colinfo {
        const char *name;
@@ -302,8 +303,12 @@ static int show_table(struct loopdev_cxt *lc,
        if (!(tb = scols_new_table()))
                err(EXIT_FAILURE, _("failed to initialize output table"));
        scols_table_enable_raw(tb, raw);
+       scols_table_enable_json(tb, json);
        scols_table_enable_noheadings(tb, no_headings);
 
+       if (json)
+               scols_table_set_name(tb, "loopdevices");
+
        for (i = 0; i < ncolumns; i++) {
                struct colinfo *ci = get_column_info(i);
 
@@ -397,6 +402,7 @@ static void usage(FILE *out)
        fputs(_(" -O, --output <cols>           specify columns to output for --list\n"), out);
        fputs(_(" -n, --noheadings              don't print headings for --list output\n"), out);
        fputs(_("     --raw                     use raw --list output format\n"), out);
+       fputs(_(" -J, --json                    use JSON --list output format\n"), out);
 
        fputs(USAGE_SEPARATOR, out);
        fputs(USAGE_HELP, out);
@@ -454,6 +460,7 @@ int main(int argc, char **argv)
                { "find", 0, 0, 'f' },
                { "help", 0, 0, 'h' },
                { "associated", 1, 0, 'j' },
+               { "json", 0, 0, 'J' },
                { "list", 0, 0, 'l' },
                { "noheadings", 0, 0, 'n' },
                { "offset", 1, 0, 'o' },
@@ -472,6 +479,7 @@ int main(int argc, char **argv)
                { 'D','a','c','d','f','j' },
                { 'D','c','d','f','l' },
                { 'D','c','d','f','O' },
+               { 'J',OPT_RAW },
                { 0 }
        };
        int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
@@ -484,7 +492,7 @@ int main(int argc, char **argv)
        if (loopcxt_init(&lc, 0))
                err(EXIT_FAILURE, _("failed to initialize loopcxt"));
 
-       while ((c = getopt_long(argc, argv, "ac:d:Dfhj:lno:O:PrvV",
+       while ((c = getopt_long(argc, argv, "ac:d:Dfhj:Jlno:O:PrvV",
                                longopts, NULL)) != -1) {
 
                err_exclusive_options(c, longopts, excl, excl_st);
@@ -519,6 +527,9 @@ int main(int argc, char **argv)
                case 'h':
                        usage(stdout);
                        break;
+               case 'J':
+                       json = 1;
+                       break;
                case 'j':
                        act = A_SHOW;
                        file = optarg;
@@ -566,6 +577,11 @@ int main(int argc, char **argv)
                list = 1;
        }
 
+       if (!act && argc == 2 && (raw || json)) {
+               act = A_SHOW;
+               list = 1;
+       }
+
        /* default --list output columns */
        if (list && !ncolumns) {
                columns[ncolumns++] = COL_NAME;