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