rpcfetch/fetch/config.go

68 lines
1.3 KiB
Go

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",
Activity: &rpcfetch.Activity{
State: "%used / %total",
Details: "%1min %5min %15min",
Timestamps: &rpcfetch.ActivityTimestamps{Start: &launchTime},
Assets: &rpcfetch.ActivityAssets{
LargeImage: "yorha",
LargeText: "%hostname",
SmallImage: "flan",
SmallText: "PID: %pid",
},
},
}