From 677de344726bc4919a9bc212df1c49d38bb5a855 Mon Sep 17 00:00:00 2001 From: Matthieu Huin Date: Mon, 31 May 2021 15:43:52 +0200 Subject: [PATCH] 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 --- .gitignore | 1 + doc/source/configuration.rst | 6 +++--- zuulclient/cmd/__init__.py | 20 ++++++++++++++++---- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 62e3a7a..8792da5 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ .testrepository .tox .venv +.vscode .coverage .stestr AUTHORS diff --git a/doc/source/configuration.rst b/doc/source/configuration.rst index 4b84784..584060b 100644 --- a/doc/source/configuration.rst +++ b/doc/source/configuration.rst @@ -3,15 +3,15 @@ Configuration ============= -The web client will look by default for a ``~/.zuul.conf`` file for its -configuration. The file should consist of a ``[webclient]`` section with at least +The web client will look by default for a ``$HOME/.config/zuul/client.conf`` or a ``$HOME/.zuul.conf`` +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 disable SSL verifications when connecting to Zuul (defaults to True). An 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. 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 :language: ini diff --git a/zuulclient/cmd/__init__.py b/zuulclient/cmd/__init__.py index e069c29..689122a 100644 --- a/zuulclient/cmd/__init__.py +++ b/zuulclient/cmd/__init__.py @@ -16,6 +16,7 @@ import argparse import configparser import logging import os +from pathlib import Path import shutil import sys import tempfile @@ -27,6 +28,12 @@ from zuulclient.utils import encrypt_with_openssl 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): pass @@ -35,7 +42,10 @@ class ZuulClient(): app_name = 'zuul-client' app_description = 'Zuul User CLI' 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): self.args = None @@ -69,7 +79,8 @@ class ZuulClient(): parser.add_argument('--use-config', dest='zuul_config', required=False, 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', required=False, action='store_false', @@ -153,7 +164,8 @@ class ZuulClient(): self.config.read(os.path.expanduser(fp)) return 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): config_args = dict( @@ -489,7 +501,7 @@ class ZuulClient(): else: raise Exception('Unable to find a way to connect to Zuul, ' 'provide the "--zuul-url" argument or set up a ' - '.zuul.conf file.') + 'zuul-client configuration file.') server = get_default(self.config, zuul_conf, 'url', None) verify = get_default(self.config, zuul_conf,