working on notifiers, need to work out how to have different notifier types

This commit is contained in:
Paul Wilde 2024-08-17 18:00:30 +01:00
parent 55687965d1
commit 6de02054f7
9 changed files with 88 additions and 8 deletions

View file

@ -1,4 +1,6 @@
import ../model/config_type import ../model/config_type
import ../model/state_type
import ../notifier/notifier
import strutils, sequtils import strutils, sequtils
import osproc import osproc
import times import times
@ -39,9 +41,20 @@ proc initRepo(nc: NorgConfig, repo: Repository): int =
return runDiscard genCommand(cmd = "init", repo = repo.path, others = nc.args.others) return runDiscard genCommand(cmd = "init", repo = repo.path, others = nc.args.others)
proc backupSources(nc: NorgConfig, repo: Repository): int = proc backupSources(nc: NorgConfig, repo: Repository): int =
if nc.notifiers.len > 0:
notify(nc.notifiers, state=Running)
let others = concat(nc.source_dirs, nc.args.others) let others = concat(nc.source_dirs, nc.args.others)
let archivename = repo.path & "::" & genArchiveName() let archivename = repo.path & "::" & genArchiveName()
return run genCommand(cmd = "create", repo = archivename, others = others) let res = run genCommand(cmd = "create", repo = archivename, others = others)
if nc.notifiers.len > 0:
case res
of 0:
notify(nc.notifiers, state=Success)
of 1:
notify(nc.notifiers, state=Failure)
else:
notify(nc.notifiers, state=Failure)
proc listArchives(nc: NorgConfig, repo: Repository): int = proc listArchives(nc: NorgConfig, repo: Repository): int =
return run genCommand(cmd = "list", repo = repo.path, others = nc.args.others) return run genCommand(cmd = "list", repo = repo.path, others = nc.args.others)

View file

@ -1,6 +1,7 @@
import parsetoml import parsetoml
import ../model/config_type import ../model/config_type
import notifier_config
export config_type export config_type
proc parseConfigFile*(file: string): NorgConfig = proc parseConfigFile*(file: string): NorgConfig =
@ -15,6 +16,7 @@ proc parseConfigFile*(file: string): NorgConfig =
repo.label = rtable["label"].getStr() repo.label = rtable["label"].getStr()
norg_config.repositories.add(repo) norg_config.repositories.add(repo)
norg_config.setEncryptionPassword(in_conf{"encryption_password"}.getStr("")) norg_config.setEncryptionPassword(in_conf{"encryption_password"}.getStr(""))
norg_config.notifiers = parseNotifiers(in_conf)
return norg_config return norg_config

View file

@ -0,0 +1,24 @@
import parsetoml
import ../model/notifier_types
import ../model/state_type
proc parseUptimeKumaConf(conf: TomlValueRef): UptimeKuma =
var uk = UptimeKuma()
uk.base_url = conf{"base_url"}.getStr()
uk.states = @[]
for state in conf{"states"}.getElems():
uk.states.add(state.getStr().toState())
if uk.states.len == 0:
uk.states = defaultStates()
return uk
proc parseNotifiers*(in_conf: TomlValueRef): seq[Notifier] =
var notifiers: seq[Notifier] = @[]
if in_conf.hasKey("uptimekuma"):
let u = parseUptimeKumaConf(in_conf["uptimekuma"])
echo u
notifiers.add(u)
echo notifiers[0]
return notifiers

View file

@ -1,9 +1,9 @@
import repository_type import repository_type
import notifier_type import notifier_types
import ../config/args import ../config/args
export repository_type export repository_type
export notifier_type export notifier_types
type type

View file

@ -1,7 +1,9 @@
import state_type
type type
Notifier* = object Notifier* = object of RootObj
name*: string base_url*: string
states*: seq[State]
proc defaultStates*(): seq[State] =
return @[Success, Failure, Running]

View file

@ -0,0 +1,7 @@
import notifier_type
import uptimekuma_type
export notifier_type
export uptimekuma_type

14
norg/model/state_type.nim Normal file
View file

@ -0,0 +1,14 @@
type
State* = enum
Unknown = "Unknown"
Running = "Running"
Success = "Success"
Failure = "Failure"
proc toState*(str: string): State =
case str
of "Running": return Running
of "Success": return Success
of "Failure": return Failure
else: return Unknown

View file

@ -0,0 +1,10 @@
import state_type
import notifier_type
type
UptimeKuma* = object of Notifier
extra*: int
proc send_notify*(uk: Notifier, state: State): int {.discardable.} =
echo "Would send to UptimeKuma here"

View file

@ -0,0 +1,8 @@
import ../model/state_type
import ../model/notifier_types
proc notify*(notifiers: seq[Notifier], state: State): int {.discardable.} =
for n in notifiers:
if state in n.states:
echo "Sending notification to " & n.base_url
n.send_notify(state)