diff --git a/fetch/apply.go b/fetch/apply.go index 41cf497..9850824 100644 --- a/fetch/apply.go +++ b/fetch/apply.go @@ -1,12 +1,10 @@ package main import ( - "encoding/gob" "errors" "fmt" "log" "os" - "path" "strconv" "strings" "time" @@ -63,6 +61,7 @@ func (s *applyState) replace(t string) string { } func apply() { + confLock.RLock() act := *conf.Activity s := &applyState{} @@ -74,6 +73,7 @@ func apply() { SmallImage: act.Assets.SmallImage, SmallText: s.replace(act.Assets.SmallText), } + confLock.RUnlock() if nonce, err := retry(&act); err != nil { log.Fatalf("error setting activity: %s", err) @@ -82,27 +82,6 @@ func apply() { } } -func save() { - nf, err := os.CreateTemp(path.Dir(confPath), ".rpcfetch.conf.*") - if err != nil { - log.Fatalf("error creating temporary configuration file: %s", err) - } - - if err = gob.NewEncoder(nf).Encode(defaultConfig); err != nil { - fmt.Printf("error writing configuration file: %s\n", err) - os.Exit(1) - } - - if err = nf.Close(); err != nil { - log.Fatalf("error closing temporary configuration file: %s", err) - } - - if err = os.Rename(nf.Name(), confPath); err != nil { - log.Fatalf("error renaming configuration file: %s", err) - } - log.Printf("saved configuration to %s", confPath) -} - func retry(act *rpcfetch.Activity) (string, error) { try: nonce, err := d.SetActivity(act) diff --git a/fetch/config.go b/fetch/config.go index 21c7cd0..ed16434 100644 --- a/fetch/config.go +++ b/fetch/config.go @@ -1,14 +1,55 @@ package main import ( + "encoding/gob" + "fmt" + "log" + "os" + "path" + "sync" + "git.ophivana.moe/cat/rpcfetch" ) +var ( + conf *config + confLock = &sync.RWMutex{} +) + type config struct { ID string Activity *rpcfetch.Activity } +func (c *config) write(v config) { + confLock.Lock() + *c = v + confLock.Unlock() +} + +func save() { + nf, err := os.CreateTemp(path.Dir(confPath), ".rpcfetch.conf.*") + if err != nil { + log.Fatalf("error creating temporary configuration file: %s", err) + } + + confLock.RLock() + if err = gob.NewEncoder(nf).Encode(defaultConfig); err != nil { + fmt.Printf("error writing configuration file: %s\n", err) + os.Exit(1) + } + confLock.RUnlock() + + if err = nf.Close(); err != nil { + log.Fatalf("error closing temporary configuration file: %s", err) + } + + if err = os.Rename(nf.Name(), confPath); err != nil { + log.Fatalf("error renaming configuration file: %s", err) + } + log.Printf("saved configuration to %s", confPath) +} + // sample config so the program works out of the box var defaultConfig = config{ ID: "1252927154480611351", diff --git a/fetch/main.go b/fetch/main.go index 24e0d53..191002d 100644 --- a/fetch/main.go +++ b/fetch/main.go @@ -14,7 +14,6 @@ import ( var ( d *rpcfetch.Client - conf config confPath string ) @@ -33,7 +32,7 @@ func main() { // use defaults log.Print("configuration file does not exist, using defaults") - conf = defaultConfig + conf = &defaultConfig } else { // decode from existing file if err = gob.NewDecoder(cf).Decode(&conf); err != nil {