Hello,

I read the source from vzlist and rewrote the patch so it is more consistent with the way JSON is handled in vzlist. I think it is much less dirty ;)

This also changed the option, it is now "-j" and not "-J" (as in vzlist).

Regards,
Denis

----8<---------------------------
diff -ru vzctl-4.2/src/snapshot-list.c vzctl-4.2-json/src/snapshot-list.c
--- vzctl-4.2/src/snapshot-list.c       2013-02-16 02:06:40.000000000 +0100
+++ vzctl-4.2-json/src/snapshot-list.c  2013-02-24 17:52:18.856138667 +0100
@@ -33,62 +33,62 @@
        return g_last_field ? "%s\n" : fmt;
 }

-static void print_uuid(struct vzctl_snapshot_data *p)
+static void print_uuid(struct vzctl_snapshot_data *p, char *fmt)
 {
-       printf(FMT("%-38s "), p->guid);
+       printf(FMT(fmt), p->guid);
 }

-static void print_date(struct vzctl_snapshot_data *p)
+static void print_date(struct vzctl_snapshot_data *p, char *fmt)
 {
-       printf(FMT("%-19s "), p->date ? p->date : "");
+       printf(FMT(fmt), p->date ? p->date : "");
 }

-static void print_name(struct vzctl_snapshot_data *p)
+static void print_name(struct vzctl_snapshot_data *p, char *fmt)
 {
-       printf(FMT("%-32s "), p->name ? p->name : "");
+       printf(FMT(fmt), p->name ? p->name : "");
 }

-static void print_desc(struct vzctl_snapshot_data *p)
+static void print_desc(struct vzctl_snapshot_data *p, char *fmt)
 {
-       printf(FMT("%-38s "), p->desc ? p->desc : "");
+       printf(FMT(fmt), p->desc ? p->desc : "");
 }

-static void print_parent_uuid(struct vzctl_snapshot_data *p)
+static void print_parent_uuid(struct vzctl_snapshot_data *p, char *fmt)
 {
-       printf(FMT("%-38s "), p->parent_guid);
+       printf(FMT(fmt), p->parent_guid);
 }

-static void print_current(struct vzctl_snapshot_data *p)
+static void print_current(struct vzctl_snapshot_data *p, char *fmt)
 {
-       printf(FMT("%1s "), p->current ? "*" : "");
+       printf(FMT(fmt), p->current ? "*" : "");
 }

-static void print_device(struct vzctl_snapshot_data *p)
+static void print_device(struct vzctl_snapshot_data *p, char *fmt)
 {
        char buf[PATH_MAX];
        char dev[PATH_MAX] = "";

        ploop.set_component_name(g_di,
-                       generate_snapshot_component_name(g_envid, p->guid, buf, 
sizeof(buf)));
+               generate_snapshot_component_name(g_envid, p->guid, buf, 
sizeof(buf)));

        if (ploop.get_dev(g_di, dev, sizeof(dev)))
                goto err;

-       printf(FMT("%-32s "), dev);
+       printf(FMT(fmt), dev);
        return;
 err:
-       printf(FMT("%-32s "), "");
+       printf(FMT(fmt), "");
        fprintf(stderr, "%s", ploop.get_last_error());
        error++;
 }

-static void print_target(struct vzctl_snapshot_data *p)
+static void print_target(struct vzctl_snapshot_data *p, char *fmt)
 {
        char buf[PATH_MAX];
        char dev[PATH_MAX];

        ploop.set_component_name(g_di,
-                       generate_snapshot_component_name(g_envid, p->guid, buf, 
sizeof(buf)));
+               generate_snapshot_component_name(g_envid, p->guid, buf, 
sizeof(buf)));

        if (ploop.get_dev(g_di, dev, sizeof(dev)))
                goto err;
@@ -107,17 +107,17 @@
        char *name;
        char *hdr;
        char *fmt;
-       void (* print_fn)(struct vzctl_snapshot_data *p);
+       void (* print_fn)(struct vzctl_snapshot_data *p, char *);
 } field_tbl[] =
 {
-{"uuid", "UUID", "%-38s", print_uuid},
-{"parent_uuid", "PARENT_UUID", "%-38s", print_parent_uuid},
-{"current", "C", "%1s", print_current},
-{"date", "DATE", "%-19s", print_date},
-{"name", "NAME", "%-32s", print_name},
-{"desc", "DESC", "%-32s", print_desc},
-{"device", "DEVICE", "%-16s", print_device},
-{"target", "TARGET", "%-32s", print_target},
+{"uuid", "UUID", "%-38s ", print_uuid},
+{"parent_uuid", "PARENT_UUID", "%-38s ", print_parent_uuid},
+{"current", "C", "%1s ", print_current},
+{"date", "DATE", "%-19s ", print_date},
+{"name", "NAME", "%-32s ", print_name},
+{"desc", "DESC", "%-32s ", print_desc},
+{"device", "DEVICE", "%-16s ", print_device},
+{"target", "TARGET", "%-32s ", print_target},
 };

 static void print_names(void)
