From 41e085e218df6d794403eef7074280634bd7adcc Mon Sep 17 00:00:00 2001 From: Andrei Jiroh Halili Date: Mon, 23 Sep 2024 00:26:43 +0000 Subject: [PATCH] chore(scripts): improve dns-records helper script (as well as reuse constants for config paths) Signed-off-by: Andrei Jiroh Halili --- script/dns-records | 13 ++++++++++--- utils/dns-records.ts | 17 +++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/script/dns-records b/script/dns-records index 2d014f5..3e8b484 100755 --- a/script/dns-records +++ b/script/dns-records @@ -1,4 +1,6 @@ #!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 + if [[ $DEBUG != "" ]]; then set -x fi @@ -13,10 +15,15 @@ if [[ "$PWD" != "$GIT_ROOT" ]]; then fi if [ -z "${CI}" ]; then - dotenvx run -f .env -- deno run -A "$GIT_ROOT/utils/dns-records.ts" "$@" -else dotenvx run -f .env.ci -- deno run -A "$GIT_ROOT/utils/dns-records.ts" "$@" +else + dotenvx run -f .env -- deno run -A "$GIT_ROOT/utils/dns-records.ts" "$@" fi +exitcode=$? # shellcheck disable=SC2164 -cd "$OLDPWD" \ No newline at end of file +cd "$OLDPWD" + +if [[ $exitcode != "0" ]]; then + exit ${exitcode:-"1"} +fi \ No newline at end of file diff --git a/utils/dns-records.ts b/utils/dns-records.ts index 8df7823..66f3ca7 100755 --- a/utils/dns-records.ts +++ b/utils/dns-records.ts @@ -1,9 +1,13 @@ #!/usr/bin/env -S deno run -A +// SPDX-License-Identifier: MPL-2.0 + import {program} from "commander"; import which from "https://esm.sh/which@4.0.0" import * as path from "jsr:@std/path"; const gitRoot = path.fromFileUrl(new URL("..", import.meta.url).href) const whereCli = path.resolve(which.sync("pipenv")) +const configFile = `${gitRoot}octodns-config.yml` +const dnsRecordsYamlDir = `${gitRoot}dns` let stderrLog: string let stdoutLog: string @@ -12,7 +16,7 @@ program.description("a Deno script to manage DNS records easily over octodns") program.command("import") .aliases(["dump"]) - .description("Dump DNS records from") + .description("dump DNS records from DNS hosting providers") .argument("", "Domain name to extract DNS records from the provider API") .argument("[provider]", "DNS nameserver provider name", "cf") .action(async(domain: string, provider: string) => { @@ -28,9 +32,9 @@ program.command("import") "--", "octodns-dump", "--config-file", - `${gitRoot}dns/octodns-config.yml`, + configFile, "--output-dir", - `${gitRoot}dns` + dnsRecordsYamlDir ] args.push(addTrailingDot(domain),`${provider || "cf"}`) console.log(`trying to exec ${whereCli} ${args.join(" ")}`) @@ -67,11 +71,12 @@ program.command("import") program.command("plan") .aliases(["dry-run","check", "validate", ]) + .description("validate DNS record changes") .action(async(opts) => { if (Deno.env.get("CLOUDFLARE_TOKEN") == undefined) { throw new Error("Cloudflare API token missing, maybe forgot to set DOTENV_PRIVATE_KEY?") } - const args = ["run", "--", "octodns-sync", "--config-file", `${gitRoot}dns/octodns-config.yml`] + const args = ["run", "--", "octodns-sync", "--config-file", configFile] console.log(`trying to exec ${whereCli} ${args.join(" ")}`) const ops = new Deno.Command(whereCli, { args, @@ -106,14 +111,14 @@ program.command("plan") program.command("apply") .aliases(["deploy"]) - .description("Deploy DNS record changes") + .description("deploy DNS record changes") .option("-f, --force-apply, --force", "force apply to skip change threadshold checks in octodns") .action(async(opts) => { if (Deno.env.get("CLOUDFLARE_TOKEN") == undefined) { throw new Error("Cloudflare API token missing, maybe forgot to set DOTENV_PRIVATE_KEY?") } - const args = ["run", "--", "octodns-sync", "--config-file", `${gitRoot}dns/octodns-config.yml`, "--doit"] + const args = ["run", "--", "octodns-sync", "--config-file", configFile, "--doit"] if (opts.forceApply == true) { args.push("--force") -- 2.51.2