157 lines
5.2 KiB
Python
Executable File
157 lines
5.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
_merge_config.py — Merges Rocket.Chat settings into ~/.hermes/config.yaml.
|
|
|
|
Adds/updates ALL entries needed:
|
|
- plugins.enabled: rocketchat-platform
|
|
- gateway.platforms.rocketchat: enabled, extra.base_url, home_channel
|
|
- toolsets: rocketchat
|
|
|
|
Safe: preserves existing structure, comments, and formatting.
|
|
Only touches rocketchat-related keys.
|
|
"""
|
|
|
|
import argparse
|
|
import os
|
|
import sys
|
|
|
|
try:
|
|
import yaml
|
|
except ImportError:
|
|
print("ERROR: pyyaml required. Run: pip3 install --user pyyaml", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
|
|
def load_config(path):
|
|
"""Load YAML config, return empty dict if file missing/broken."""
|
|
if not os.path.exists(path):
|
|
return {}
|
|
try:
|
|
with open(path) as f:
|
|
return yaml.safe_load(f) or {}
|
|
except Exception:
|
|
return {}
|
|
|
|
|
|
def save_config(path, data):
|
|
"""Write config preserving basic structure. Keeps existing file intact if possible."""
|
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
with open(path, "w") as f:
|
|
yaml.safe_dump(data, f, default_flow_style=False, allow_unicode=True, sort_keys=False)
|
|
# Post-process: ensure proper blank lines between top-level sections
|
|
_reformat_sections(path)
|
|
|
|
|
|
def _reformat_sections(path):
|
|
"""Add blank lines between top-level YAML keys for readability."""
|
|
with open(path) as f:
|
|
lines = f.readlines()
|
|
|
|
out = []
|
|
prev_was_top_key = False
|
|
for line in lines:
|
|
stripped = line.rstrip()
|
|
is_top_key = bool(stripped) and not stripped[0].isspace() and ":" in stripped
|
|
|
|
if is_top_key and out and prev_was_top_key:
|
|
out.append("\n")
|
|
elif is_top_key and out:
|
|
out.append("\n")
|
|
|
|
out.append(stripped + "\n")
|
|
prev_was_top_key = is_top_key
|
|
|
|
with open(path, "w") as f:
|
|
f.writelines(out)
|
|
|
|
|
|
def remove_rocketchat(config):
|
|
"""Remove all rocketchat-related entries from config (in-place)."""
|
|
# plugins.enabled
|
|
if "plugins" in config and "enabled" in config["plugins"]:
|
|
config["plugins"]["enabled"] = [
|
|
p for p in config["plugins"]["enabled"] if p != "rocketchat-platform"
|
|
]
|
|
# gateway.platforms.rocketchat
|
|
if "gateway" in config and "platforms" in config["gateway"]:
|
|
config["gateway"]["platforms"].pop("rocketchat", None)
|
|
# toolsets
|
|
if "toolsets" in config and isinstance(config["toolsets"], list):
|
|
config["toolsets"] = [t for t in config["toolsets"] if t != "rocketchat"]
|
|
# known_plugin_toolsets.cli
|
|
if "known_plugin_toolsets" in config and "cli" in config["known_plugin_toolsets"]:
|
|
config["known_plugin_toolsets"]["cli"] = [
|
|
t for t in config["known_plugin_toolsets"]["cli"] if t != "rocketchat"
|
|
]
|
|
|
|
|
|
def merge_config(config, base_url, user, password, room_id, room_name):
|
|
"""Merge Rocket.Chat settings into the config dict (in-place)."""
|
|
|
|
# ── 1. plugins.enabled ──
|
|
plugins = config.setdefault("plugins", {})
|
|
enabled_plugins = plugins.setdefault("enabled", [])
|
|
if "rocketchat-platform" not in enabled_plugins:
|
|
enabled_plugins.append("rocketchat-platform")
|
|
|
|
# ── 2. gateway.platforms.rocketchat ──
|
|
gateway = config.setdefault("gateway", {})
|
|
platforms = gateway.setdefault("platforms", {})
|
|
rc = platforms.setdefault("rocketchat", {})
|
|
rc["enabled"] = True
|
|
|
|
extra = rc.setdefault("extra", {})
|
|
extra["base_url"] = base_url
|
|
|
|
if not rc.get("home_channel") or not rc["home_channel"].get("chat_id"):
|
|
hc = {"platform": "rocketchat"}
|
|
if room_id:
|
|
hc["chat_id"] = room_id
|
|
if room_name:
|
|
hc["name"] = room_name
|
|
rc["home_channel"] = hc
|
|
|
|
# ── 3. toolsets (agent-level) ──
|
|
toolsets = config.setdefault("toolsets", [])
|
|
if "rocketchat" not in toolsets:
|
|
# Don't insert if it's a complex structure (dict of lists)
|
|
if all(isinstance(t, str) for t in toolsets):
|
|
toolsets.append("rocketchat")
|
|
|
|
# ── 4. known_plugin_toolsets.cli ──
|
|
# Some Hermes versions auto-manage this; add if missing
|
|
known = config.setdefault("known_plugin_toolsets", {})
|
|
cli_ts = known.setdefault("cli", [])
|
|
if "rocketchat" not in cli_ts:
|
|
if all(isinstance(t, str) for t in cli_ts):
|
|
cli_ts.append("rocketchat")
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="Merge Rocket.Chat config into Hermes config.yaml")
|
|
parser.add_argument("--config", required=True, help="Path to ~/.hermes/config.yaml")
|
|
parser.add_argument("--base-url", default="")
|
|
parser.add_argument("--user", default="")
|
|
parser.add_argument("--password", default="")
|
|
parser.add_argument("--room-id", default="")
|
|
parser.add_argument("--room-name", default="")
|
|
parser.add_argument("--remove", action="store_true", help="Remove rocketchat entries")
|
|
args = parser.parse_args()
|
|
|
|
if args.remove:
|
|
config = load_config(args.config)
|
|
remove_rocketchat(config)
|
|
save_config(args.config, config)
|
|
print("OK (removed)")
|
|
return
|
|
|
|
config = load_config(args.config)
|
|
merge_config(config, args.base_url, args.user, args.password, args.room_id, args.room_name)
|
|
save_config(args.config, config)
|
|
|
|
print("OK")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|