save access token

This commit is contained in:
stryan 2022-07-13 15:20:59 -04:00
parent ccdd7c3280
commit 1b677a2bbc
2 changed files with 20 additions and 0 deletions

View File

@ -37,6 +37,7 @@ func newMatrixClient(config *botConfig) *mautrix.Client {
}
config.Token = loginRes.AccessToken
log.Println("Login succesful, saving access_token to config file")
writeConfig(config)
} else {
log.Println("skipping login since token provided")
}

View File

@ -2,6 +2,8 @@ package main
import (
"io/ioutil"
"log"
"os"
"gopkg.in/yaml.v2"
)
@ -14,6 +16,7 @@ type botConfig struct {
Password string `yaml:"password"`
Statefile string `yaml:"statefile"`
Token string `yaml:"token"`
filename string
}
func loadConfig(filename string) *botConfig {
@ -27,5 +30,21 @@ func loadConfig(filename string) *botConfig {
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)
}
}