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
import "fmt"
import (
"fmt"
"github.com/qwc/backive/config"
)
func main() {
fmt.Println("vim-go")
// load config
config.Load()
}

View File

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

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
go 1.17
require (