import glob import os import argparse from savefile import SaveFile # Spaghetti code to replace the shell script PROJECT_NAME = os.environ.get("PROJECT_NAME", "neo") files = glob.glob("docker-compose.*.yml") def setup_handler(what: str): types = { "def": lambda: os.system("sh ./utils/setup.sh"), "ports": lambda: os.system("sh ./utils/setup-ports.sh ./utils/rulelist.rules") }.get(what, lambda: print("Invalid setup type")) def dc(cmd: str): fopts = "" for f in files: fopts += f"-f {f} " # print(f"docker-compose {fopts}{cmd}") os.system(f"docker-compose -p {PROJECT_NAME} {fopts} {cmd}") def main(): global files ap = argparse.ArgumentParser() ap.add_argument("--savefile", help="The savefile to use", default=os.path.expanduser("~/.local/faulty/neo.save")) ap.add_argument("--del-savefile", help="Delete the savefile", action="store_true") ap.add_argument("--ignore-savefile", help="Ignore the savefile", action="store_true") # -x, --exclude: e.g. -x this -x that ap.add_argument("-x", "--exclude", help="Exclude a file from the list", action="append", default=[]) # -i, --include: e.g. -i this -i that ap.add_argument("-i", "--include", help="Include a file from the list", action="append", default=[]) ap.add_argument('--save', help="Save the list of files to the savefile", action="store_true") ap.add_argument('action', help="The action to perform on the files", nargs="?", default="up") ap.add_argument('args', help="Extra arguments for the action", nargs=argparse.REMAINDER) args = ap.parse_args() if args.del_savefile: os.remove(args.savefile) return if not args.ignore_savefile: savefile = SaveFile(args.savefile) else: savefile = SaveFile(os.path.expanduser("~/.local/faulty/neo.save")) # load the savefile if not args.ignore_savefile: files = savefile.get("files", files) if len(args.include) > 0: files = [] for i in args.include: files.append(f"docker-compose.{i}.yml") if len(args.exclude) > 0: for i in args.exclude: files.remove(f"docker-compose.{i}.yml") if args.save: savefile["files"] = files return def handle_aliases(fnlist: dict, name: str): return fnlist.get(name, lambda: print("does not exist")) fnlist = { "up": lambda: dc(f"up -d {' '.join(args.args)}"), "upr": lambda: dc(f"up -d --build {' '.join(args.args)}"), "logs": lambda: dc(f"logs --tail=20 -f {' '.join(args.args)}"), "setup": lambda: setup_handler(args.args[0]), "delete-project-files": lambda: os.system(f"sudo rm -rf /srv/{args.args[0]}"), "dpf": lambda: handle_aliases(fnlist, "delete-project-files")() } fnlist.get(args.action, lambda: dc(f"{args.action} {' '.join(args.args)}"))() if __name__ == "__main__": main()