Config init a bit better

This commit is contained in:
Marcel Otte 2021-10-18 22:36:54 +02:00
parent 53a230fb0a
commit d4fd1f4112
2 changed files with 30 additions and 1 deletions

View File

@ -8,6 +8,11 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
var (
config *Configuration
vconfig *viper.Viper
)
type Configuration struct { type Configuration struct {
Settings Settings `mapstructure:"settings"` Settings Settings `mapstructure:"settings"`
Devices Devices `mapstructure:"devices"` Devices Devices `mapstructure:"devices"`
@ -44,11 +49,25 @@ func Load() *Configuration {
var cfg *Configuration var cfg *Configuration
//Unmarshal all into Configuration type
err := vconfig.Unmarshal(cfg) err := vconfig.Unmarshal(cfg)
if err != nil { if err != nil {
fmt.Printf("Error occured when loading config: %v\n", err) fmt.Printf("Error occured when loading config: %v\n", err)
panic("No configuration available!") panic("No configuration available!")
} }
//Unmarshal all into Configuration type for k, v := range cfg.Backups {
v.Name = k
}
for k, v := range cfg.Devices {
v.Name = k
}
return cfg return cfg
} }
func Init() {
config = Load()
}
func Get() *Configuration {
return config
}

10
db/db.go Normal file
View File

@ -0,0 +1,10 @@
package db
import "encoding/json"
var database map[string]string
func Save() {
jsonstr := json.Marshal(database)
}