From 113ee8258951028d54b798eaaa813982222c20e8 Mon Sep 17 00:00:00 2001 From: Juanra GM Date: Fri, 1 Dec 2023 14:43:18 +0100 Subject: [PATCH] feat(cli): add `ntfy` step --- .changeset/witty-bees-deliver.md | 5 ++++ packages/cli/src/Action/BackupAction.ts | 4 ++- packages/cli/src/Task/ScriptTask.ts | 11 +++++++ packages/cli/src/utils/steps.ts | 39 +++++++++++++++++++++---- 4 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 .changeset/witty-bees-deliver.md diff --git a/.changeset/witty-bees-deliver.md b/.changeset/witty-bees-deliver.md new file mode 100644 index 0000000..1f58db1 --- /dev/null +++ b/.changeset/witty-bees-deliver.md @@ -0,0 +1,5 @@ +--- +"@datatruck/cli": minor +--- + +Add `ntfy` step diff --git a/packages/cli/src/Action/BackupAction.ts b/packages/cli/src/Action/BackupAction.ts index 7a1e8f0..981e8bb 100644 --- a/packages/cli/src/Action/BackupAction.ts +++ b/packages/cli/src/Action/BackupAction.ts @@ -414,7 +414,9 @@ export class BackupAction { report.format ?? "list", ); await runSteps(report.run, { - vars: { dtt: { title: "DTT Backup", text, result } }, + vars: { + dtt: { title: "DTT Backup", text, result, success }, + }, verbose: this.options.verbose, }); }, diff --git a/packages/cli/src/Task/ScriptTask.ts b/packages/cli/src/Task/ScriptTask.ts index 1323134..129c3ae 100644 --- a/packages/cli/src/Task/ScriptTask.ts +++ b/packages/cli/src/Task/ScriptTask.ts @@ -36,12 +36,14 @@ export enum ScriptTaskDefinitionEnum { processStepConfig = "processStepConfig", nodeStepConfig = "nodeStepConfig", telegramMessageStepConfig = "telegramMessageStepConfig", + ntfyStepConfig = "ntfyStepConfig", } const stepTypes = { process: ScriptTaskDefinitionEnum.processStepConfig, node: ScriptTaskDefinitionEnum.nodeStepConfig, "telegram-message": ScriptTaskDefinitionEnum.telegramMessageStepConfig, + ntfy: ScriptTaskDefinitionEnum.ntfyStepConfig, }; export const scriptTaskName = "script"; @@ -115,6 +117,15 @@ export const scriptTaskDefinition: JSONSchema7 = { text: { type: "string" }, }, }, + ntfyStepConfig: { + type: "object", + required: ["token"], + properties: { + token: { type: "string" }, + topic: { type: "string" }, + text: { type: "string" }, + }, + }, }, type: "object", additionalProperties: false, diff --git a/packages/cli/src/utils/steps.ts b/packages/cli/src/utils/steps.ts index 14d32bf..5f84b16 100644 --- a/packages/cli/src/utils/steps.ts +++ b/packages/cli/src/utils/steps.ts @@ -25,6 +25,12 @@ export type TelegramMessageStepConfig = { text?: string; }; +export type NtfyStepConfig = { + token: string; + topic?: string; + text?: string; +}; + export type Step = | { type: "process"; @@ -37,6 +43,10 @@ export type Step = | { type: "telegram-message"; config: TelegramMessageStepConfig; + } + | { + type: "ntfy"; + config: NtfyStepConfig; }; export type StepOptions = { @@ -50,11 +60,12 @@ export type StepOptions = { export async function runSteps(input: Step[] | Step, options: StepOptions) { const steps = Array.isArray(input) ? input : [input]; + const vars = options?.vars || {}; for (const step of steps) { if (step.type === "process") { await exec( step.config.command, - (step.config.args || []).map((v) => render(v, options?.vars || {})), + (step.config.args || []).map((v) => render(v, vars)), { cwd: options.cwd, env: { @@ -81,12 +92,12 @@ export async function runSteps(input: Step[] | Step, options: StepOptions) { tempDir = await mkTmpDir("node-step"); } const scriptPath = join(tempDir, "script.js"); - const vars = { + const nodeVars = { ...step.config.vars, - ...options?.vars, + ...vars, }; - const varKeys = Object.keys(vars); - const varJson = JSON.stringify(vars); + const varKeys = Object.keys(nodeVars); + const varJson = JSON.stringify(nodeVars); const code = Array.isArray(step.config.code) ? [...step.config.code].join(";\n") : step.config.code; @@ -121,7 +132,7 @@ export async function runSteps(input: Step[] | Step, options: StepOptions) { `https://api.telegram.org/bot${step.config.bot}/sendMessage`, JSON.stringify({ chat_id: step.config.chatId.toString(), - text: render(step.config.text ?? `{dtt.text}`, options?.vars || {}), + text: render(step.config.text ?? `{dtt.text}`, vars), disable_notification: true, }), { @@ -130,6 +141,22 @@ export async function runSteps(input: Step[] | Step, options: StepOptions) { }, }, ); + } else if (step.type === "ntfy") { + const topic = [step.config.token, step.config.topic] + .filter(Boolean) + .join("-"); + if (topic.length < 32) + throw new Error(`Topic is less than 32 characters: ${topic}`); + await post( + `https://ntfy.sh/${topic}`, + render(step.config.text ?? `{dtt.text}`, vars), + { + headers: { + Title: render("{dtt.title}", vars), + Priority: vars.success ? "default" : "high", + }, + }, + ); } else { throw new Error(`Invalid step type: ${(step as any).type}`); }