diff --git a/cmd/backive/main.go b/cmd/backive/main.go index 50e8d8d..6a9fe0c 100644 --- a/cmd/backive/main.go +++ b/cmd/backive/main.go @@ -1,7 +1,14 @@ package main -import "fmt" +import ( + "fmt" + + "github.com/qwc/backive/config" +) func main() { fmt.Println("vim-go") + // load config + config.Load() + } diff --git a/config/config.go b/config/config.go index 79d019b..f481a10 100644 --- a/config/config.go +++ b/config/config.go @@ -1,34 +1,46 @@ package config -import "github.com/spf13/viper" +import ( + "fmt" + "github.com/qwc/backive/core" -type Device struct { - name string - uuid string - ownerUser string -} + "github.com/spf13/viper" +) -type Backup struct { - name string - targetDeviceName string - targetDir string - sourceDir string - scriptPath string - frequency int - exeUser string +type Configuration struct { + Settings Settings `mapstructure:"settings"` + Devices Devices `mapstructure:"devices"` + Backups Backups `mapstructure:"backups"` } type Settings struct { - systemMountPoint string - userMountPoint string + SystemMountPoint string `mapstructure:"systemMountPoint"` + UserMountPoint string `mapstructure:"userMountPoint"` } -type Devices map[string]Device +type Devices map[string]core.Device -type Backups map[string]Backup - -func loadDevice() { - v1 := viper.New() - v1.SetConfigName("devices") +type Backups map[string]core.Backup +func CreateViper() *viper.Viper { + vconfig := viper.New() + vconfig.SetConfigName("backive") + vconfig.SetConfigType("yaml") + vconfig.AddConfigPath("/etc/backive/") // system config + vconfig.AddConfigPath("$HOME/.backive/") + vconfig.AddConfigPath(".") + return vconfig +} + +func Load() *Configuration { + vconfig := CreateViper() + if err := vconfig.ReadInConfig(); err != nil { + if _, ok := err.(viper.ConfigFileNotFoundError); ok { + panic(fmt.Errorf("Fatal: No config file could be found!")) + } + panic(fmt.Errorf("Fatal error config file: %w \n", err)) + } + + //Unmarshal all into Configuration type + return nil } diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000..d8d34d0 --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,39 @@ +package config + +// test package for config + +import ( + "bytes" + "fmt" + "testing" +) + +func TestDummyConfig(t *testing.T) { + + v := CreateViper() + var yamlExample = []byte(` +settings: + systemMountPoint: /media/backive + userMountPoint: $HOME/.backive/mounts +devices: + my_device: + uuid: 98237459872398745987 + owner: +backups: + my_backup: + targetDevice: my_device + targetDir: backive_backup + sourceDir: /home/user123/stuff + scriptPath: /path/to/script + frequency: 7 #weekly +`) + v.ReadConfig(bytes.NewBuffer(yamlExample)) + var theConfig Configuration + err := v.Unmarshal(&theConfig) + if err != nil { + fmt.Errorf("Unable to decode into struct, %v \n", err) + panic("Failed!") + } + fmt.Printf("systemMountpoint is %v \n", theConfig) + +} diff --git a/core/backup.go b/core/backup.go index e69de29..52d0478 100644 --- a/core/backup.go +++ b/core/backup.go @@ -0,0 +1,12 @@ +package core + +// Backup contains all necessary information for executing a configured backup. +type Backup struct { + Name string `mapstructure:",omitempty"` + TargetDevice string `mapstructure:"targetDevice"` + TargetDir string `mapstructure:"targetDir"` + SourceDir string `mapstructure:"sourceDir"` + ScriptPath string `mapstructure:"scriptPath"` + Frequency int `mapstructure:"frequency"` + ExeUser string `mapstructure:"user,omitempty"` +} diff --git a/core/device.go b/core/device.go index e69de29..de86a8a 100644 --- a/core/device.go +++ b/core/device.go @@ -0,0 +1,14 @@ +package core + +// Device represents a device, with a name easy to remember and the UUID to identify it, optionally an owner. +type Device struct { + Name string `mapstructure:",omitempty"` + UUID string `mapstructure:"uuid"` + OwnerUser string `mapstructure:"owner,omitempty"` +} + +func (d Device) Mount() { +} + +func (d Device) Unmount() { +} diff --git a/core/events.go b/core/events.go index e69de29..9a8bc95 100644 --- a/core/events.go +++ b/core/events.go @@ -0,0 +1 @@ +package core diff --git a/core/scheduler.go b/core/scheduler.go index e69de29..9a8bc95 100644 --- a/core/scheduler.go +++ b/core/scheduler.go @@ -0,0 +1 @@ +package core diff --git a/go.mod b/go.mod index 3a527cc..2f868dc 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,6 @@ module github.com/qwc/backive + go 1.17 require (