[libvirt] [PATCH 1/2] virsh-snapshot: Add ability to print only snapshot names

Peter Krempa pkrempa at redhat.com
Fri Mar 1 15:43:53 UTC 2013


Help script creators by not having to parse the names from the table.
---
 tools/virsh-snapshot.c | 30 ++++++++++++++++++++++++------
 tools/virsh.pod        | 11 +++++++----
 2 files changed, 31 insertions(+), 10 deletions(-)

diff --git a/tools/virsh-snapshot.c b/tools/virsh-snapshot.c
index 3d82276..ed41014 100644
--- a/tools/virsh-snapshot.c
+++ b/tools/virsh-snapshot.c
@@ -1556,6 +1556,12 @@ static const vshCmdOptDef opts_snapshot_list[] = {
      .flags = 0,
      .help = N_("with --from, list all descendants")
     },
+    {.name = "name",
+     .type = VSH_OT_BOOL,
+     .flags = 0,
+     .help = N_("list snapshot names only")
+    },
+
     {.name = NULL}
 };

@@ -1578,10 +1584,17 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
     char timestr[100];
     struct tm time_info;
     bool tree = vshCommandOptBool(cmd, "tree");
+    bool name = vshCommandOptBool(cmd, "name");
     const char *from = NULL;
     virDomainSnapshotPtr start = NULL;
     vshSnapshotListPtr snaplist = NULL;

+    if (tree && name) {
+        vshError(ctl, "%s",
+                 _("--tree and --name are mutually exclusive"));
+        return false;
+    }
+
     dom = vshCommandOptDomain(ctl, cmd, NULL);
     if (dom == NULL)
         goto cleanup;
@@ -1660,7 +1673,7 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
                                            tree)) == NULL)
         goto cleanup;

-    if (!tree) {
+    if (!tree && !name) {
         if (show_parent)
             vshPrintExtra(ctl, " %-20s %-25s %-15s %s",
                           _("Name"), _("Creation Time"), _("State"),
@@ -1689,7 +1702,7 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
     }

     for (i = 0; i < snaplist->nsnaps; i++) {
-        const char *name;
+        const char *snap_name;

         /* free up memory from previous iterations of the loop */
         VIR_FREE(parent);
@@ -1699,8 +1712,13 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)
         VIR_FREE(doc);

         snapshot = snaplist->snaps[i].snap;
-        name = virDomainSnapshotGetName(snapshot);
-        assert(name);
+        snap_name = virDomainSnapshotGetName(snapshot);
+        assert(snap_name);
+
+        if (name) {
+            vshPrint(ctl, "%s\n", snap_name);
+            continue;
+        }

         doc = virDomainSnapshotGetXMLDesc(snapshot, 0);
         if (!doc)
@@ -1731,9 +1749,9 @@ cmdSnapshotList(vshControl *ctl, const vshCmd *cmd)

         if (parent)
             vshPrint(ctl, " %-20s %-25s %-15s %s\n",
-                     name, timestr, state, parent);
+                     snap_name, timestr, state, parent);
         else
-            vshPrint(ctl, " %-20s %-25s %s\n", name, timestr, state);
+            vshPrint(ctl, " %-20s %-25s %s\n", snap_name, timestr, state);
     }

     ret = true;
diff --git a/tools/virsh.pod b/tools/virsh.pod
index a5d8fe6..7fb89e4 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -2854,10 +2854,11 @@ accessible only from the original name.
 Output basic information about a named <snapshot>, or the current snapshot
 with I<--current>.

-=item B<snapshot-list> I<domain> [{I<--parent> | I<--roots> | I<--tree>}]
+=item B<snapshot-list> I<domain> [I<--metadata>] [I<--no-metadata>]
+[{I<--parent> | I<--roots> | [{I<--tree> | I<--name>}]}]
 [{[I<--from>] B<snapshot> | I<--current>} [I<--descendants>]]
-[I<--metadata>] [I<--no-metadata>] [I<--leaves>] [I<--no-leaves>]
-[I<--inactive>] [I<--active>] [I<--disk-only>] [I<--internal>] [I<--external>]
+[I<--leaves>] [I<--no-leaves>] p[I<--inactive>] [I<--active>]
+[I<--disk-only>] [I<--internal>] [I<--external>]

 List all of the available snapshots for the given domain, defaulting
 to show columns for the snapshot name, creation time, and domain state.
@@ -2866,7 +2867,9 @@ If I<--parent> is specified, add a column to the output table giving
 the name of the parent of each snapshot.  If I<--roots> is specified,
 the list will be filtered to just snapshots that have no parents.
 If I<--tree> is specified, the output will be in a tree format, listing
-just snapshot names.  These three options are mutually exclusive.
+just snapshot names.  These three options are mutually exclusive. If
+I<--name> is specified only the snapshot name is printed. This option is
+mutually exclusive with I<--tree>.

 If I<--from> is provided, filter the list to snapshots which are
 children of the given B<snapshot>; or if I<--current> is provided,
-- 
1.8.1.1




More information about the libvir-list mailing list