diff --git a/config/config.go b/config/config.go index 2f1e934..0330473 100644 --- a/config/config.go +++ b/config/config.go @@ -13,21 +13,26 @@ var ( vconfig *viper.Viper ) +// Configuration struct holding the settings and config items of devices and backups type Configuration struct { Settings Settings `mapstructure:"settings"` Devices Devices `mapstructure:"devices"` Backups Backups `mapstructure:"backups"` } +// Settings struct holds the global configuration items type Settings struct { SystemMountPoint string `mapstructure:"systemMountPoint"` UserMountPoint string `mapstructure:"userMountPoint"` } +// Devices is nothing else than a name to Device type mapping type Devices map[string]core.Device +// Backups is nothing else than a name to Backup type mapping type Backups map[string]core.Backup +// CreateViper creates a viper instance for usage later func CreateViper() *viper.Viper { vconfig := viper.New() vconfig.SetConfigName("backive") @@ -38,6 +43,7 @@ func CreateViper() *viper.Viper { return vconfig } +// Load loads the configuration from the disk func Load() *Configuration { vconfig := CreateViper() if err := vconfig.ReadInConfig(); err != nil { @@ -64,10 +70,12 @@ func Load() *Configuration { return cfg } +// Init Initializes the configuration func Init() { config = Load() } +// Get returns the Configuration global variable func Get() *Configuration { return config } diff --git a/core/device.go b/core/device.go index de86a8a..d21ce91 100644 --- a/core/device.go +++ b/core/device.go @@ -7,8 +7,10 @@ type Device struct { OwnerUser string `mapstructure:"owner,omitempty"` } +// Mount will mount a device func (d Device) Mount() { } +// Unmount will unmount a device func (d Device) Unmount() { } diff --git a/db/db.go b/db/db.go index 520ecc6..c79c137 100644 --- a/db/db.go +++ b/db/db.go @@ -5,9 +5,11 @@ import ( "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 path string = "/var/lib/backive/data.json" +// Save saves the database func Save() { jsonstr, merr := json.Marshal(Database) if merr != nil { @@ -20,10 +22,11 @@ func Save() { } } +// Load loads the database func Load() { data, err := os.ReadFile(path) if err != nil { panic(err) } - json.Unmarshal(data, Database) + json.Unmarshal(data, &Database) } diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go index f9e055f..1dd2b1b 100644 --- a/scheduler/scheduler.go +++ b/scheduler/scheduler.go @@ -8,10 +8,12 @@ import ( "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 var runs Runs +// Load loads the data from the json database func Load() { runerr := json.Unmarshal([]byte(db.Database["runs"]), &runs) if runerr != nil { @@ -19,6 +21,7 @@ func Load() { } } +// Save saves the data into the json database func Save() { str, err := json.Marshal(runs) if err != nil { @@ -28,9 +31,10 @@ func Save() { 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 { - 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 if freq > 0 { return true