diff options
author | Tulir Asokan <tulir@maunium.net> | 2019-04-06 10:57:24 +0300 |
---|---|---|
committer | Tulir Asokan <tulir@maunium.net> | 2019-04-06 10:57:24 +0300 |
commit | 8c1682b225300d055fba72106f07a0fc1fa7dfc5 (patch) | |
tree | 0266f572a59c18c0469694a8b63daa12540ca433 /main.go | |
parent | 7ad2103f8f2c9b7e3d12554634a68db973a05b36 (diff) |
Use XDG config and cache home
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 52 |
1 files changed, 51 insertions, 1 deletions
@@ -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 +} |