Enable user config

Read both `/etc/rpkg/centpkg.conf` and `~/.config/rpkg/centpkg.conf`
config files, merging the results.  Also define a `--user-config` flag
to override the later config path, similar to `--config`.  This approach
is borrowed from fedpkg.

Fixes #23
This commit is contained in:
Carl George 2021-04-15 22:02:09 -05:00
parent 8c80cfef18
commit 5b6d175224

View file

@ -29,17 +29,22 @@ def main():
Centpkg main.
"""
# 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)
default_user_config_path = os.path.join(
os.path.expanduser('~'), '.config', 'rpkg', '%s.conf' % program_name)
# Setup an argparser and parse the known commands to get the config file
# - use the custom ArgumentParser class from pyrpkg.cli and disable
# argument abbreviation to ensure that --user will be not treated as
# --user-config
parser = pyrpkg.cli.ArgumentParser(add_help=False, allow_abbrev=False)
parser.add_argument('-C', '--config', help='Specify a config file to use',
default=f'/etc/rpkg/%s.conf' % program_name)
default='/etc/rpkg/%s.conf' % program_name)
parser.add_argument('--user-config', help='Specify a user config file to use',
default=default_user_config_path)
(args, other) = parser.parse_known_args()
@ -51,6 +56,7 @@ def main():
# Setup a configuration object and read config file data
config = ConfigParser.SafeConfigParser()
config.read(args.config)
config.read(args.user_config)
client = centpkg.cli.centpkgClient(config, name=program_name)
client.do_imports(site='centpkg')