ollie/util/paths.go

67 lines
1.6 KiB
Go

package util
import (
"fmt"
"os"
"path/filepath"
"strings"
)
// IsGitRepo reports whether dir contains a .git entry.
func IsGitRepo(dir string) bool {
_, err := os.Stat(filepath.Join(dir, ".git"))
return err == nil
}
// 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
}
// RuntimeDir returns $XDG_RUNTIME_DIR or the standard fallback /run/user/UID.
func RuntimeDir() string {
if d := os.Getenv("XDG_RUNTIME_DIR"); d != "" {
return d
}
return fmt.Sprintf("/run/user/%d", os.Getuid())
}
// 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")
}
// UserDataHome returns $XDG_DATA_HOME or ~/.local/share (Linux),
// ~/Library/Application Support (macOS), or %LocalAppData% (Windows).
func UserDataHome() string {
return xdgDataHome()
}
// DataDir returns the ollie data root: $XDG_DATA_HOME/ollie.
func DataDir() string {
return filepath.Join(xdgDataHome(), "ollie")
}