Implements core/paths (Config/Data/State/Cache/Runtime/Log/Bin
directory resolution with canonical-vs-search-dir separation, XDG on
Unix, native APPDATA/LOCALAPPDATA/PROGRAMDATA layering on Windows,
~/Library on macOS Desktop, injectable environment/platform for
deterministic cross-platform tests) and core/config (file search,
first-match resolution, layered YAML merging, safe ${VAR} expansion,
Explain/doctor introspection) on top of the shared Application/
Kind/Scope model in the core package.
137 lines
4.3 KiB
Go
137 lines
4.3 KiB
Go
package paths
|
|
|
|
import "strings"
|
|
|
|
// Path joining is implemented per address-family (unix "/" vs. windows
|
|
// "\") rather than via package path/filepath, deliberately: filepath's
|
|
// behavior depends on the host GOOS, but a Resolver must be able to
|
|
// compute Windows paths while running on Linux/macOS (and vice versa) so
|
|
// that platform logic is unit-testable on every host -- see
|
|
// WithPlatform and AGENTS.md's "OS specific logic isolated" rule.
|
|
|
|
// unixJoin joins elements with "/", collapsing empty elements and
|
|
// duplicate slashes at the seams. The first element determines whether
|
|
// the result is absolute.
|
|
func unixJoin(elems ...string) string {
|
|
parts := make([]string, 0, len(elems))
|
|
for i, e := range elems {
|
|
e = strings.Trim(e, "/")
|
|
if e == "" {
|
|
continue
|
|
}
|
|
if i == 0 && strings.HasPrefix(elems[0], "/") {
|
|
e = "/" + e
|
|
}
|
|
parts = append(parts, e)
|
|
}
|
|
joined := strings.Join(parts, "/")
|
|
if len(elems) > 0 && strings.HasPrefix(elems[0], "/") && !strings.HasPrefix(joined, "/") {
|
|
joined = "/" + joined
|
|
}
|
|
if joined == "" && len(elems) > 0 && elems[0] == "/" {
|
|
return "/"
|
|
}
|
|
return joined
|
|
}
|
|
|
|
// windowsJoin joins elements with "\", preserving a leading drive letter
|
|
// or UNC prefix on the first element.
|
|
func windowsJoin(elems ...string) string {
|
|
if len(elems) == 0 {
|
|
return ""
|
|
}
|
|
// A UNC prefix ("\\server\share") is two significant leading
|
|
// backslashes; strip and preserve it separately so the trim loop
|
|
// below (which treats a run of backslashes as a plain separator)
|
|
// doesn't collapse it down to zero, losing the UNC-ness of the path.
|
|
uncPrefix := ""
|
|
first := elems[0]
|
|
if rest, ok := strings.CutPrefix(first, `\\`); ok {
|
|
uncPrefix = `\\`
|
|
first = rest
|
|
}
|
|
|
|
nonEmpty := make([]string, 0, len(elems))
|
|
for i, e := range elems {
|
|
if i == 0 {
|
|
e = first
|
|
}
|
|
e = strings.Trim(e, `\`)
|
|
if e != "" {
|
|
nonEmpty = append(nonEmpty, e)
|
|
}
|
|
}
|
|
return uncPrefix + strings.Join(nonEmpty, `\`)
|
|
}
|
|
|
|
// JoinFamily joins elems using platform's address family ("windows":
|
|
// backslash; anything else: forward slash) rather than the *dir*-content
|
|
// heuristic JoinDirFile uses -- needed when an element (e.g. a bare ".")
|
|
// carries no separator of its own to infer a family from, such as
|
|
// core/config's default working-directory layer.
|
|
func JoinFamily(platform string, elems ...string) string {
|
|
if platform == "windows" {
|
|
return windowsJoin(elems...)
|
|
}
|
|
return unixJoin(elems...)
|
|
}
|
|
|
|
// JoinDirFile joins a directory (as produced by ConfigSearchDirs/
|
|
// DataSearchDirs/ExecutableSearchDirs, in either address family) with a
|
|
// plain file name, picking the join style from the directory's own
|
|
// separators rather than the host OS. Exported so core/config can build
|
|
// file paths from these directories without re-implementing the
|
|
// address-family detection.
|
|
func JoinDirFile(dir, name string) string {
|
|
if strings.ContainsRune(dir, '\\') && !strings.ContainsRune(dir, '/') {
|
|
return windowsJoin(dir, name)
|
|
}
|
|
return unixJoin(dir, name)
|
|
}
|
|
|
|
// splitWindowsRoot separates a Windows absolute path's root (a drive
|
|
// letter like "C:" or a UNC share like "\\server\share") from the
|
|
// remainder, so WithRoot can rebase the remainder under a portable root.
|
|
// It returns ok=false for a path it doesn't recognize as rooted.
|
|
func splitWindowsRoot(p string) (root, rest string, ok bool) {
|
|
if trimmed, ok := strings.CutPrefix(p, `\\`); ok {
|
|
// UNC: \\server\share\rest...
|
|
parts := strings.SplitN(trimmed, `\`, 3)
|
|
switch len(parts) {
|
|
case 0, 1:
|
|
return "", "", false
|
|
case 2:
|
|
return `\\` + parts[0] + `\` + parts[1], "", true
|
|
default:
|
|
return `\\` + parts[0] + `\` + parts[1], parts[2], true
|
|
}
|
|
}
|
|
if len(p) >= 2 && p[1] == ':' {
|
|
rest := strings.TrimPrefix(p[2:], `\`)
|
|
return p[:2], rest, true
|
|
}
|
|
return "", "", false
|
|
}
|
|
|
|
// applyRootUnix rebases an absolute unix-family path under root, when
|
|
// root is set. An empty root leaves p untouched.
|
|
func applyRootUnix(root, p string) string {
|
|
if root == "" {
|
|
return p
|
|
}
|
|
return unixJoin(root, strings.TrimPrefix(p, "/"))
|
|
}
|
|
|
|
// applyRootWindows rebases an absolute windows-family path under root,
|
|
// when root is set. An empty root leaves p untouched.
|
|
func applyRootWindows(root, p string) string {
|
|
if root == "" {
|
|
return p
|
|
}
|
|
_, rest, ok := splitWindowsRoot(p)
|
|
if !ok {
|
|
return windowsJoin(root, p)
|
|
}
|
|
return windowsJoin(root, rest)
|
|
}
|