#!/usr/bin/python3

import sys
from yaml import load, dump

try:
    from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
    from yaml import Loader, Dumper

if __name__ == "__main__":
    config_file = sys.argv[1]
    git_url = sys.argv[2]

    with open(config_file, "r") as f:
        data = load(f, Loader=Loader)
        git_clone_cmd = "git clone {}".format(git_url)
        existing_plugin_hook = [
            x for x in data["hooks"]["after_code"] if git_clone_cmd in x["exec"]["cmd"]
        ]
        if not existing_plugin_hook:
            plugin_hook = {"exec": {"cd": "$home/plugins", "cmd": [git_clone_cmd]}}
            data["hooks"]["after_code"].append(plugin_hook)
        print(dump(data, Dumper=Dumper))
