2022-07-13 15:07:01 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2022-07-13 15:20:59 -04:00
|
|
|
"log"
|
|
|
|
"os"
|
2022-07-13 15:07:01 -04:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type botConfig struct {
|
|
|
|
Homeserver string `yaml:"homeserver"`
|
|
|
|
Domain string `yaml:"domain"`
|
|
|
|
Dimension string `yaml:"dimension"`
|
|
|
|
Username string `yaml:"username"`
|
|
|
|
Password string `yaml:"password"`
|
|
|
|
Statefile string `yaml:"statefile"`
|
|
|
|
Token string `yaml:"token"`
|
2022-07-13 15:20:59 -04:00
|
|
|
filename string
|
2022-07-13 15:07:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfig(filename string) *botConfig {
|
|
|
|
yamlFile, err := ioutil.ReadFile(filename)
|
|
|
|
cnf := &botConfig{}
|
|
|
|
if err == nil {
|
|
|
|
err = yaml.Unmarshal(yamlFile, cnf)
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-07-13 15:20:59 -04:00
|
|
|
cnf.filename = filename
|
2022-07-13 15:07:01 -04:00
|
|
|
return cnf
|
|
|
|
}
|
2022-07-13 15:20:59 -04:00
|
|
|
|
|
|
|
func writeConfig(cnf *botConfig) {
|
|
|
|
file, err := os.OpenFile(cnf.filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error opening/creating file: %v", err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
enc := yaml.NewEncoder(file)
|
|
|
|
|
|
|
|
err = enc.Encode(cnf)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error encoding: %v", err)
|
|
|
|
}
|
|
|
|
}
|