aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2019-04-06 10:57:24 +0300
committerTulir Asokan <tulir@maunium.net>2019-04-06 10:57:24 +0300
commit8c1682b225300d055fba72106f07a0fc1fa7dfc5 (patch)
tree0266f572a59c18c0469694a8b63daa12540ca433
parent7ad2103f8f2c9b7e3d12554634a68db973a05b36 (diff)
Use XDG config and cache home
-rw-r--r--config/config.go1
-rw-r--r--gomuks.go5
-rw-r--r--main.go52
3 files changed, 53 insertions, 5 deletions
diff --git a/config/config.go b/config/config.go
index a3127c4..f77275f 100644
--- a/config/config.go
+++ b/config/config.go
@@ -98,6 +98,7 @@ func (config *Config) CreateCacheDirs() {
func (config *Config) DeleteSession() {
config.AuthCache.NextBatch = ""
config.AuthCache.InitialSyncDone = false
+ config.AccessToken = ""
config.Rooms = make(map[string]*rooms.Room)
config.PushRules = nil
diff --git a/gomuks.go b/gomuks.go
index 8d5ac76..7c307b9 100644
--- a/gomuks.go
+++ b/gomuks.go
@@ -19,7 +19,6 @@ package main
import (
"os"
"os/signal"
- "path/filepath"
"syscall"
"time"
@@ -39,9 +38,7 @@ type Gomuks struct {
// NewGomuks creates a new Gomuks instance with everything initialized,
// but does not start it.
-func NewGomuks(uiProvider ifc.UIProvider) *Gomuks {
- configDir := filepath.Join(os.Getenv("HOME"), ".config/gomuks")
- cacheDir := filepath.Join(os.Getenv("HOME"), ".cache/gomuks")
+func NewGomuks(uiProvider ifc.UIProvider, configDir, cacheDir string) *Gomuks {
gmx := &Gomuks{
stop: make(chan bool, 1),
}
diff --git a/main.go b/main.go
index 26762f9..e925f3e 100644
--- a/main.go
+++ b/main.go
@@ -17,8 +17,11 @@
package main
import (
+ "errors"
"fmt"
"os"
+ "path/filepath"
+ "runtime"
"time"
"maunium.net/go/gomuks/debug"
@@ -34,7 +37,18 @@ func main() {
enableDebug := len(os.Getenv("DEBUG")) > 0
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()
// 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().")
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
+}