Compare commits

..

3 Commits

Author SHA1 Message Date
84f4b11b9c added config file to the nix store 2023-08-15 21:51:59 +02:00
912b405511 fixed typo 2023-08-15 21:45:59 +02:00
307a8e9819 added config command line option 2023-08-15 21:45:47 +02:00
3 changed files with 35 additions and 10 deletions

View File

@ -27,7 +27,7 @@ The script looks for a config file at `$HOME/.config/dyn-gandi/config.log` or `/
}, },
"/path/to/a/file/containing/api_key": { "/path/to/a/file/containing/api_key": {
"example.at": [ "sub1" ], "example.at": [ "sub1" ],
"example.au": [ "sub1" "sub2" ] "example.au": [ "sub1", "sub2" ]
} }
}, },
"resolvers": [ "resolvers": [

View File

@ -6,11 +6,12 @@ from datetime import datetime
from typing import Callable, Any from typing import Callable, Any
def loadSettings() -> dict: def loadSettings(path: str | None) -> dict:
path = f"{os.path.expanduser('~')}/.config/dyn-gandi/config.json" if path is None:
path = f"{os.path.expanduser('~')}/.config/dyn-gandi/config.json"
if not os.path.exists(path): if not os.path.exists(path):
path = '/etc/dyn-gandi.json' path = '/etc/dyn-gandi.json'
if not os.path.exists(path): if not os.path.exists(path):
quit() 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()}") 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(): 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'] log_path, ttl = settings['log_path'], settings['ttl']
def logger(x: str) -> Any: return log(x, log_path) def logger(x: str) -> Any: return log(x, log_path)

View File

@ -8,6 +8,9 @@ inputs: {
cfg = config.dyn-gandi; cfg = config.dyn-gandi;
package = inputs.self.packages.${pkgs.stdenv.hostPlatform.system}.default; package = inputs.self.packages.${pkgs.stdenv.hostPlatform.system}.default;
inherit (lib) mkIf mkEnableOption mkOption types; inherit (lib) mkIf mkEnableOption mkOption types;
format = pkgs.formats.json {};
configFile = format.generate "config.json" cfg.settings;
in { in {
options.dyn-gandi = { options.dyn-gandi = {
enable = mkEnableOption "dyn-gandi"; enable = mkEnableOption "dyn-gandi";
@ -42,14 +45,14 @@ in {
environment = { environment = {
systemPackages = [package]; systemPackages = [package];
etc."dyn-gandi.json" = { # etc."dyn-gandi.json" = {
text = builtins.toJSON cfg.settings; # text = builtins.toJSON cfg.settings;
}; # };
}; };
systemd.services.dyn-gandi = mkIf (cfg.timer systemd.services.dyn-gandi = mkIf (cfg.timer
!= null) { != null) {
script = "${package}/bin/dyn_gandi"; # TODO: add config file via command line options script = "${package}/bin/dyn_gandi --config ${configFile}";
serviceConfig = { serviceConfig = {
Type = "oneshot"; Type = "oneshot";