#! /usr/bin/env python3
import sys
import yaml
from nomad import Nomad


def write_nomad():
    nomad = Nomad()
    with open("./ansible_playbooks/vars/nomad_vars.yml") as f:
        vars = yaml.load(f, yaml.CLoader)

        for path, items in vars.items():
            # ignore special path there for terraform
            if path == "nomad/oidc":
                continue

            print("path", path, "items", items)
            if items == "DELETE":
                print(nomad.variable.delete_variable(path))
            else:
                print(
                    nomad.variable.create_variable(
                        path,
                        {"Path": path, "Items": {k: str(v) for k, v in items.items()}},
                    )
                )


def print_sample():
    clean_vars = {}
    with open("./ansible_playbooks/vars/nomad_vars.yml") as f:
        vars = yaml.load(f, yaml.CLoader)

        for path, items in vars.items():
            if items == "DELETE":
                continue
            else:
                clean_vars[path] = {k: "VALUE" for k in items}

    print(yaml.dump(clean_vars))


def main():
    if len(sys.argv) > 1 and sys.argv[1] == "print":
        print_sample()
    else:
        write_nomad()


if __name__ == "__main__":
    main()