forked from OpenPDU/openpdu
79 lines
2.2 KiB
Go
79 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
// Config definition
|
|
type Config struct {
|
|
hostname string `mapstructure:"hostname"`
|
|
boards []*Board `flow,mapstructure:"boards"`
|
|
}
|
|
|
|
// Configuration container
|
|
var Configuration Config
|
|
|
|
func init() {
|
|
viper.SetConfigName("openpdu") // name of config file (without extension)
|
|
viper.SetConfigType("yaml") // type of config file
|
|
viper.AddConfigPath("/etc/openpdu/") // path to look for the config file in
|
|
viper.AddConfigPath(".") // optionally look for config in the working directory
|
|
err := viper.ReadInConfig() // Find and read the config file
|
|
if err != nil { // Handle errors reading the config file
|
|
log.Printf("Fatal error config file: %s \n", err)
|
|
}
|
|
|
|
viper.SetDefault("hostname", "openpdu1")
|
|
br := viper.Get("boards").([]interface{})
|
|
for k, v := range br {
|
|
log.Printf("Key: %v", k)
|
|
log.Printf(" Value: %v", v)
|
|
var ba Board
|
|
viper.UnmarshalKey("boards."+string(k), ba)
|
|
log.Printf(" Valueid: %v", ba.ID)
|
|
}
|
|
|
|
log.Printf("basdo: %v", viper.Get("boards[0]"))
|
|
log.Printf("todo: %v", viper.AllSettings())
|
|
|
|
Configuration := Config{}
|
|
err = viper.Unmarshal(&Configuration)
|
|
if err != nil {
|
|
fmt.Printf("unable to decode into config struct, %v", err)
|
|
}
|
|
|
|
viper.WriteConfigAs("openpdu_out.yaml")
|
|
|
|
log.Printf("All settings - hostname: %v \n", Configuration.hostname)
|
|
for idx, b := range Configuration.boards {
|
|
log.Printf("b %v: %v (%v)", idx, b.Name, b.Type)
|
|
|
|
}
|
|
// var b []Board
|
|
// var brd Board
|
|
// b = viper.Get("boards").([]Board)
|
|
// for brd := range viper.Get("boards") {
|
|
// log.Printf("brd: %v", brd)
|
|
// }
|
|
// log.Printf("bo: %v", viper.Get("boards"))
|
|
// log.Printf("bo: %v", b)
|
|
log.Printf("cfg: %v", Configuration)
|
|
log.Printf("cfgb: %v", Configuration.boards)
|
|
}
|
|
|
|
func writeConfig() {
|
|
viper.SetConfigName("openpdu") // name of config file (without extension)
|
|
viper.SetConfigType("yaml")
|
|
viper.AddConfigPath("/etc/openpdu/") // path to look for the config file in
|
|
viper.SetDefault("hostname", "openpdu")
|
|
err := viper.WriteConfig()
|
|
if err != nil { // Handle errors reading the config file
|
|
log.Printf("Fatato error config file: %s \n", err)
|
|
}
|
|
log.Printf("ok credo \n")
|
|
|
|
}
|