matrixbotlib/config.go

54 lines
1.2 KiB
Go
Raw Permalink Normal View History

2022-07-17 17:22:28 -04:00
package matrixbotlib
import (
"io/ioutil"
"os"
"gopkg.in/yaml.v2"
)
//MatrixClientConfig represents the config requires to log into the matrix server
type MatrixClientConfig 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,omitempty"`
Token string `yaml:"token"`
filename string
}
//LoadMatrixClientConfig reads info from a file
func LoadMatrixClientConfig(filename string) (*MatrixClientConfig, error) {
yamlFile, err := ioutil.ReadFile(filename)
cnf := &MatrixClientConfig{}
if err == nil {
err = yaml.Unmarshal(yamlFile, cnf)
} else {
return cnf, err
}
if err != nil {
return cnf, err
}
cnf.filename = filename
return cnf, nil
}
//WriteMatrixClientConfig saves current running config to a file
func WriteMatrixClientConfig(cnf *MatrixClientConfig) error {
file, err := os.OpenFile(cnf.filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
defer file.Close()
enc := yaml.NewEncoder(file)
err = enc.Encode(cnf)
if err != nil {
return err
}
return nil
}