Add support for XDG-compliant config file

zuul-client will also look by default for the
$XDG_CONFIG_HOME/zuul/client.conf file when looking for user settings.

Change-Id: I60d59fceb1d4e4d767ca9e952a8ecc1630eba0a8
This commit is contained in:
Matthieu Huin 2021-05-31 15:43:52 +02:00
parent a7ca41220c
commit 677de34472
3 changed files with 20 additions and 7 deletions

1
.gitignore vendored
View File

@ -9,6 +9,7 @@
.testrepository .testrepository
.tox .tox
.venv .venv
.vscode
.coverage .coverage
.stestr .stestr
AUTHORS AUTHORS

View File

@ -3,15 +3,15 @@
Configuration Configuration
============= =============
The web client will look by default for a ``~/.zuul.conf`` file for its The web client will look by default for a ``$HOME/.config/zuul/client.conf`` or a ``$HOME/.zuul.conf``
configuration. The file should consist of a ``[webclient]`` section with at least file for its configuration. The file should consist of a ``[webclient]`` section with at least
the ``url`` attribute set. The optional ``verify_ssl`` can be set to False to the ``url`` attribute set. The optional ``verify_ssl`` can be set to False to
disable SSL verifications when connecting to Zuul (defaults to True). An disable SSL verifications when connecting to Zuul (defaults to True). An
authentication token can also be stored in the configuration file under the attribute authentication token can also be stored in the configuration file under the attribute
``auth_token`` to avoid passing the token in the clear on the command line. ``auth_token`` to avoid passing the token in the clear on the command line.
A default tenant can also be set with the ``tenant`` attribute. A default tenant can also be set with the ``tenant`` attribute.
Here is an example of a ``.zuul.conf`` file that can be used with zuul-client: Here is an example of a configuration file that can be used with zuul-client:
.. literalinclude:: /examples/.zuul.conf .. literalinclude:: /examples/.zuul.conf
:language: ini :language: ini

View File

@ -16,6 +16,7 @@ import argparse
import configparser import configparser
import logging import logging
import os import os
from pathlib import Path
import shutil import shutil
import sys import sys
import tempfile import tempfile
@ -27,6 +28,12 @@ from zuulclient.utils import encrypt_with_openssl
from zuulclient.utils import formatters from zuulclient.utils import formatters
_HOME = Path(os.path.expandvars('$HOME'))
_XDG_CONFIG_HOME = Path(os.environ.get(
'XDG_CONFIG_HOME',
_HOME / '.config'))
class ArgumentException(Exception): class ArgumentException(Exception):
pass pass
@ -35,7 +42,10 @@ class ZuulClient():
app_name = 'zuul-client' app_name = 'zuul-client'
app_description = 'Zuul User CLI' app_description = 'Zuul User CLI'
log = logging.getLogger("zuul-client") log = logging.getLogger("zuul-client")
default_config_locations = ['~/.zuul.conf'] default_config_locations = [
_XDG_CONFIG_HOME / 'zuul' / 'client.conf',
_HOME / '.zuul.conf'
]
def __init__(self): def __init__(self):
self.args = None self.args = None
@ -69,7 +79,8 @@ class ZuulClient():
parser.add_argument('--use-config', dest='zuul_config', parser.add_argument('--use-config', dest='zuul_config',
required=False, required=False,
default=None, default=None,
help='A predefined configuration in .zuul.conf') help='A predefined configuration in the '
'zuul-client configuration file')
parser.add_argument('--insecure', dest='verify_ssl', parser.add_argument('--insecure', dest='verify_ssl',
required=False, required=False,
action='store_false', action='store_false',
@ -153,7 +164,8 @@ class ZuulClient():
self.config.read(os.path.expanduser(fp)) self.config.read(os.path.expanduser(fp))
return return
raise ArgumentException( raise ArgumentException(
"Unable to locate config file in %s" % locations) "Unable to locate config "
"file in %s" % ', '.join([x.as_posix() for x in locations]))
def setup_logging(self): def setup_logging(self):
config_args = dict( config_args = dict(
@ -489,7 +501,7 @@ class ZuulClient():
else: else:
raise Exception('Unable to find a way to connect to Zuul, ' raise Exception('Unable to find a way to connect to Zuul, '
'provide the "--zuul-url" argument or set up a ' 'provide the "--zuul-url" argument or set up a '
'.zuul.conf file.') 'zuul-client configuration file.')
server = get_default(self.config, server = get_default(self.config,
zuul_conf, 'url', None) zuul_conf, 'url', None)
verify = get_default(self.config, zuul_conf, verify = get_default(self.config, zuul_conf,