Add config library

main
trivernis 1 year ago
parent 5621b4733c
commit e8400a31d9
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG Key ID: DFFFCC2C7A02DB45

@ -1,6 +1,6 @@
#!/usr/bin/env nu #!/usr/bin/env nu
use ./cntfy.nu use ./lib/cntfy.nu
def main [ def main [
subcommand: string@subcommands subcommand: string@subcommands

@ -1,3 +1,5 @@
use ./config.nu
# Publishes a message # Publishes a message
export def publish [ export def publish [
topic: string topic: string
@ -8,7 +10,7 @@ export def publish [
--actions: list<any> --actions: list<any>
--icon: string --icon: string
] { ] {
let cfg = ( get_config ) let cfg = ( config read_file cntfy )
let request_body = ({ let request_body = ({
topic: $topic topic: $topic
@ -21,22 +23,3 @@ export def publish [
} | to json ) } | to json )
http put -u $cfg.username -p $cfg.password $cfg.url $request_body 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 )
}

@ -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
}
}
}
Loading…
Cancel
Save