Some Doc strings

This commit is contained in:
Marcel Otte 2021-10-27 23:17:18 +02:00
parent b5f3eec03a
commit a64be87c6f
4 changed files with 20 additions and 3 deletions

View File

@ -13,21 +13,26 @@ var (
vconfig *viper.Viper vconfig *viper.Viper
) )
// Configuration struct holding the settings and config items of devices and backups
type Configuration struct { type Configuration struct {
Settings Settings `mapstructure:"settings"` Settings Settings `mapstructure:"settings"`
Devices Devices `mapstructure:"devices"` Devices Devices `mapstructure:"devices"`
Backups Backups `mapstructure:"backups"` Backups Backups `mapstructure:"backups"`
} }
// Settings struct holds the global configuration items
type Settings struct { type Settings struct {
SystemMountPoint string `mapstructure:"systemMountPoint"` SystemMountPoint string `mapstructure:"systemMountPoint"`
UserMountPoint string `mapstructure:"userMountPoint"` UserMountPoint string `mapstructure:"userMountPoint"`
} }
// Devices is nothing else than a name to Device type mapping
type Devices map[string]core.Device type Devices map[string]core.Device
// Backups is nothing else than a name to Backup type mapping
type Backups map[string]core.Backup type Backups map[string]core.Backup
// CreateViper creates a viper instance for usage later
func CreateViper() *viper.Viper { func CreateViper() *viper.Viper {
vconfig := viper.New() vconfig := viper.New()
vconfig.SetConfigName("backive") vconfig.SetConfigName("backive")
@ -38,6 +43,7 @@ func CreateViper() *viper.Viper {
return vconfig return vconfig
} }
// Load loads the configuration from the disk
func Load() *Configuration { func Load() *Configuration {
vconfig := CreateViper() vconfig := CreateViper()
if err := vconfig.ReadInConfig(); err != nil { if err := vconfig.ReadInConfig(); err != nil {
@ -64,10 +70,12 @@ func Load() *Configuration {
return cfg return cfg
} }
// Init Initializes the configuration
func Init() { func Init() {
config = Load() config = Load()
} }
// Get returns the Configuration global variable
func Get() *Configuration { func Get() *Configuration {
return config return config
} }

View File

@ -7,8 +7,10 @@ type Device struct {
OwnerUser string `mapstructure:"owner,omitempty"` OwnerUser string `mapstructure:"owner,omitempty"`
} }
// Mount will mount a device
func (d Device) Mount() { func (d Device) Mount() {
} }
// Unmount will unmount a device
func (d Device) Unmount() { func (d Device) Unmount() {
} }

View File

@ -5,9 +5,11 @@ import (
"os" "os"
) )
// Database is a simple string to string mapping, where arbitrary strings can be stored and safed to disk or loaded
var Database map[string]string var Database map[string]string
var path string = "/var/lib/backive/data.json" var path string = "/var/lib/backive/data.json"
// Save saves the database
func Save() { func Save() {
jsonstr, merr := json.Marshal(Database) jsonstr, merr := json.Marshal(Database)
if merr != nil { if merr != nil {
@ -20,10 +22,11 @@ func Save() {
} }
} }
// Load loads the database
func Load() { func Load() {
data, err := os.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
panic(err) panic(err)
} }
json.Unmarshal(data, Database) json.Unmarshal(data, &Database)
} }

View File

@ -8,10 +8,12 @@ import (
"github.com/qwc/backive/db" "github.com/qwc/backive/db"
) )
// Runs contains the Data for the scheduler: mapping from backups to a list of timestamps of the last 10 backups
type Runs map[string][]time.Time type Runs map[string][]time.Time
var runs Runs var runs Runs
// Load loads the data from the json database
func Load() { func Load() {
runerr := json.Unmarshal([]byte(db.Database["runs"]), &runs) runerr := json.Unmarshal([]byte(db.Database["runs"]), &runs)
if runerr != nil { if runerr != nil {
@ -19,6 +21,7 @@ func Load() {
} }
} }
// Save saves the data into the json database
func Save() { func Save() {
str, err := json.Marshal(runs) str, err := json.Marshal(runs)
if err != nil { if err != nil {
@ -28,9 +31,10 @@ func Save() {
db.Database["runs"] = string(str) db.Database["runs"] = string(str)
} }
// ShouldRun Takes a backup key and returns a bool if a backup should run now.
func ShouldRun(backup string) bool { func ShouldRun(backup string) bool {
freq := config.Get().Backups[backup].Frequency backupdata := config.Get().Backups[backup]
freq := backupdata.Frequency
// calculate time difference from last run, return true if no run has taken place // calculate time difference from last run, return true if no run has taken place
if freq > 0 { if freq > 0 {
return true return true