mirror of https://github.com/qwc/backive.git
Another small meterstone (no milestone... :P)
This commit is contained in:
parent
924bbf134d
commit
53a230fb0a
|
@ -1,7 +1,41 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("vim-go")
|
f, err := os.OpenFile("/tmp/backive/udev.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error creating logfile!")
|
||||||
|
panic("no logfile no info")
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
log.SetOutput(f)
|
||||||
|
|
||||||
|
env := map[string]string{}
|
||||||
|
|
||||||
|
for _, e := range os.Environ() {
|
||||||
|
pair := strings.SplitN(e, "=", 2)
|
||||||
|
env[pair[0]] = pair[1]
|
||||||
|
log.Println("%s", e)
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := net.Dial("unix", "/tmp/backive/backive.sock")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("Could not instantiate unix socket. Aborting")
|
||||||
|
}
|
||||||
|
jsonstr, err := json.Marshal(env)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("Could not convert to json. Aborting")
|
||||||
|
}
|
||||||
|
c.Write(jsonstr)
|
||||||
|
defer c.Close()
|
||||||
|
log.Printf("Sent %d bytes to unix socket.\n", len(jsonstr))
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/qwc/backive/core"
|
"github.com/qwc/backive/core"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
@ -41,6 +42,13 @@ func Load() *Configuration {
|
||||||
panic(fmt.Errorf("Fatal error config file: %w \n", err))
|
panic(fmt.Errorf("Fatal error config file: %w \n", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cfg *Configuration
|
||||||
|
|
||||||
|
err := vconfig.Unmarshal(cfg)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error occured when loading config: %v\n", err)
|
||||||
|
panic("No configuration available!")
|
||||||
|
}
|
||||||
//Unmarshal all into Configuration type
|
//Unmarshal all into Configuration type
|
||||||
return nil
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,34 +6,38 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDummyConfig(t *testing.T) {
|
func TestDummyConfig(t *testing.T) {
|
||||||
|
|
||||||
v := CreateViper()
|
v := viper.New()
|
||||||
|
v.SetConfigType("yaml")
|
||||||
var yamlExample = []byte(`
|
var yamlExample = []byte(`
|
||||||
settings:
|
settings:
|
||||||
systemMountPoint: /media/backive
|
systemMountPoint: /media/backive
|
||||||
userMountPoint: $HOME/.backive/mounts
|
userMountPoint: $HOME/.backive/mounts
|
||||||
devices:
|
devices:
|
||||||
my_device:
|
my_device:
|
||||||
uuid: 98237459872398745987
|
uuid: 98237459872398745987
|
||||||
owner:
|
owner:
|
||||||
backups:
|
backups:
|
||||||
my_backup:
|
my_backup:
|
||||||
targetDevice: my_device
|
targetDevice: my_device
|
||||||
targetDir: backive_backup
|
targetDir: backive_backup
|
||||||
sourceDir: /home/user123/stuff
|
sourceDir: /home/user123/stuff
|
||||||
scriptPath: /path/to/script
|
scriptPath: /path/to/script
|
||||||
frequency: 7 #weekly
|
frequency: 7 #weekly
|
||||||
`)
|
`)
|
||||||
v.ReadConfig(bytes.NewBuffer(yamlExample))
|
v.ReadConfig(bytes.NewBuffer(yamlExample))
|
||||||
var theConfig Configuration
|
var theConfig Configuration
|
||||||
err := v.Unmarshal(&theConfig)
|
err := v.Unmarshal(&theConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Errorf("Unable to decode into struct, %v \n", err)
|
fmt.Printf("Unable to decode into struct, %v \n", err)
|
||||||
panic("Failed!")
|
panic("Failed!")
|
||||||
}
|
}
|
||||||
fmt.Printf("systemMountpoint is %v \n", theConfig)
|
fmt.Printf("systemMountpoint is %v \n", theConfig)
|
||||||
|
|
||||||
|
fmt.Printf("System Mountpoint is %v\n", v.Get("settings.systemmountpoint"))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue