From b5f3eec03a736f6026f6efa7b87f518997ea74af Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Wed, 27 Oct 2021 23:05:36 +0200 Subject: [PATCH] little restructuring --- .gitignore | 2 ++ core/events.go | 1 - core/scheduler.go | 1 - db/db.go | 25 ++++++++++++++++++++++--- events/events.go | 1 + scheduler/scheduler.go | 40 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 65 insertions(+), 5 deletions(-) delete mode 100644 core/events.go delete mode 100644 core/scheduler.go create mode 100644 events/events.go create mode 100644 scheduler/scheduler.go diff --git a/.gitignore b/.gitignore index 4794f82..0f08883 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,6 @@ # vendor/ example +*.vim + dist/ diff --git a/core/events.go b/core/events.go deleted file mode 100644 index 9a8bc95..0000000 --- a/core/events.go +++ /dev/null @@ -1 +0,0 @@ -package core diff --git a/core/scheduler.go b/core/scheduler.go deleted file mode 100644 index 9a8bc95..0000000 --- a/core/scheduler.go +++ /dev/null @@ -1 +0,0 @@ -package core diff --git a/db/db.go b/db/db.go index 0cd6270..520ecc6 100644 --- a/db/db.go +++ b/db/db.go @@ -1,10 +1,29 @@ package db -import "encoding/json" +import ( + "encoding/json" + "os" +) -var database map[string]string +var Database map[string]string +var path string = "/var/lib/backive/data.json" func Save() { - jsonstr := json.Marshal(database) + jsonstr, merr := json.Marshal(Database) + if merr != nil { + panic(merr) + } + err := os.WriteFile(path, []byte(jsonstr), 0644) + if err != nil { + panic(err) + } +} + +func Load() { + data, err := os.ReadFile(path) + if err != nil { + panic(err) + } + json.Unmarshal(data, Database) } diff --git a/events/events.go b/events/events.go new file mode 100644 index 0000000..b3adf69 --- /dev/null +++ b/events/events.go @@ -0,0 +1 @@ +package events diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go new file mode 100644 index 0000000..f9e055f --- /dev/null +++ b/scheduler/scheduler.go @@ -0,0 +1,40 @@ +package scheduler + +import ( + "encoding/json" + "time" + + "github.com/qwc/backive/config" + "github.com/qwc/backive/db" +) + +type Runs map[string][]time.Time + +var runs Runs + +func Load() { + runerr := json.Unmarshal([]byte(db.Database["runs"]), &runs) + if runerr != nil { + panic(runerr) + } +} + +func Save() { + str, err := json.Marshal(runs) + if err != nil { + panic(err) + } + + db.Database["runs"] = string(str) +} + +func ShouldRun(backup string) bool { + freq := config.Get().Backups[backup].Frequency + + // calculate time difference from last run, return true if no run has taken place + if freq > 0 { + return true + } + + return false +}