From 307a8e9819e25c97c3d1520535167006db6e2949 Mon Sep 17 00:00:00 2001 From: Kristian Krsnik Date: Tue, 15 Aug 2023 21:45:47 +0200 Subject: [PATCH] added config command line option --- dyn_gandi/main.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/dyn_gandi/main.py b/dyn_gandi/main.py index bf9173e..b314977 100644 --- a/dyn_gandi/main.py +++ b/dyn_gandi/main.py @@ -6,11 +6,12 @@ from datetime import datetime from typing import Callable, Any -def loadSettings() -> dict: - path = f"{os.path.expanduser('~')}/.config/dyn-gandi/config.json" +def loadSettings(path: str | None) -> dict: + if path is None: + path = f"{os.path.expanduser('~')}/.config/dyn-gandi/config.json" - if not os.path.exists(path): - path = '/etc/dyn-gandi.json' + if not os.path.exists(path): + path = '/etc/dyn-gandi.json' if not os.path.exists(path): quit() @@ -99,8 +100,29 @@ def renewRecords(api: dict[str, dict[str, list[str]]], current_ip: str, log: Cal log(f"[ERROR][{domain}][{record}][{response.status_code}] {response.json()}") +def parseArgv(argv: list[str]) -> dict[str, list[str]]: + cli_options = {} + buffer = None + + for arg in argv: + if arg.startswith('--'): + cli_options[arg.removeprefix('--')] = [] + buffer = arg.removeprefix('--') + else: + cli_options[buffer].append(arg) + return cli_options + def main(): - settings = loadSettings() + argv = os.sys.argv + options = {} + if len(argv) > 1: + options = parseArgv(argv[1:]) + + config_path = None + if 'config' in options: + config_path = options['config'][0] + + settings = loadSettings(config_path) log_path, ttl = settings['log_path'], settings['ttl'] def logger(x: str) -> Any: return log(x, log_path)