Add bash completion for --node node1[:node2]
authorMichael Hanselmann <hansmi@google.com>
Fri, 4 Sep 2009 15:08:51 +0000 (17:08 +0200)
committerMichael Hanselmann <hansmi@google.com>
Fri, 4 Sep 2009 15:58:48 +0000 (17:58 +0200)
Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Luca Bigliardi <shammash@google.com>

autotools/build-bash-completion
lib/cli.py
scripts/gnt-backup
scripts/gnt-instance

index 3a986c6..7edc9df 100755 (executable)
@@ -339,6 +339,49 @@ class CompletionWriter:
           WriteCompReply(sw, "-W \"$(_ganeti_os)\"", cur=cur)
         elif suggest == cli.OPT_COMPL_ONE_IALLOCATOR:
           WriteCompReply(sw, "-W \"$(_ganeti_iallocator)\"", cur=cur)
+        elif suggest == cli.OPT_COMPL_INST_ADD_NODES:
+          sw.Write("local tmp= node1= pfx= curvalue=\"${optcur#*:}\"")
+
+          sw.Write("if [[ \"$optcur\" == *:* ]]; then")
+          sw.IncIndent()
+          try:
+            sw.Write("node1=\"${optcur%%:*}\"")
+
+            sw.Write("if [[ \"$COMP_WORDBREAKS\" != *:* ]]; then")
+            sw.IncIndent()
+            try:
+              sw.Write("pfx=\"$node1:\"")
+            finally:
+              sw.DecIndent()
+            sw.Write("fi")
+          finally:
+            sw.DecIndent()
+          sw.Write("fi")
+
+          sw.Write("_ganeti_dbglog pfx=\"'$pfx'\" curvalue=\"'$curvalue'\""
+                   " node1=\"'$node1'\"")
+
+          sw.Write("for i in $(_ganeti_nodes); do")
+          sw.IncIndent()
+          try:
+            sw.Write("if [[ -z \"$node1\" ]]; then")
+            sw.IncIndent()
+            try:
+              sw.Write("tmp=\"$tmp $i $i:\"")
+            finally:
+              sw.DecIndent()
+            sw.Write("elif [[ \"$i\" != \"$node1\" ]]; then")
+            sw.IncIndent()
+            try:
+              sw.Write("tmp=\"$tmp $i\"")
+            finally:
+              sw.DecIndent()
+            sw.Write("fi")
+          finally:
+            sw.DecIndent()
+          sw.Write("done")
+
+          WriteCompReply(sw, "-P \"$pfx\" -W \"$tmp\"", cur="\"$curvalue\"")
         else:
           WriteCompReply(sw, "-W %s" % utils.ShellQuote(suggest), cur=cur)
       finally:
index dd98b4d..7eb05ab 100644 (file)
@@ -59,6 +59,7 @@ __all__ = ["DEBUG_OPT", "NOHDR_OPT", "SEP_OPT", "GenericMain",
            "OPT_COMPL_ONE_NODE", "OPT_COMPL_ONE_INSTANCE",
            "OPT_COMPL_MANY_NODES",
            "OPT_COMPL_ONE_OS", "OPT_COMPL_ONE_IALLOCATOR",
+           "OPT_COMPL_INST_ADD_NODES",
            ]
 
 NO_PREFIX = "no_"
@@ -340,7 +341,8 @@ def check_key_val(option, opt, value):
  OPT_COMPL_ONE_NODE,
  OPT_COMPL_ONE_INSTANCE,
  OPT_COMPL_ONE_OS,
- OPT_COMPL_ONE_IALLOCATOR) = range(100, 105)
+ OPT_COMPL_ONE_IALLOCATOR,
+ OPT_COMPL_INST_ADD_NODES) = range(100, 106)
 
 OPT_COMPL_ALL = frozenset([
   OPT_COMPL_MANY_NODES,
@@ -348,6 +350,7 @@ OPT_COMPL_ALL = frozenset([
   OPT_COMPL_ONE_INSTANCE,
   OPT_COMPL_ONE_OS,
   OPT_COMPL_ONE_IALLOCATOR,
+  OPT_COMPL_INST_ADD_NODES,
   ])
 
 
index 2608f87..12a667d 100755 (executable)
@@ -213,7 +213,8 @@ import_opts = [
   DEBUG_OPT,
   cli_option("-n", "--node", dest="node",
              help="Target node and optional secondary node",
-             metavar="<pnode>[:<snode>]"),
+             metavar="<pnode>[:<snode>]",
+             completion_suggest=OPT_COMPL_INST_ADD_NODES),
   cli_option("-B", "--backend", dest="beparams",
              type="keyval", default={},
              help="Backend parameters"),
index 03a22ce..4980614 100755 (executable)
@@ -1369,7 +1369,8 @@ add_opts = [
   DEBUG_OPT,
   cli_option("-n", "--node", dest="node",
              help="Target node and optional secondary node",
-             metavar="<pnode>[:<snode>]"),
+             metavar="<pnode>[:<snode>]",
+             completion_suggest=OPT_COMPL_INST_ADD_NODES),
   os_opt,
   cli_option("-B", "--backend", dest="beparams",
              type="keyval", default={},