More and more stuff

This commit is contained in:
Marcel Otte 2021-10-12 23:00:24 +02:00
parent 0ea3276fc3
commit 924bbf134d
8 changed files with 110 additions and 23 deletions

View File

@ -1,7 +1,14 @@
package main package main
import "fmt" import (
"fmt"
"github.com/qwc/backive/config"
)
func main() { func main() {
fmt.Println("vim-go") fmt.Println("vim-go")
// load config
config.Load()
} }

View File

@ -1,34 +1,46 @@
package config package config
import "github.com/spf13/viper" import (
"fmt"
"github.com/qwc/backive/core"
type Device struct { "github.com/spf13/viper"
name string )
uuid string
ownerUser string
}
type Backup struct { type Configuration struct {
name string Settings Settings `mapstructure:"settings"`
targetDeviceName string Devices Devices `mapstructure:"devices"`
targetDir string Backups Backups `mapstructure:"backups"`
sourceDir string
scriptPath string
frequency int
exeUser string
} }
type Settings struct { type Settings struct {
systemMountPoint string SystemMountPoint string `mapstructure:"systemMountPoint"`
userMountPoint string UserMountPoint string `mapstructure:"userMountPoint"`
} }
type Devices map[string]Device type Devices map[string]core.Device
type Backups map[string]Backup type Backups map[string]core.Backup
func loadDevice() {
v1 := viper.New()
v1.SetConfigName("devices")
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
} }

39
config/config_test.go Normal file
View File

@ -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)
}

View File

@ -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"`
}

View File

@ -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() {
}

View File

@ -0,0 +1 @@
package core

View File

@ -0,0 +1 @@
package core

1
go.mod
View File

@ -1,5 +1,6 @@
module github.com/qwc/backive module github.com/qwc/backive
go 1.17 go 1.17
require ( require (