From 53a230fb0a91c4398332112e215394435fb79b55 Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Mon, 18 Oct 2021 21:02:10 +0200 Subject: [PATCH] Another small meterstone (no milestone... :P) --- cmd/backive_udev/main.go | 38 ++++++++++++++++++++++++++++++++++++-- config/config.go | 10 +++++++++- config/config_test.go | 26 +++++++++++++++----------- 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/cmd/backive_udev/main.go b/cmd/backive_udev/main.go index 50e8d8d..6c7c9cf 100644 --- a/cmd/backive_udev/main.go +++ b/cmd/backive_udev/main.go @@ -1,7 +1,41 @@ package main -import "fmt" +import ( + "encoding/json" + "fmt" + "log" + "net" + "os" + "strings" +) 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)) } diff --git a/config/config.go b/config/config.go index f481a10..f946e3c 100644 --- a/config/config.go +++ b/config/config.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "github.com/qwc/backive/core" "github.com/spf13/viper" @@ -41,6 +42,13 @@ func Load() *Configuration { 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 - return nil + return cfg } diff --git a/config/config_test.go b/config/config_test.go index d8d34d0..47a1ef4 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -6,34 +6,38 @@ import ( "bytes" "fmt" "testing" + + "github.com/spf13/viper" ) func TestDummyConfig(t *testing.T) { - v := CreateViper() + v := viper.New() + v.SetConfigType("yaml") var yamlExample = []byte(` settings: systemMountPoint: /media/backive userMountPoint: $HOME/.backive/mounts devices: - my_device: - uuid: 98237459872398745987 - owner: + 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 + 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) + fmt.Printf("Unable to decode into struct, %v \n", err) panic("Failed!") } fmt.Printf("systemMountpoint is %v \n", theConfig) + fmt.Printf("System Mountpoint is %v\n", v.Get("settings.systemmountpoint")) }