feat: argparse

This commit is contained in:
Daniel Langbein 2023-06-28 16:15:46 +02:00
parent 699d909e2d
commit 93b20ba4dc

View File

@ -1,8 +1,8 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import argparse
import ipaddress import ipaddress
import json import json
import sys
from functools import lru_cache from functools import lru_cache
from pathlib import Path from pathlib import Path
from typing import Type from typing import Type
@ -17,13 +17,8 @@ def main():
""" """
The main effort is done by https://github.com/nbuchwitz/nc_dnsapi The main effort is done by https://github.com/nbuchwitz/nc_dnsapi
""" """
if len(sys.argv) > 1: args = parse_args()
cfg_dir = Path(sys.argv[1]) cfg_dir: Path = args.cfg_dir
else:
cfg_dir = Path('/etc/netcup-dns')
if not cfg_dir.exists():
raise Exception(f'The config directory is missing: {cfg_dir}')
cfg_files = [file for file in cfg_dir.iterdir() if file.name.endswith('.json') and file.is_file()] cfg_files = [file for file in cfg_dir.iterdir() if file.name.endswith('.json') and file.is_file()]
for cfg_file in cfg_files: for cfg_file in cfg_files:
@ -59,6 +54,19 @@ def main():
print(f'The {hostname}.{domain} {type_} record points already to {destination}') print(f'The {hostname}.{domain} {type_} record points already to {destination}')
def parse_args():
parser = argparse.ArgumentParser(description='Update DNS A/AAAA records with your current external IP address'
' using the netcup DNS API.')
parser.add_argument('--config-directory',
help='Path to directory where `.json` config files reside.',
dest='cfg_dir',
default=Path('/etc/netcup-dns'),
type=Path)
args = parser.parse_args()
if not args.cfg_dir.exists():
raise Exception(f'The given config directory does not exist: {args.cfg_dir}')
def update_record_destination(api: Client, domain: str, hostname: str, type_: str, destination: str) -> bool: def update_record_destination(api: Client, domain: str, hostname: str, type_: str, destination: str) -> bool:
""" """
Updates the `destination` of the DNS record identified by `domain`, `hostname` and `type`. Updates the `destination` of the DNS record identified by `domain`, `hostname` and `type`.