#22 dynamic cli with no need for sig extra checks
Closed 5 months ago by carlwgeorge. Opened 5 months ago by lrossett.
centos/ lrossett/centpkg progname  into  develop

file modified
+1 -1

@@ -16,5 +16,5 @@ 

  from centpkg.__main__ import main

  

  if __name__ == "__main__":

-     main(sig=False)

+     main()

  

file modified
+1 -1

@@ -17,5 +17,5 @@ 

  from centpkg.__main__ import main

  

  if __name__ == "__main__":

-     main(sig=True)

+     main()

  

file modified
+5 -13

@@ -24,27 +24,22 @@ 

  import centpkg.cli

  

  

- def main(sig):

+ def main():

      """

      Centpkg main.

  

-     Params:

-       sig: Flag to switch between centpkg-sig and centpkg.

      """

      # Setup an argparser and parse the known commands to get the config file

      program_name = os.path.basename(sys.argv[0])

  

      # Modified ArgumentParser provides parameter "allow_abbrev=False" (which affects processing

      # of commandline arguments with common prefix). Generaly it is available since python3.6.

+     

      # This enables "allow_abbrev" for older python versions.

      parser = pyrpkg.cli.ArgumentParser(add_help=False)

-     if sig:

-         parser.add_argument('-C', '--config', help='Specify a config file to use',

-                             default='/etc/rpkg/centpkg-sig.conf')

-     else:

-         parser.add_argument('-C', '--config', help='Specify a config file to use',

-                             default='/etc/rpkg/centpkg.conf')

  

+     parser.add_argument('-C', '--config', help='Specify a config file to use',

+                             default=f'/etc/rpkg/%s.conf' % program_name)

  

      (args, other) = parser.parse_known_args()

  

@@ -57,10 +52,7 @@ 

      config = ConfigParser.SafeConfigParser()

      config.read(args.config)

  

-     if sig:

-         client = centpkg.cli.centpkgClientSig(config)

-     else:

-         client = centpkg.cli.centpkgClient(config)

+     client = centpkg.cli.centpkgClient(config, name=program_name)

      client.do_imports(site='centpkg')

      client.parse_cmdline()

  

file modified
+12 -17

@@ -20,12 +20,17 @@ 

  

  from centpkg.utils import config_get_safely, do_add_remote, do_fork

  from pyrpkg.cli import cliClient

+ from pyrpkg import rpkgError

  from six.moves.urllib_parse import urlparse

  

  

+ _DEFAULT_API_BASE_URL = 'https://gitlab.com'

+ 

+ 

  class centpkgClient(cliClient):

-     def __init__(self, config, name=None):

-         self.DEFAULT_CLI_NAME = 'centpkg'

+     def __init__(self, config, name='centpkg'):

+         self.DEFAULT_CLI_NAME = name

+ 

          super(centpkgClient, self).__init__(config, name)

  

          self.setup_centos_subparsers()

@@ -36,7 +41,11 @@ 

      def register_do_fork(self):

          help_msg = 'Create a new fork of the current repository'

          distgit_section = '{0}.distgit'.format(self.name)

-         distgit_api_base_url = config_get_safely(self.config, distgit_section, "apibaseurl")

+         # uses default dist-git url in case section is not present

+         try:

+             distgit_api_base_url = config_get_safely(self.config, distgit_section, "apibaseurl")

+         except rpkgError:

+             distgit_api_base_url = _DEFAULT_API_BASE_URL

          description = textwrap.dedent('''

              Create a new fork of the current repository

  

@@ -115,17 +124,3 @@ 

          else:

              msg = "Remote with name '{0}' already exists."

          self.log.info(msg.format(self.cmd.user))

- 

- 

- class centpkgClientSig(cliClient):

-     def __init__(self, config, name=None):

-         self.DEFAULT_CLI_NAME = 'centpkg-sig'

-         super(centpkgClientSig, self).__init__(config, name)

- 

-         self.setup_centos_subparsers()

- 

-     def setup_centos_subparsers(self):

-         self.register_parser()

- 

-     def register_parser(self):

-         pass

Some minor changes in the CLI which loads config files based on progname which also allows the usage of symlinks to target different config files.

Changes

  • removed sig bool flag
  • CLI is more dynamic using "progname" instead of sig checks
  • Sets a default dist-git api url in case none was provided (it was raising an exception) - I didn't know if it should show an error message or use a default one.

Pull-Request has been closed by carlwgeorge

5 months ago