From 6de02054f79a8dbda33cb92a25bf0f1ad2e8c084 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Sat, 17 Aug 2024 18:00:30 +0100 Subject: [PATCH] working on notifiers, need to work out how to have different notifier types --- norg/borg/borg.nim | 15 ++++++++++++++- norg/config/init.nim | 2 ++ norg/config/notifier_config.nim | 24 ++++++++++++++++++++++++ norg/model/config_type.nim | 4 ++-- norg/model/notifier_type.nim | 12 +++++++----- norg/model/notifier_types.nim | 7 +++++++ norg/model/state_type.nim | 14 ++++++++++++++ norg/model/uptimekuma_type.nim | 10 ++++++++++ norg/notifier/notifier.nim | 8 ++++++++ 9 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 norg/config/notifier_config.nim create mode 100644 norg/model/notifier_types.nim create mode 100644 norg/model/state_type.nim create mode 100644 norg/model/uptimekuma_type.nim create mode 100644 norg/notifier/notifier.nim diff --git a/norg/borg/borg.nim b/norg/borg/borg.nim index 7ea03ef..e9e123b 100644 --- a/norg/borg/borg.nim +++ b/norg/borg/borg.nim @@ -1,4 +1,6 @@ import ../model/config_type +import ../model/state_type +import ../notifier/notifier import strutils, sequtils import osproc 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) 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 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 = return run genCommand(cmd = "list", repo = repo.path, others = nc.args.others) diff --git a/norg/config/init.nim b/norg/config/init.nim index 3599ed4..0cfe2a3 100644 --- a/norg/config/init.nim +++ b/norg/config/init.nim @@ -1,6 +1,7 @@ import parsetoml import ../model/config_type +import notifier_config export config_type proc parseConfigFile*(file: string): NorgConfig = @@ -15,6 +16,7 @@ proc parseConfigFile*(file: string): NorgConfig = repo.label = rtable["label"].getStr() norg_config.repositories.add(repo) norg_config.setEncryptionPassword(in_conf{"encryption_password"}.getStr("")) + norg_config.notifiers = parseNotifiers(in_conf) return norg_config diff --git a/norg/config/notifier_config.nim b/norg/config/notifier_config.nim new file mode 100644 index 0000000..019522d --- /dev/null +++ b/norg/config/notifier_config.nim @@ -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 diff --git a/norg/model/config_type.nim b/norg/model/config_type.nim index 4ce7da8..4f54ff2 100644 --- a/norg/model/config_type.nim +++ b/norg/model/config_type.nim @@ -1,9 +1,9 @@ import repository_type -import notifier_type +import notifier_types import ../config/args export repository_type -export notifier_type +export notifier_types type diff --git a/norg/model/notifier_type.nim b/norg/model/notifier_type.nim index 2c53a41..dbc39d2 100644 --- a/norg/model/notifier_type.nim +++ b/norg/model/notifier_type.nim @@ -1,7 +1,9 @@ +import state_type -type - Notifier* = object - name*: string - - +type + Notifier* = object of RootObj + base_url*: string + states*: seq[State] +proc defaultStates*(): seq[State] = + return @[Success, Failure, Running] diff --git a/norg/model/notifier_types.nim b/norg/model/notifier_types.nim new file mode 100644 index 0000000..23156df --- /dev/null +++ b/norg/model/notifier_types.nim @@ -0,0 +1,7 @@ +import notifier_type +import uptimekuma_type + +export notifier_type +export uptimekuma_type + + diff --git a/norg/model/state_type.nim b/norg/model/state_type.nim new file mode 100644 index 0000000..0f76af9 --- /dev/null +++ b/norg/model/state_type.nim @@ -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 diff --git a/norg/model/uptimekuma_type.nim b/norg/model/uptimekuma_type.nim new file mode 100644 index 0000000..c41e2aa --- /dev/null +++ b/norg/model/uptimekuma_type.nim @@ -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" + diff --git a/norg/notifier/notifier.nim b/norg/notifier/notifier.nim new file mode 100644 index 0000000..b418fc6 --- /dev/null +++ b/norg/notifier/notifier.nim @@ -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)