From 504688ed70e63e052dd60c3c875914616ec2c81d Mon Sep 17 00:00:00 2001 From: trivernis Date: Fri, 16 Jun 2023 09:55:54 +0200 Subject: [PATCH] Add ntfy script --- cntfy.nu | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100755 cntfy.nu diff --git a/cntfy.nu b/cntfy.nu new file mode 100755 index 0000000..eb9f5a4 --- /dev/null +++ b/cntfy.nu @@ -0,0 +1,72 @@ +#!/usr/bin/env nu + +def main [ + subcommand: string@subcommands +] { + if $subcommand == "help" { + print $"cntfy ", + print "" + print "Subcommands: " + + for subcommand in ( subcommands ) { + print "" + print $"(ansi {attr: b})($subcommand)(ansi reset)" + print ( help $"main ($subcommand)" ) + } + } else { + let span = (metadata $subcommand).span; + error make { + msg: $"Unknown subcommand ($subcommand)", + label: { + text: "Unknown subcommand", + start: $span.start, + end: $span.end + } + } + } +} + +def subcommands [] { + ["publish"] +} + +# Publishes a message +export def `main publish` [ + topic: string + message: string + --title: string + --priority: string + --tags: string +] { + let headers = ( [] + | append [Title $title] + | append [Priority $priority] + | append [Tags $tags] + ) + + let cfg = ( get_config ) + http post -u $cfg.username -p $cfg.password -H $headers ( build_url $topic ) $message +} + +def get_config [] { + let cfg_path = ( $env.HOME | path join ".config/cntfy/config.toml" ) + + if ($cfg_path | path exists ) == false { + let span = (metadata $cfg_path).span + + error make { + msg: $"Config file ($cfg_path) does not exist", + label: { + text: "This file does not exist", + start: $span.start, + end: $span.end + } + } + } + + ( $cfg_path | open ) +} + +def build_url [topic: string] { + $"https://ntfy.trivernis.dev/($topic)" +} \ No newline at end of file