Merge branch 'devel-2.3'
[ganeti-local] / autotools / build-bash-completion
index 56204af..b046543 100755 (executable)
@@ -40,43 +40,6 @@ from ganeti import build
 from ganeti import _autoconf
 
 
-class ShellWriter:
-  """Helper class to write scripts with indentation.
-
-  """
-  INDENT_STR = "  "
-
-  def __init__(self, fh):
-    self._fh = fh
-    self._indent = 0
-
-  def IncIndent(self):
-    """Increase indentation level by 1.
-
-    """
-    self._indent += 1
-
-  def DecIndent(self):
-    """Decrease indentation level by 1.
-
-    """
-    assert self._indent > 0
-    self._indent -= 1
-
-  def Write(self, txt, *args):
-    """Write line to output file.
-
-    """
-    self._fh.write(self._indent * self.INDENT_STR)
-
-    if args:
-      self._fh.write(txt % args)
-    else:
-      self._fh.write(txt)
-
-    self._fh.write("\n")
-
-
 def WritePreamble(sw):
   """Writes the script preamble.
 
@@ -154,6 +117,15 @@ def WritePreamble(sw):
       sw.DecIndent()
     sw.Write("}")
 
+  sw.Write("_ganeti_nodegroup() {")
+  sw.IncIndent()
+  try:
+    nodegroups_path = os.path.join(constants.DATA_DIR, "ssconf_nodegroups")
+    sw.Write("cat %s 2>/dev/null || :", utils.ShellQuote(nodegroups_path))
+  finally:
+    sw.DecIndent()
+  sw.Write("}")
+
   # Params: <offset> <options with values> <options without values>
   # Result variable: $first_arg_idx
   sw.Write("_ganeti_find_first_arg() {")
@@ -312,6 +284,10 @@ class CompletionWriter:
       # Only static choices implemented so far (e.g. no node list)
       suggest = getattr(opt, "completion_suggest", None)
 
+      # our custom option type
+      if opt.type == "bool":
+        suggest = ["yes", "no"]
+
       if not suggest:
         suggest = opt.choices
 
@@ -357,6 +333,8 @@ 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_ONE_NODEGROUP:
+          WriteCompReply(sw, "-W \"$(_ganeti_nodegroup)\"", cur=cur)
         elif suggest == cli.OPT_COMPL_INST_ADD_NODES:
           sw.Write("local tmp= node1= pfx= curvalue=\"${optcur#*:}\"")
 
@@ -608,11 +586,14 @@ def GetCommands(filename, module):
   help_option = cli.cli_option("-h", "--help", default=False,
                                action="store_true")
 
-  for (_, _, optdef, _, _) in commands.itervalues():
+  for name, (_, _, optdef, _, _) in commands.items():
     if help_option not in optdef:
       optdef.append(help_option)
-    if cli.DEBUG_OPT not in optdef:
-      optdef.append(cli.DEBUG_OPT)
+    for opt in cli.COMMON_OPTS:
+      if opt in optdef:
+        raise Exception("Common option '%s' listed for command '%s' in %s" %
+                        (opt, name, filename))
+      optdef.append(opt)
 
   # Use aliases
   aliases = getattr(module, "aliases", {})
@@ -626,7 +607,7 @@ def GetCommands(filename, module):
 
 def main():
   buf = StringIO()
-  sw = ShellWriter(buf)
+  sw = utils.ShellWriter(buf)
 
   WritePreamble(sw)