From 1b677a2bbca6a02ae61f56ca5da7211c725eff31 Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 13 Jul 2022 15:20:59 -0400 Subject: [PATCH] save access token --- client.go | 1 + config.go | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/client.go b/client.go index c69f7b1..c63af16 100644 --- a/client.go +++ b/client.go @@ -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") } diff --git a/config.go b/config.go index 236256a..2c91b5a 100644 --- a/config.go +++ b/config.go @@ -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) + } +}