summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2020-05-25 08:45:37 +0300
committerLars Wirzenius <liw@liw.fi>2020-05-25 08:45:37 +0300
commit2d02801fbcd2fbdc7e7e95a1c10a9ef66cb1cbe4 (patch)
treec398c9f05a389bf6a5d1950d3c044cc88fd53146
parentf24c72538183736f63c9f5ca94f1d9045115bd07 (diff)
downloadick-contractor-2d02801fbcd2fbdc7e7e95a1c10a9ef66cb1cbe4.tar.gz
feat: add -C option to read a default config file
-rwxr-xr-xcontractor32
1 files changed, 32 insertions, 0 deletions
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)