From 2d02801fbcd2fbdc7e7e95a1c10a9ef66cb1cbe4 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 25 May 2020 08:45:37 +0300 Subject: feat: add -C option to read a default config file --- contractor | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/contractor b/contractor index d5ef494..4b195d7 100755 --- a/contractor +++ b/contractor @@ -14,6 +14,10 @@ from subprocess import PIPE, STDOUT import yaml +# Default configuration files. +DEFAULT_CONFIGS = {os.path.expanduser("~/.config/contractor/config.yaml")} + + # The device in the manager VM for the workspace disk. WS_DEV = "/dev/vdb" @@ -648,8 +652,34 @@ def setup_logging(args): logger.setLevel(logging.DEBUG) +def load_default_config(args): + for filename in DEFAULT_CONFIGS: + if os.path.exists(filename): + load_config(filename, args) + + +def load_config(filename, args): + with open(filename) as f: + config = yaml.safe_load(f) + + keys = { + "manager_address": None, + "manager_port": None, + "manager_user": None, + "verbose": None, + "log": os.path.expanduser, + } + for key in keys: + if key in config: + func = keys[key] + if func is None: + func = lambda x: x + setattr(args, key, func(config[key])) + + def main(): p = argparse.ArgumentParser() + p.add_argument("-C", "--use-default-config", action="store_true") p.add_argument("-v", "--verbose", action="store_true") p.add_argument("--log", help="log to a file") @@ -678,6 +708,8 @@ def main(): build.set_defaults(func=cmd_build, **manager_defaults) args = p.parse_args() + if args.use_default_config: + load_default_config(args) setup_logging(args) args.func(args) -- cgit v1.2.1