Skip to content

Commit

Permalink
feat(cli): add ntfy step
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Dec 1, 2023
1 parent 8e64409 commit 113ee82
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-bees-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@datatruck/cli": minor
---

Add `ntfy` step
4 changes: 3 additions & 1 deletion packages/cli/src/Action/BackupAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,9 @@ export class BackupAction<TRequired extends boolean = true> {
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,
});
},
Expand Down
11 changes: 11 additions & 0 deletions packages/cli/src/Task/ScriptTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down
39 changes: 33 additions & 6 deletions packages/cli/src/utils/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export type TelegramMessageStepConfig = {
text?: string;
};

export type NtfyStepConfig = {
token: string;
topic?: string;
text?: string;
};

export type Step =
| {
type: "process";
Expand All @@ -37,6 +43,10 @@ export type Step =
| {
type: "telegram-message";
config: TelegramMessageStepConfig;
}
| {
type: "ntfy";
config: NtfyStepConfig;
};

export type StepOptions = {
Expand All @@ -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: {
Expand All @@ -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;
Expand Down Expand Up @@ -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,
}),
{
Expand All @@ -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}`);
}
Expand Down

0 comments on commit 113ee82

Please sign in to comment.