]> Frank Brehm's Git Trees - pixelpark/puppet-tools.git/commitdiff
Re-arranging commandline arguments for 'get-module-info'.
authorFrank Brehm <frank.brehm@pixelpark.com>
Mon, 13 Feb 2023 10:29:59 +0000 (11:29 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Mon, 13 Feb 2023 10:29:59 +0000 (11:29 +0100)
lib/dpx_puppettools/app/get_forge_module.py

index 88554f3048559211c26a090ad300b153c81ceed3..851598f8a2999d054378a77a5e8463d4b1819b09 100644 (file)
@@ -9,11 +9,15 @@ from __future__ import absolute_import
 
 # Standard modules
 import logging
+import os
+
+from pathlib import Path
 
 # Third party modules
+from fb_tools.argparse_actions import DirectoryOptionAction
 
 # Own modules
-from .. import pp
+from .. import pp, DEFAULT_VAR_DIR
 
 from ..xlate import XLATOR
 
@@ -26,7 +30,7 @@ LOG = logging.getLogger(__name__)
 _ = XLATOR.gettext
 ngettext = XLATOR.ngettext
 
-__version__ = '0.3.0'
+__version__ = '0.4.0'
 
 
 # =============================================================================
@@ -43,9 +47,15 @@ class GetForgeModuleApplication(BaseDPXPuppetApplication):
     # -------------------------------------------------------------------------
     def __init__(self, appname=None, verbose=0, base_dir=None, initialized=False):
 
+        euid = os.geteuid()
+
+        self.read_cache = True
         self.write_cache = True
+        if euid != 0:
+            self.write_cache = False
         self.module_name = None
         self.module_info = None
+        self.var_dir = None
 
         desc = _(
             "This application retrieves information about the given module "
@@ -64,13 +74,45 @@ class GetForgeModuleApplication(BaseDPXPuppetApplication):
 
         forge_group = self.arg_parser.add_argument_group(_('Options for Puppet forge operations'))
 
+        read_cache_group = forge_group.add_mutually_exclusive_group()
+
+        help_txt = _("Read the forge information from a possibly existing cache file (default).")
+        read_cache_group.add_argument(
+            '-r', '--read-cache', dest="read_cache", action="store_true", help=help_txt)
+
+        help_txt = _(
+            "Don't read the forge information from a existing cache file, "
+            "get it always straight from Puppet forge.")
+        read_cache_group.add_argument(
+            '-R', '--no-read-cache', dest="read_cache", action="store_false", help=help_txt)
+
+        write_cache_group = forge_group.add_mutually_exclusive_group()
+
+        help_txt = _("Write the cache file after retrieving the infos from Puppet forge")
+        if self.write_cache:
+            help_txt += ' ' + _("(default)")
+        help_txt += '.'
+        write_cache_group.add_argument(
+            '-w', '--write-cache', dest='write_cache', action="store_true", help=help_txt)
+
+        help_txt = _("Don't write the cache file after retrieving the infos from Puppet forge")
+        if not self.write_cache:
+            help_txt += ' ' + _("(default)")
+        help_txt += '.'
+        write_cache_group.add_argument(
+            '-W', '--no-write-cache', dest='write_cache', action="store_false", help=help_txt)
+
+        help_txt = _(
+            "The directory containing variable data from DPX puppet tools, Default: {vd!r}. "
+            "Cache files for the forge information are read and written from the sub "
+            "directory {sd!r} below it.").format(vd=str(DEFAULT_VAR_DIR), sd='forge')
         forge_group.add_argument(
-            '-n', '--no-write', dest='no_write', action="store_true",
-            help=_("Don't write the cache file after retrieving the infos from Puppet forge."),
+            '-D', '--dir', '--var-dir', dest='var_dir', metavar=_('DIRECTORY'),
+            action=DirectoryOptionAction, must_exists=True, help=help_txt,
         )
 
         forge_group.add_argument(
-            'module_name', nargs=1,
+            'module_name', metavar=_('PUPPET-MODULE'), nargs=1,
             help=_("The name of the module, which data should be retrieved from Puppet forge."),
         )
 
@@ -84,14 +126,36 @@ class GetForgeModuleApplication(BaseDPXPuppetApplication):
 
         super(GetForgeModuleApplication, self).post_init()
 
-        if getattr(self.args, 'no_write', False):
-            self.write_cache = False
+        read_cache = getattr(self.args, 'read_cache', None)
+        if read_cache is not None:
+            if read_cache:
+                self.read_cache = True
+            else:
+                self.read_cache = False
+
+        write_cache = getattr(self.args, 'write_cache', None)
+        if write_cache is not None:
+            if write_cache:
+                self.write_cache = True
+            else:
+                self.write_cache = False
+
+        var_dir = getattr(self.args, 'var_dir', None)
+        if var_dir:
+            if self.wite_cache:
+                if not os.access(str(var_dir), os.W_OK):
+                    msg = _("The given var directory {!r} is not writeable.").format(
+                        str(var_dir))
+                    LOG.error(msg)
+                    self.exit(1)
+        else:
+            var_dir = DEFAULT_VAR_DIR
 
         self.module_name = self.args.module_name[0]
 
         self.module_info = ForgeModuleInfo(
             appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
-            full_name=self.module_name, initialized=True,)
+            full_name=self.module_name, var_dir=var_dir, initialized=True,)
 
     # -------------------------------------------------------------------------
     def _run(self):