Use XDG config and cache home

This commit is contained in:
Tulir Asokan 2019-04-06 10:57:24 +03:00
parent 7ad2103f8f
commit 8c1682b225
3 changed files with 53 additions and 5 deletions

View File

@ -98,6 +98,7 @@ func (config *Config) CreateCacheDirs() {
func (config *Config) DeleteSession() { func (config *Config) DeleteSession() {
config.AuthCache.NextBatch = "" config.AuthCache.NextBatch = ""
config.AuthCache.InitialSyncDone = false config.AuthCache.InitialSyncDone = false
config.AccessToken = ""
config.Rooms = make(map[string]*rooms.Room) config.Rooms = make(map[string]*rooms.Room)
config.PushRules = nil config.PushRules = nil

View File

@ -19,7 +19,6 @@ package main
import ( import (
"os" "os"
"os/signal" "os/signal"
"path/filepath"
"syscall" "syscall"
"time" "time"
@ -39,9 +38,7 @@ type Gomuks struct {
// NewGomuks creates a new Gomuks instance with everything initialized, // NewGomuks creates a new Gomuks instance with everything initialized,
// but does not start it. // but does not start it.
func NewGomuks(uiProvider ifc.UIProvider) *Gomuks { func NewGomuks(uiProvider ifc.UIProvider, configDir, cacheDir string) *Gomuks {
configDir := filepath.Join(os.Getenv("HOME"), ".config/gomuks")
cacheDir := filepath.Join(os.Getenv("HOME"), ".cache/gomuks")
gmx := &Gomuks{ gmx := &Gomuks{
stop: make(chan bool, 1), stop: make(chan bool, 1),
} }

52
main.go
View File

@ -17,8 +17,11 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath"
"runtime"
"time" "time"
"maunium.net/go/gomuks/debug" "maunium.net/go/gomuks/debug"
@ -34,7 +37,18 @@ func main() {
enableDebug := len(os.Getenv("DEBUG")) > 0 enableDebug := len(os.Getenv("DEBUG")) > 0
debug.RecoverPrettyPanic = !enableDebug debug.RecoverPrettyPanic = !enableDebug
gmx := NewGomuks(MainUIProvider) configDir, err := UserConfigDir()
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to get config directory:", err)
os.Exit(3)
}
cacheDir, err := UserCacheDir()
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to get cache directory:", err)
os.Exit(3)
}
gmx := NewGomuks(MainUIProvider, configDir, cacheDir)
gmx.Start() gmx.Start()
// We use os.Exit() everywhere, so exiting by returning from Start() shouldn't happen. // We use os.Exit() everywhere, so exiting by returning from Start() shouldn't happen.
@ -42,3 +56,39 @@ func main() {
fmt.Println("Unexpected exit by return from gmx.Start().") fmt.Println("Unexpected exit by return from gmx.Start().")
os.Exit(2) os.Exit(2)
} }
func UserCacheDir() (dir string, err error) {
dir = os.Getenv("GOMUKS_CACHE_HOME")
if dir == "" {
dir, err = os.UserCacheDir()
dir = filepath.Join(dir, "gomuks")
}
return
}
func UserConfigDir() (dir string, err error) {
dir = os.Getenv("GOMUKS_CONFIG_HOME")
if dir != "" {
return
}
if runtime.GOOS == "windows" {
dir = os.Getenv("AppData")
if dir == "" {
err = errors.New("%AppData% is not defined")
}
} else {
dir = os.Getenv("XDG_CONFIG_HOME")
if dir == "" {
dir = os.Getenv("HOME")
if dir == "" {
err = errors.New("neither $XDG_CONFIG_HOME nor $HOME are defined")
} else if runtime.GOOS == "darwin" {
dir = filepath.Join(dir, "Library", "Application Support")
} else {
dir = filepath.Join(dir, ".config")
}
}
}
dir = filepath.Join(dir, "gomuks")
return
}