discord-community/config.go
Knut Ahlers 378a2247ec
Add templating to config to load env-vars
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2021-07-25 15:00:18 +02:00

57 lines
1.2 KiB
Go

package main
import (
"bytes"
"io/ioutil"
"os"
"text/template"
korvike "github.com/Luzifer/korvike/functions"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
type (
configFile struct {
BotToken string `yaml:"bot_token"`
GuildID string `yaml:"guild_id"`
ModuleConfigs []moduleConfig `yaml:"module_configs"`
}
moduleConfig struct {
Type string `yaml:"type"`
Attributes moduleAttributeStore `yaml:"attributes"`
}
)
func newConfigFromFile(filename string) (*configFile, error) {
f, err := os.Open(filename)
if err != nil {
return nil, errors.Wrap(err, "opening config file")
}
defer f.Close()
configContent, err := ioutil.ReadAll(f)
if err != nil {
return nil, errors.Wrap(err, "reading config file")
}
tpl, err := template.New("config").Funcs(korvike.GetFunctionMap()).Parse(string(configContent))
if err != nil {
return nil, errors.Wrap(err, "parsing config file template")
}
renderedConfig := new(bytes.Buffer)
if err = tpl.Execute(renderedConfig, nil); err != nil {
return nil, errors.Wrap(err, "rendering config template")
}
var (
decoder = yaml.NewDecoder(renderedConfig)
tmp configFile
)
decoder.SetStrict(true)
return &tmp, errors.Wrap(decoder.Decode(&tmp), "decoding config")
}