46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
package paths
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// ExpandHome replaces a leading "~" or "~/" with the user's home directory.
|
|
func ExpandHome(path string) string {
|
|
if path == "~" || strings.HasPrefix(path, "~/") {
|
|
if home, _ := os.UserHomeDir(); home != "" {
|
|
return home + path[1:]
|
|
}
|
|
}
|
|
return path
|
|
}
|
|
|
|
// xdgConfigHome returns $XDG_CONFIG_HOME or ~/.config.
|
|
func xdgConfigHome() string {
|
|
if p := os.Getenv("XDG_CONFIG_HOME"); p != "" {
|
|
return p
|
|
}
|
|
home, _ := os.UserHomeDir()
|
|
return filepath.Join(home, ".config")
|
|
}
|
|
|
|
// xdgDataHome returns $XDG_DATA_HOME or ~/.local/share.
|
|
func xdgDataHome() string {
|
|
if p := os.Getenv("XDG_DATA_HOME"); p != "" {
|
|
return p
|
|
}
|
|
home, _ := os.UserHomeDir()
|
|
return filepath.Join(home, ".local", "share")
|
|
}
|
|
|
|
// CfgDir returns the ollie config root: $XDG_CONFIG_HOME/ollie.
|
|
func CfgDir() string {
|
|
return filepath.Join(xdgConfigHome(), "ollie")
|
|
}
|
|
|
|
// DataDir returns the ollie data root: $XDG_DATA_HOME/ollie.
|
|
func DataDir() string {
|
|
return filepath.Join(xdgDataHome(), "ollie")
|
|
}
|