From 9a68048f5b6e6c5cce0c9ea452925dfb51c52a86 Mon Sep 17 00:00:00 2001 From: LunarEclipse Date: Sun, 13 Aug 2023 21:21:28 +0200 Subject: [PATCH 1/5] Added the ability to replace the config file with environment variables --- porkbun_ddns/cli.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/porkbun_ddns/cli.py b/porkbun_ddns/cli.py index f4f6bf5..b96702a 100644 --- a/porkbun_ddns/cli.py +++ b/porkbun_ddns/cli.py @@ -1,5 +1,6 @@ import argparse import sys +import os import traceback import logging from porkbun_ddns import PorkbunDDNS, PorkbunDDNS_Error @@ -52,6 +53,21 @@ def main(argv=sys.argv[1:]): args = parser.parse_args(argv) + if args.config == "-": + try: + config = { + "apikey": os.environ["PORKBUN_APIKEY"], + "secretapikey": os.environ["PORKBUN_SECRETAPIKEY"], + } + endpoint = os.environ.get("PORKBUN_DDNS_ENDPOINT"), + if endpoint is not None: + config["endpoint"] = endpoint # type: ignore + except KeyError as e: + print("Invalid config environment variables.") + raise e + else: + config = args.config + ipv4 = args.ipv4_only ipv6 = args.ipv6_only if not any([ipv4, ipv6]): @@ -63,7 +79,7 @@ def main(argv=sys.argv[1:]): handler.setLevel(logging.DEBUG) try: - porkbun_ddns = PorkbunDDNS(config=args.config, domain=args.domain, + porkbun_ddns = PorkbunDDNS(config=config, domain=args.domain, public_ips=args.public_ips, fritzbox_ip=args.fritzbox, ipv4=ipv4, ipv6=ipv6) if args.subdomain: From b7a480482ddb320446125a6a43079e908962db33 Mon Sep 17 00:00:00 2001 From: LunarEclipse Date: Sun, 13 Aug 2023 22:08:40 +0200 Subject: [PATCH 2/5] support for multiple subdomains --- porkbun_ddns/cli.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/porkbun_ddns/cli.py b/porkbun_ddns/cli.py index b96702a..6667ff2 100644 --- a/porkbun_ddns/cli.py +++ b/porkbun_ddns/cli.py @@ -22,9 +22,9 @@ def main(argv=sys.argv[1:]): parser.add_argument("config", help="Path to config file") parser.add_argument("domain", help="Domain to be updated") - subdomain = parser.add_mutually_exclusive_group() - subdomain.add_argument('subdomain', nargs='?', - default=None, help="Subdomain") + subdomains = parser.add_mutually_exclusive_group() + subdomains.add_argument('subdomains', nargs='*', + default=None, help="Subdomain(s)") public_ips = parser.add_mutually_exclusive_group() public_ips.add_argument('-i', '--public-ips', nargs='*', @@ -82,9 +82,12 @@ def main(argv=sys.argv[1:]): porkbun_ddns = PorkbunDDNS(config=config, domain=args.domain, public_ips=args.public_ips, fritzbox_ip=args.fritzbox, ipv4=ipv4, ipv6=ipv6) - if args.subdomain: - porkbun_ddns.set_subdomain(args.subdomain) - porkbun_ddns.update_records() + if args.subdomains: + for s in args.subdomains: + porkbun_ddns.set_subdomain(s) + porkbun_ddns.update_records() + else: + porkbun_ddns.update_records() except PorkbunDDNS_Error as e: logger.error("Error: " + str(e)) except Exception as e: From 779b4b03f7b75ebb7721c2413b90fb54843b6868 Mon Sep 17 00:00:00 2001 From: Nils Stein Date: Sat, 7 Oct 2023 17:46:38 +0200 Subject: [PATCH 3/5] Minor changes --- porkbun_ddns/cli.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/porkbun_ddns/cli.py b/porkbun_ddns/cli.py index 6667ff2..4d6cef0 100644 --- a/porkbun_ddns/cli.py +++ b/porkbun_ddns/cli.py @@ -59,12 +59,9 @@ def main(argv=sys.argv[1:]): "apikey": os.environ["PORKBUN_APIKEY"], "secretapikey": os.environ["PORKBUN_SECRETAPIKEY"], } - endpoint = os.environ.get("PORKBUN_DDNS_ENDPOINT"), - if endpoint is not None: - config["endpoint"] = endpoint # type: ignore + config["endpoint"] = os.environ.get("PORKBUN_DDNS_ENDPOINT", "https://porkbun.com/api/json/v3") except KeyError as e: - print("Invalid config environment variables.") - raise e + logger.error("Invalid config environment variables.") else: config = args.config From ed0287816ce85a3bad2de10e280e6805233f5398 Mon Sep 17 00:00:00 2001 From: Nils Stein Date: Sat, 7 Oct 2023 17:46:51 +0200 Subject: [PATCH 4/5] Updated ReadMe --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index dec66b8..2ea47c7 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,15 @@ Examples: ```shell $ porkbun-ddns "./config.json" domain.com my_subdomain +# Multiple subdomains: +$ porkbun-ddns "./config.json" domain.com my_subdomain_1 my_subdomain_2 my_subdomain_3 + +# Get config from environment variable: +# PORKBUN_APIKEY +# PORKBUN_SECRETAPIKEY +# PORKBUN_DDNS_ENDPOINT (Optional) +$ porkbun-ddns - domain.com my_subdomain + # Set IP's explicit $ porkbun-ddns "./config.json" domain.com my_subdomain -i '1.2.3.4' '1234:abcd:0:4567::8900' From 39f8eb3cfc868ef408e8e7ff046d3be15e0ce23a Mon Sep 17 00:00:00 2001 From: Nils Stein Date: Sat, 7 Oct 2023 17:48:45 +0200 Subject: [PATCH 5/5] ruff fixes --- porkbun_ddns/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/porkbun_ddns/cli.py b/porkbun_ddns/cli.py index 4d6cef0..374c58a 100644 --- a/porkbun_ddns/cli.py +++ b/porkbun_ddns/cli.py @@ -60,7 +60,7 @@ def main(argv=sys.argv[1:]): "secretapikey": os.environ["PORKBUN_SECRETAPIKEY"], } config["endpoint"] = os.environ.get("PORKBUN_DDNS_ENDPOINT", "https://porkbun.com/api/json/v3") - except KeyError as e: + except KeyError: logger.error("Invalid config environment variables.") else: config = args.config