@@ -240,7 +240,7 @@
 static void usage_snapshot_list(int err)
 {
        fprintf(err ? stderr : stdout,
- "vzctl snapshot-list <ctid|name> [-H] [-o field[,field...]] [--id <uuid>]\n" + "vzctl snapshot-list <ctid|name> [-H] [-J] [-o field[,field...]] [--id <uuid>]\n"
                "vzctl snapshot-list <ctid|name> -L | --list\n"
               );
 }
@@ -249,7 +249,8 @@
                const char *ve_private)
 {
        int c, ret;
-       int no_hdr = 0;
+       int no_hdr = 0, fmt_json = 0;
+       int is_json = 0;
        struct vzctl_snapshot_tree *tree = NULL;
        struct id_entry *field_entry = NULL;
        struct id_entry * uuid_entry = NULL;
@@ -264,13 +265,14 @@
                {"uuid",      required_argument, NULL, 'u'},
                        {"id",        required_argument, NULL, 'u'},
                {"list",      no_argument, NULL, 'L'},
+               {"json",      no_argument, NULL, 'j'},
                { NULL, 0, NULL, 0 }
        };

        g_envid = envid;
        while (1) {
                int option_index = -1;
-               c = getopt_long(argc, argv, "HhLo:u:",
+               c = getopt_long(argc, argv, "HjhLo:u:",
                                list_options, &option_index);
                if (c == -1)
                        break;
@@ -296,6 +298,9 @@
                case 'L':
                        print_names();
                        return 0;
+               case 'j':
+                       no_hdr = fmt_json = 1;
+                       break;
                default:
                        usage_snapshot_list(1);
                        return VZ_INVALID_PARAMETER_SYNTAX;
@@ -309,7 +314,7 @@
                fprintf(stderr, "\n");
                return VZ_INVALID_PARAMETER_SYNTAX;
        }
-
+       
        ret = build_field_order_list(output);
        if (ret)
                return ret;
@@ -352,12 +357,29 @@

        print_hdr(no_hdr);

-       list_for_each(uuid_entry, &g_uuid_list_head, list) {
+       if (fmt_json)
+               printf("[");
+
+        list_for_each(uuid_entry, &g_uuid_list_head, list) {
+               if (fmt_json)
+                       printf("%s\n  {",is_json?",":"");
+               is_json = 0;
                list_for_each(field_entry, &g_field_order_head, list) {
-                       g_last_field = ((void *)g_field_order_head.prev == 
(void*)field_entry);
-                       
field_tbl[field_entry->id].print_fn(tree->snapshots[uuid_entry->id]);
+                       if (fmt_json)
+ printf("%s\n \"%s\": \"",is_json?",":"",field_tbl[field_entry->id].name);
+                       else
+                               g_last_field = ((void *)g_field_order_head.prev 
== (void*)field_entry);
+ field_tbl[field_entry->id].print_fn(tree->snapshots[uuid_entry->id], fmt_json?"%s":field_tbl[field_entry->id].fmt);
+                       if (fmt_json)
+                               printf("\"");
+                       is_json = 1;
                }
+               if (fmt_json)
+                       printf("%s  }",is_json?"\n":"");
        }
+        if (fmt_json)
+                printf("%s]\n",is_json?"\n":"");
+
        ploop.free_diskdescriptor(g_di);

        return 0;
diff -ru vzctl-4.2/src/vzctl.c vzctl-4.2-json/src/vzctl.c
--- vzctl-4.2/src/vzctl.c       2013-02-16 02:06:40.000000000 +0100
+++ vzctl-4.2-json/src/vzctl.c  2013-02-24 16:51:04.284138604 +0100
@@ -73,7 +73,7 @@
"vzctl snapshot <ctid> [--id <uuid>] [--name <name>] [--description <desc>]\n"
 "   [--skip-suspend]\n"
 "vzctl snapshot-switch | snapshot-delete <ctid> --id <uuid>\n"
-"vzctl snapshot-list <ctid> [-H] [-o field[,field...]] [--id <uuid>]\n"
+"vzctl snapshot-list <ctid> [-H] [-j] [-o field[,field...]] [--id <uuid>]\n"
 #endif
 "vzctl quotaon | quotaoff | quotainit <ctid>\n"
 "vzctl console <ctid> [ttyno]\n"
----8<---------------------------
_______________________________________________
Devel mailing list
[email protected]
https://lists.openvz.org/mailman/listinfo/devel

Reply via email to