Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions ghmoon
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ except:

MIN_ENQUEUE_INTERVAL = 30

def deep_merge(base, overlay):
"""Recursively merge overlay into base. Dicts merge key-wise; everything else is replaced."""
if isinstance(base, dict) and isinstance(overlay, dict):
out = dict(base)
for k, v in overlay.items():
out[k] = deep_merge(base.get(k), v) if k in base else v
return out
return overlay

gistgh = None
context = None
config = None
Expand Down Expand Up @@ -443,19 +452,20 @@ def init(args):
global repos
global wq

cfgfile = args.config
if not cfgfile:
cfgfiles = args.config
if not cfgfiles:
for src in (os.path.expanduser("~/.ghmoon/config.yaml"),
"/etc/ghmoon/config.yaml"):
try:
cfgfile = open(src, "r")
if os.path.exists(src):
cfgfiles = [src]
break
except:
pass

assert cfgfile, "No config specified, an no default found"
assert cfgfiles, "No config specified, and no default found"

config = yaml.load(cfgfile, Loader=yaml.FullLoader)
config = {}
for path in cfgfiles:
with open(path, "r") as f:
config = deep_merge(config, yaml.load(f, Loader=yaml.FullLoader) or {})
context = config.get("context", f"{getpass.getuser()}@{socket.gethostname()}")

gistgh = GH(token=config.get("gist", {}).get("token"))
Expand All @@ -470,7 +480,8 @@ def init(args):
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="ghmoon")
parser.add_argument("-f", "--config-file",
type=argparse.FileType("r"), dest="config")
action="append", dest="config", default=[],
help="Config file (repeat to layer; later files override earlier ones)")

sps = parser.add_subparsers(dest="cmd")

Expand Down