aboutsummaryrefslogtreecommitdiffstats
path: root/lib/xdg/xdg.go
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-08-23 19:51:54 +0200
committerRobin Jarry <robin@jarry.cc>2023-08-27 18:44:06 +0200
commitfff16640ad7cd8c4b73187fbce10f2aa558701be (patch)
tree8112c2d2414231d78d05dc255a430512c26fc4e1 /lib/xdg/xdg.go
parentbc8fdbbe8479f9f604f429931e7c90e396ea6f00 (diff)
downloadaerc-fff16640ad7cd8c4b73187fbce10f2aa558701be.tar.gz
xdg: add functions to deal with user home paths
These are intended to replace the following deprecated libraries: github.com/kyoh86/xdg github.com/mitchellh/go-homedir The feature set should be roughly equivalent with some tweaks to make our life easier in aerc. Signed-off-by: Robin Jarry <robin@jarry.cc> Reviewed-by: Moritz Poldrack <moritz@poldrack.dev>
Diffstat (limited to 'lib/xdg/xdg.go')
-rw-r--r--lib/xdg/xdg.go89
1 files changed, 89 insertions, 0 deletions
diff --git a/lib/xdg/xdg.go b/lib/xdg/xdg.go
new file mode 100644
index 00000000..c1eaab03
--- /dev/null
+++ b/lib/xdg/xdg.go
@@ -0,0 +1,89 @@
+package xdg
+
+import (
+ "os"
+ "path/filepath"
+ "runtime"
+ "strconv"
+)
+
+// Return a path relative to the user home cache dir
+func CachePath(paths ...string) string {
+ res := filepath.Join(paths...)
+ if !filepath.IsAbs(res) {
+ var cache string
+ if runtime.GOOS == "darwin" {
+ // preserve backward compat with github.com/kyoh86/xdg
+ cache = os.Getenv("XDG_CACHE_HOME")
+ }
+ if cache == "" {
+ var err error
+ cache, err = os.UserCacheDir()
+ if err != nil {
+ cache = ExpandHome("~/.cache")
+ }
+ }
+ res = filepath.Join(cache, res)
+ }
+ return res
+}
+
+// Return a path relative to the user home config dir
+func ConfigPath(paths ...string) string {
+ res := filepath.Join(paths...)
+ if !filepath.IsAbs(res) {
+ var config string
+ if runtime.GOOS == "darwin" {
+ // preserve backward compat with github.com/kyoh86/xdg
+ config = os.Getenv("XDG_CONFIG_HOME")
+ if config == "" {
+ config = ExpandHome("~/Library/Preferences")
+ }
+ } else {
+ var err error
+ config, err = os.UserConfigDir()
+ if err != nil {
+ config = ExpandHome("~/.config")
+ }
+ }
+ res = filepath.Join(config, res)
+ }
+ return res
+}
+
+// Return a path relative to the user data home dir
+func DataPath(paths ...string) string {
+ res := filepath.Join(paths...)
+ if !filepath.IsAbs(res) {
+ data := os.Getenv("XDG_DATA_HOME")
+ // preserve backward compat with github.com/kyoh86/xdg
+ if data == "" && runtime.GOOS == "darwin" {
+ data = ExpandHome("~/Library/Application Support")
+ } else if data == "" {
+ data = ExpandHome("~/.local/share")
+ }
+ res = filepath.Join(data, res)
+ }
+ return res
+}
+
+// ugly: there's no other way to allow mocking a function in go...
+var userRuntimePath = func() string {
+ return filepath.Join("/run/user", strconv.Itoa(os.Getuid()))
+}
+
+// Return a path relative to the user runtime dir
+func RuntimePath(paths ...string) string {
+ res := filepath.Join(paths...)
+ if !filepath.IsAbs(res) {
+ run := os.Getenv("XDG_RUNTIME_DIR")
+ // preserve backward compat with github.com/kyoh86/xdg
+ if run == "" && runtime.GOOS == "darwin" {
+ run = ExpandHome("~/Library/Application Support")
+ } else if run == "" {
+ run = userRuntimePath()
+ }
+ res = filepath.Join(run, res)
+ }
+ return res
+}