loadmaster2k/config/config.go
2020-10-28 15:35:21 +01:00

73 lines
1.3 KiB
Go

package config
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"strings"
)
type config struct {
Areas []Area
Bot struct {
Interval int
Token string
Channels []struct {
Id int64
Message int64
}
}
// Enable debug output
Debug bool
}
type Area struct {
Name string
Id int
}
func (c config) GetAreaByName(name string) *Area {
for i, area := range c.Areas {
if strings.ToLower(area.Name) == strings.ToLower(name) {
return &c.Areas[i]
}
}
return nil
}
func (c config) GetAreaById(id int) *Area {
for i, area := range c.Areas {
if area.Id == id {
return &c.Areas[i]
}
}
return nil
}
func (a Area) String() string {
return fmt.Sprintf("Area[%v](%v)", a.Id, a.Name)
}
var C config
func init() {
//viper.SetDefault("Target.User", "uplog")
//viper.SetDefault("Target.Group", "uplog")
//viper.SetDefault("Target.Perm.File", 0640)
viper.SetDefault("Bot.Interval", 300)
viper.AutomaticEnv()
}
func LoadConfiguration() {
viper.AddConfigPath(".")
viper.AddConfigPath("~/")
viper.AddConfigPath("/etc/")
viper.SetConfigName("load")
if err := viper.ReadInConfig(); err != nil {
log.WithError(err).Warn("cannot read configuration file")
}
if err := viper.Unmarshal(&C); err != nil {
log.WithError(err).Warn("cannot parse configuration")
}
}