diff options
Diffstat (limited to 'config/serve.go')
-rw-r--r-- | config/serve.go | 49 |
1 files changed, 31 insertions, 18 deletions
diff --git a/config/serve.go b/config/serve.go index ef9b0ae0..6de993b8 100644 --- a/config/serve.go +++ b/config/serve.go @@ -1,8 +1,8 @@ package config import ( - "github.com/BurntSushi/toml" log "github.com/Sirupsen/logrus" + "github.com/spf13/viper" "reflect" ) @@ -11,41 +11,54 @@ type BgpConfigSet struct { Policy RoutingPolicy } -func ReadConfigfileServe(path string, configCh chan BgpConfigSet, reloadCh chan bool) { +func ReadConfigfileServe(path, format string, configCh chan BgpConfigSet, reloadCh chan bool) { cnt := 0 for { <-reloadCh b := Bgp{} p := RoutingPolicy{} - md, err := toml.DecodeFile(path, &b) - if err == nil { - err = SetDefaultConfigValues(md, &b) - if err == nil { - _, err = toml.DecodeFile(path, &p) - } + v := viper.New() + v.SetConfigFile(path) + v.SetConfigType(format) + err := v.ReadInConfig() + if err != nil { + goto ERROR } - + err = v.Unmarshal(&b) if err != nil { - if cnt == 0 { - log.Fatal("can't read config file ", path, ", ", err) - } else { - log.Warning("can't read config file ", path, ", ", err) - continue - } + goto ERROR + } + err = SetDefaultConfigValues(v, &b) + if err != nil { + goto ERROR + } + err = v.Unmarshal(&p) + if err != nil { + goto ERROR } + if cnt == 0 { log.Info("finished reading the config file") } cnt++ - bgpConfig := BgpConfigSet{Bgp: b, Policy: p} - configCh <- bgpConfig + configCh <- BgpConfigSet{Bgp: b, Policy: p} + continue + + ERROR: + if cnt == 0 { + log.Fatal("can't read config file ", path, ", ", err) + } else { + log.Warning("can't read config file ", path, ", ", err) + continue + } + } } func inSlice(n Neighbor, b []Neighbor) int { for i, nb := range b { - if nb.Config.NeighborAddress.String() == n.Config.NeighborAddress.String() { + if nb.Config.NeighborAddress == n.Config.NeighborAddress { return i } } |