diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-05-25 05:59:10 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-05-25 05:59:10 +0000 |
commit | 8fb1eba3225eacc9a56bd0422c2a3d323458b502 (patch) | |
tree | ab40e43794a681824a6c1a418cf7bfa67545444c | |
parent | f24c72538183736f63c9f5ca94f1d9045115bd07 (diff) | |
parent | ed048cc30906e4a4245d76662d6d492d96588d50 (diff) | |
download | ick-contractor-8fb1eba3225eacc9a56bd0422c2a3d323458b502.tar.gz |
Merge branch 'config' into 'master'
Add config file support
Closes #22
See merge request larswirzenius/contractor!18
-rwxr-xr-x | contractor | 35 |
1 files changed, 35 insertions, 0 deletions
@@ -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,35 @@ 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("-c", "--config") p.add_argument("-v", "--verbose", action="store_true") p.add_argument("--log", help="log to a file") @@ -678,6 +709,10 @@ def main(): build.set_defaults(func=cmd_build, **manager_defaults) args = p.parse_args() + if args.use_default_config: + load_default_config(args) + if args.config: + load_config(args.config, args) setup_logging(args) args.func(args) |