backive/db/db.go

33 lines
587 B
Go
Raw Normal View History

2021-10-18 22:36:54 +02:00
package db
2021-10-27 23:05:36 +02:00
import (
"encoding/json"
"os"
)
2021-10-18 22:36:54 +02:00
2021-10-27 23:17:18 +02:00
// Database is a simple string to string mapping, where arbitrary strings can be stored and safed to disk or loaded
2021-10-27 23:05:36 +02:00
var Database map[string]string
var path string = "/var/lib/backive/data.json"
2021-10-18 22:36:54 +02:00
2021-10-27 23:17:18 +02:00
// Save saves the database
2021-10-18 22:36:54 +02:00
func Save() {
2021-10-27 23:05:36 +02:00
jsonstr, merr := json.Marshal(Database)
if merr != nil {
panic(merr)
}
2021-10-18 22:36:54 +02:00
2021-10-27 23:05:36 +02:00
err := os.WriteFile(path, []byte(jsonstr), 0644)
if err != nil {
panic(err)
}
}
2021-10-27 23:17:18 +02:00
// Load loads the database
2021-10-27 23:05:36 +02:00
func Load() {
data, err := os.ReadFile(path)
if err != nil {
panic(err)
}
2021-10-27 23:17:18 +02:00
json.Unmarshal(data, &Database)
2021-10-18 22:36:54 +02:00
}