From e8400a31d90594c0930a9a465e1af6a47ee864d1 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 17 Jun 2023 12:33:44 +0200 Subject: [PATCH] Add config library --- cntfy | 2 +- cntfy.nu => lib/cntfy.nu | 25 ++++--------------------- lib/config.nu | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 22 deletions(-) rename cntfy.nu => lib/cntfy.nu (51%) create mode 100644 lib/config.nu diff --git a/cntfy b/cntfy index 32d931f..c63038f 100755 --- a/cntfy +++ b/cntfy @@ -1,6 +1,6 @@ #!/usr/bin/env nu -use ./cntfy.nu +use ./lib/cntfy.nu def main [ subcommand: string@subcommands diff --git a/cntfy.nu b/lib/cntfy.nu similarity index 51% rename from cntfy.nu rename to lib/cntfy.nu index cd8fcda..9a28fb5 100755 --- a/cntfy.nu +++ b/lib/cntfy.nu @@ -1,3 +1,5 @@ +use ./config.nu + # Publishes a message export def publish [ topic: string @@ -8,7 +10,7 @@ export def publish [ --actions: list --icon: string ] { - let cfg = ( get_config ) + let cfg = ( config read_file cntfy ) let request_body = ({ topic: $topic @@ -20,23 +22,4 @@ export def publish [ icon: $icon } | to json ) http put -u $cfg.username -p $cfg.password $cfg.url $request_body -} - -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 ) -} +} \ No newline at end of file diff --git a/lib/config.nu b/lib/config.nu new file mode 100644 index 0000000..a571608 --- /dev/null +++ b/lib/config.nu @@ -0,0 +1,22 @@ +# reads a config file +export def read_file [app: string, filename = "config.toml"] { + get_path $app | path join $filename | open +} + +# returns the path to an apps config dir +export def get_path [app: string] { + let local = ( $env.HOME | path join ".config" | path join $app ) + + if ( $local | path exists ) { + $local + } else { + let etc_cfg = ( "/etc" | path join $app ) + + if ( $etc_cfg | path exists ) { + $etc_cfg + } else { + mkdir $local + $local + } + } +} \ No newline at end of file