1
0
Fork 0
mirror of https://github.com/alice-lg/birdwatcher.git synced 2025-03-09 00:00:05 +01:00
birdwatcher/config.go

69 lines
1.4 KiB
Go
Raw Permalink Normal View History

package main
// Birdwatcher Configuration
import (
"fmt"
"log"
2017-02-22 18:49:17 +01:00
"strings"
2016-11-30 15:19:01 +01:00
"github.com/BurntSushi/toml"
"github.com/imdario/mergo"
2016-11-30 13:40:34 +01:00
"github.com/ecix/birdwatcher/bird"
2016-11-30 15:19:01 +01:00
"github.com/ecix/birdwatcher/endpoints"
)
type Config struct {
2016-11-30 15:19:01 +01:00
Server endpoints.ServerConfig
2016-11-30 13:40:34 +01:00
2016-12-13 10:49:18 +01:00
Ratelimit bird.RateLimitConfig
Status bird.StatusConfig
Bird bird.BirdConfig
Bird6 bird.BirdConfig
Parser bird.ParserConfig
}
// Try to load configfiles as specified in the files
// list. For example:
//
// ./etc/birdwatcher/birdwatcher.conf
// /etc/birdwatcher/birdwatcher.conf
// ./etc/birdwatcher/birdwatcher.local.conf
//
//
func LoadConfigs(configFiles []string) (*Config, error) {
config := &Config{}
hasConfig := false
var confError error
for _, filename := range configFiles {
tmp := &Config{}
_, err := toml.DecodeFile(filename, tmp)
if err != nil {
continue
} else {
log.Println("Using config file:", filename)
hasConfig = true
// Merge configs
if err := mergo.Merge(config, tmp); err != nil {
return nil, err
}
}
}
if !hasConfig {
confError = fmt.Errorf("Could not load any config file")
}
return config, confError
}
2017-02-22 18:49:17 +01:00
func ConfigOptions(filename string) []string {
return []string{
strings.Join([]string{"/", filename}, ""),
2017-02-22 18:52:48 +01:00
strings.Join([]string{"./", filename}, ""),
2017-02-22 18:49:17 +01:00
strings.Replace(filename, ".conf", ".local.conf", 1),
}
}