2021-07-22 22:54:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-07-25 13:00:18 +00:00
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2021-07-22 22:54:11 +00:00
|
|
|
"os"
|
2021-07-25 13:00:18 +00:00
|
|
|
"text/template"
|
2021-07-22 22:54:11 +00:00
|
|
|
|
2021-07-25 13:00:18 +00:00
|
|
|
korvike "github.com/Luzifer/korvike/functions"
|
2021-07-22 22:54:11 +00:00
|
|
|
"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()
|
|
|
|
|
2021-07-25 13:00:18 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-07-22 22:54:11 +00:00
|
|
|
var (
|
2021-07-25 13:00:18 +00:00
|
|
|
decoder = yaml.NewDecoder(renderedConfig)
|
2021-07-22 22:54:11 +00:00
|
|
|
tmp configFile
|
|
|
|
)
|
|
|
|
|
|
|
|
decoder.SetStrict(true)
|
|
|
|
return &tmp, errors.Wrap(decoder.Decode(&tmp), "decoding config")
|
|
|
|
}
|