aboutsummaryrefslogtreecommitdiff
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
parentbc8fdbbe8479f9f604f429931e7c90e396ea6f00 (diff)
downloadaerc-fff16640ad7cd8c4b73187fbce10f2aa558701be.tar.gz
aerc-fff16640ad7cd8c4b73187fbce10f2aa558701be.zip
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>
-rw-r--r--lib/xdg/home.go47
-rw-r--r--lib/xdg/home_test.go88
-rw-r--r--lib/xdg/xdg.go89
-rw-r--r--lib/xdg/xdg_test.go179
4 files changed, 403 insertions, 0 deletions
diff --git a/lib/xdg/home.go b/lib/xdg/home.go
new file mode 100644
index 00000000..3471e5e2
--- /dev/null
+++ b/lib/xdg/home.go
@@ -0,0 +1,47 @@
+package xdg
+
+import (
+ "os"
+ "os/user"
+ "path"
+ "strings"
+
+ "git.sr.ht/~rjarry/aerc/log"
+)
+
+// assign to a local var to allow mocking in unit tests
+var currentUser = user.Current
+
+// Get the current user home directory (first from the $HOME env var and
+// fallback on calling getpwuid_r() from libc if $HOME is unset).
+func HomeDir() string {
+ home, err := os.UserHomeDir()
+ if err != nil {
+ u, e := currentUser()
+ if e == nil {
+ home = u.HomeDir
+ } else {
+ log.Errorf("HomeDir: %s (while handling %s)", e, err)
+ }
+ }
+ return home
+}
+
+// Replace ~ with the current user's home dir
+func ExpandHome(fragments ...string) string {
+ home := HomeDir()
+ res := path.Join(fragments...)
+ if strings.HasPrefix(res, "~/") || res == "~" {
+ res = home + strings.TrimPrefix(res, "~")
+ }
+ return res
+}
+
+// Replace $HOME with ~ (inverse function of ExpandHome)
+func TildeHome(path string) string {
+ home := HomeDir()
+ if strings.HasPrefix(path, home+"/") || path == home {
+ path = "~" + strings.TrimPrefix(path, home)
+ }
+ return path
+}
diff --git a/lib/xdg/home_test.go b/lib/xdg/home_test.go
new file mode 100644
index 00000000..673e35b5
--- /dev/null
+++ b/lib/xdg/home_test.go
@@ -0,0 +1,88 @@
+package xdg
+
+import (
+ "errors"
+ "os/user"
+ "testing"
+)
+
+func TestHomeDir(t *testing.T) {
+ t.Run("from env", func(t *testing.T) {
+ t.Setenv("HOME", "/home/user")
+ home := HomeDir()
+ if home != "/home/user" {
+ t.Errorf(`got %q expected "/home/user"`, home)
+ }
+ })
+ t.Run("from getpwuid_r", func(t *testing.T) {
+ t.Setenv("HOME", "")
+ orig := currentUser
+ currentUser = func() (*user.User, error) {
+ return &user.User{HomeDir: "/home/user"}, nil
+ }
+ home := HomeDir()
+ currentUser = orig
+ if home != "/home/user" {
+ t.Errorf(`got %q expected "/home/user"`, home)
+ }
+ })
+ t.Run("failure", func(t *testing.T) {
+ t.Setenv("HOME", "")
+ orig := currentUser
+ currentUser = func() (*user.User, error) {
+ return nil, errors.New("no such user")
+ }
+ home := HomeDir()
+ currentUser = orig
+ if home != "" {
+ t.Errorf(`got %q expected ""`, home)
+ }
+ })
+}
+
+func TestExpandHome(t *testing.T) {
+ t.Setenv("HOME", "/home/user")
+ vectors := []struct {
+ args []string
+ expected string
+ }{
+ {args: []string{"foo"}, expected: "foo"},
+ {args: []string{"foo", "bar"}, expected: "foo/bar"},
+ {args: []string{"/foobar/baz"}, expected: "/foobar/baz"},
+ {args: []string{"~/foobar/baz"}, expected: "/home/user/foobar/baz"},
+ {args: []string{}, expected: ""},
+ {args: []string{"~"}, expected: "/home/user"},
+ }
+ for _, vec := range vectors {
+ t.Run(vec.expected, func(t *testing.T) {
+ res := ExpandHome(vec.args...)
+ if res != vec.expected {
+ t.Errorf("got %q expected %q", res, vec.expected)
+ }
+ })
+ }
+}
+
+func TestTildeHome(t *testing.T) {
+ t.Setenv("HOME", "/home/user")
+ vectors := []struct {
+ arg string
+ expected string
+ }{
+ {arg: "foo", expected: "foo"},
+ {arg: "foo/bar", expected: "foo/bar"},
+ {arg: "/foobar/baz", expected: "/foobar/baz"},
+ {arg: "/home/user/foobar/baz", expected: "~/foobar/baz"},
+ {arg: "", expected: ""},
+ {arg: "/home/user", expected: "~"},
+ {arg: "/home/user2/foobar/baz", expected: "/home/user2/foobar/baz"},
+ }
+ for _, vec := range vectors {
+ t.Run(vec.expected, func(t *testing.T) {
+ res := TildeHome(vec.arg)
+ if res != vec.expected {
+ t.Errorf("got %q expected %q", res, vec.expected)
+ }
+ })
+ }
+}
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
+}
diff --git a/lib/xdg/xdg_test.go b/lib/xdg/xdg_test.go
new file mode 100644
index 00000000..6b8eac35
--- /dev/null
+++ b/lib/xdg/xdg_test.go
@@ -0,0 +1,179 @@
+package xdg
+
+import (
+ "runtime"
+ "testing"
+)
+
+func TestCachePath(t *testing.T) {
+ t.Setenv("HOME", "/home/user")
+ vectors := []struct {
+ args []string
+ env map[string]string
+ expected map[string]string
+ }{
+ {
+ args: []string{"aerc", "foo", "history"},
+ expected: map[string]string{
+ "": "/home/user/.cache/aerc/foo/history",
+ "darwin": "/home/user/Library/Caches/aerc/foo/history",
+ },
+ },
+ {
+ args: []string{"aerc", "foo/zuul"},
+ env: map[string]string{"XDG_CACHE_HOME": "/home/x/.cache"},
+ expected: map[string]string{"": "/home/x/.cache/aerc/foo/zuul"},
+ },
+ {
+ args: []string{},
+ env: map[string]string{"XDG_CACHE_HOME": "/blah"},
+ expected: map[string]string{"": "/blah"},
+ },
+ }
+ for _, vec := range vectors {
+ expected, found := vec.expected[runtime.GOOS]
+ if !found {
+ expected = vec.expected[""]
+ }
+ t.Run(expected, func(t *testing.T) {
+ for key, value := range vec.env {
+ t.Setenv(key, value)
+ }
+ res := CachePath(vec.args...)
+ if res != expected {
+ t.Errorf("got %q expected %q", res, expected)
+ }
+ })
+ }
+}
+
+func TestConfigPath(t *testing.T) {
+ t.Setenv("HOME", "/home/user")
+ vectors := []struct {
+ args []string
+ env map[string]string
+ expected map[string]string
+ }{
+ {
+ args: []string{"aerc", "accounts.conf"},
+ expected: map[string]string{
+ "": "/home/user/.config/aerc/accounts.conf",
+ "darwin": "/home/user/Library/Preferences/aerc/accounts.conf",
+ },
+ },
+ {
+ args: []string{"aerc", "accounts.conf"},
+ env: map[string]string{"XDG_CONFIG_HOME": "/users/x/.config"},
+ expected: map[string]string{"": "/users/x/.config/aerc/accounts.conf"},
+ },
+ {
+ args: []string{},
+ env: map[string]string{"XDG_CONFIG_HOME": "/blah"},
+ expected: map[string]string{"": "/blah"},
+ },
+ }
+ for _, vec := range vectors {
+ expected, found := vec.expected[runtime.GOOS]
+ if !found {
+ expected = vec.expected[""]
+ }
+ t.Run(expected, func(t *testing.T) {
+ for key, value := range vec.env {
+ t.Setenv(key, value)
+ }
+ res := ConfigPath(vec.args...)
+ if res != expected {
+ t.Errorf("got %q expected %q", res, expected)
+ }
+ })
+ }
+}
+
+func TestDataPath(t *testing.T) {
+ t.Setenv("HOME", "/home/user")
+ vectors := []struct {
+ args []string
+ env map[string]string
+ expected map[string]string
+ }{
+ {
+ args: []string{"aerc", "templates"},
+ expected: map[string]string{
+ "": "/home/user/.local/share/aerc/templates",
+ "darwin": "/home/user/Library/Application Support/aerc/templates",
+ },
+ },
+ {
+ args: []string{"aerc", "templates"},
+ env: map[string]string{"XDG_DATA_HOME": "/users/x/.local/share"},
+ expected: map[string]string{"": "/users/x/.local/share/aerc/templates"},
+ },
+ {
+ args: []string{},
+ env: map[string]string{"XDG_DATA_HOME": "/blah"},
+ expected: map[string]string{"": "/blah"},
+ },
+ }
+ for _, vec := range vectors {
+ expected, found := vec.expected[runtime.GOOS]
+ if !found {
+ expected = vec.expected[""]
+ }
+ t.Run(expected, func(t *testing.T) {
+ for key, value := range vec.env {
+ t.Setenv(key, value)
+ }
+ res := DataPath(vec.args...)
+ if res != expected {
+ t.Errorf("got %q expected %q", res, expected)
+ }
+ })
+ }
+}
+
+func TestRuntimePath(t *testing.T) {
+ // poor man's function mocking
+ orig := userRuntimePath
+ userRuntimePath = func() string { return "/run/user/1000" }
+ defer func() { userRuntimePath = orig }()
+ t.Setenv("HOME", "/home/user")
+
+ vectors := []struct {
+ args []string
+ env map[string]string
+ expected map[string]string
+ }{
+ {
+ args: []string{"aerc.sock"},
+ expected: map[string]string{
+ "": "/run/user/1000/aerc.sock",
+ "darwin": "/home/user/Library/Application Support/aerc.sock",
+ },
+ },
+ {
+ args: []string{"aerc.sock"},
+ env: map[string]string{"XDG_RUNTIME_DIR": "/run/user/1234"},
+ expected: map[string]string{"": "/run/user/1234/aerc.sock"},
+ },
+ {
+ args: []string{},
+ env: map[string]string{"XDG_RUNTIME_DIR": "/blah"},
+ expected: map[string]string{"": "/blah"},
+ },
+ }
+ for _, vec := range vectors {
+ expected, found := vec.expected[runtime.GOOS]
+ if !found {
+ expected = vec.expected[""]
+ }
+ t.Run(expected, func(t *testing.T) {
+ for key, value := range vec.env {
+ t.Setenv(key, value)
+ }
+ res := RuntimePath(vec.args...)
+ if res != expected {
+ t.Errorf("got %q expected %q", res, expected)
+ }
+ })
+ }
+}