nunbot/config.go
2022-07-13 15:20:59 -04:00

51 lines
967 B
Go

package main
import (
"io/ioutil"
"log"
"os"
"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"`
filename string
}
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)
}
cnf.filename = filename
return cnf
}
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)
}
}