mirror of https://github.com/qwc/backive.git
More and more stuff
This commit is contained in:
parent
0ea3276fc3
commit
924bbf134d
|
@ -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()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
|
||||||
|
}
|
|
@ -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"`
|
||||||
|
}
|
|
@ -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() {
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
package core
|
|
@ -0,0 +1 @@
|
||||||
|
package core
|
Loading…
Reference in New Issue