Another small meterstone (no milestone... :P)

This commit is contained in:
Marcel Otte 2021-10-18 21:02:10 +02:00
parent 924bbf134d
commit 53a230fb0a
3 changed files with 60 additions and 14 deletions

View File

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

View File

@ -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))
} }
//Unmarshal all into Configuration type var cfg *Configuration
return nil
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 cfg
} }

View File

@ -6,11 +6,14 @@ 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
@ -31,9 +34,10 @@ backups:
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"))
} }