mirror of
https://git.sr.ht/~rjarry/aerc
synced 2025-06-30 19:00:21 +02:00

This has nothing to do at the root of the source tree. Signed-off-by: Robin Jarry <robin@jarry.cc> Acked-by: Bence Ferdinandy <bence@ferdinandy.com>
47 lines
1 KiB
Go
47 lines
1 KiB
Go
package xdg
|
|
|
|
import (
|
|
"os"
|
|
"os/user"
|
|
"path"
|
|
"strings"
|
|
|
|
"git.sr.ht/~rjarry/aerc/lib/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
|
|
}
|