go/path.go
Snider 7e2783dcf5 feat: add core.Path() + core.Env() fallthrough + PathGlob/PathIsAbs/CleanPath
Path() builds OS-aware absolute paths using Env("DS") — single point
of responsibility for filesystem paths. Relative paths anchor to
DIR_HOME. cleanPath resolves .. and double separators.

Env() now falls through to os.Getenv for unknown keys — universal
replacement for os.Getenv. Core keys (OS, DIR_HOME, etc.) take
precedence, arbitrary env vars pass through.

New exports: Path, PathBase, PathDir, PathExt, PathIsAbs, PathGlob,
CleanPath. Info init moved to init() so Path() can be used during
population without init cycle. DIR_HOME respects CORE_HOME env var
override for agent workspace sandboxing.

13 path tests, 17 env tests — all passing.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-22 09:50:50 +00:00

171 lines
3.8 KiB
Go

// SPDX-License-Identifier: EUPL-1.2
// OS-aware filesystem path operations for the Core framework.
// Zero filepath import — uses Env("DS") for the separator and
// Core string primitives for all path manipulation.
//
// Path anchors relative segments to DIR_HOME:
//
// core.Path("Code", ".core") // "/Users/snider/Code/.core"
// core.Path("/tmp", "workspace") // "/tmp/workspace"
// core.Path() // "/Users/snider"
//
// Path component helpers:
//
// core.PathBase("/Users/snider/Code/core") // "core"
// core.PathDir("/Users/snider/Code/core") // "/Users/snider/Code"
// core.PathExt("main.go") // ".go"
package core
import "path/filepath"
// Path builds a clean, absolute filesystem path from segments.
// Uses Env("DS") for the OS directory separator.
// Relative paths are anchored to DIR_HOME. Absolute paths pass through.
//
// core.Path("Code", ".core") // "/Users/snider/Code/.core"
// core.Path("/tmp", "workspace") // "/tmp/workspace"
// core.Path() // "/Users/snider"
func Path(segments ...string) string {
ds := Env("DS")
home := Env("DIR_HOME")
if len(segments) == 0 {
return home
}
p := Join(ds, segments...)
if PathIsAbs(p) {
return CleanPath(p, ds)
}
return CleanPath(home+ds+p, ds)
}
// PathBase returns the last element of a path.
//
// core.PathBase("/Users/snider/Code/core") // "core"
// core.PathBase("deploy/to/homelab") // "homelab"
func PathBase(p string) string {
if p == "" {
return "."
}
ds := Env("DS")
p = TrimSuffix(p, ds)
if p == "" {
return ds
}
parts := Split(p, ds)
return parts[len(parts)-1]
}
// PathDir returns all but the last element of a path.
//
// core.PathDir("/Users/snider/Code/core") // "/Users/snider/Code"
func PathDir(p string) string {
if p == "" {
return "."
}
ds := Env("DS")
i := lastIndex(p, ds)
if i < 0 {
return "."
}
dir := p[:i]
if dir == "" {
return ds
}
return dir
}
// PathExt returns the file extension including the dot.
//
// core.PathExt("main.go") // ".go"
// core.PathExt("Makefile") // ""
func PathExt(p string) string {
base := PathBase(p)
i := lastIndex(base, ".")
if i <= 0 {
return ""
}
return base[i:]
}
// PathIsAbs returns true if the path is absolute.
// Handles Unix (starts with /) and Windows (drive letter like C:).
//
// core.PathIsAbs("/tmp") // true
// core.PathIsAbs("C:\\tmp") // true
// core.PathIsAbs("relative") // false
func PathIsAbs(p string) bool {
if p == "" {
return false
}
if p[0] == '/' {
return true
}
// Windows: C:\ or C:/
if len(p) >= 3 && p[1] == ':' && (p[2] == '/' || p[2] == '\\') {
return true
}
return false
}
// CleanPath removes redundant separators and resolves . and .. elements.
//
// core.CleanPath("/tmp//file", "/") // "/tmp/file"
// core.CleanPath("a/b/../c", "/") // "a/c"
func CleanPath(p, ds string) string {
if p == "" {
return "."
}
rooted := HasPrefix(p, ds)
parts := Split(p, ds)
var clean []string
for _, part := range parts {
switch part {
case "", ".":
continue
case "..":
if len(clean) > 0 && clean[len(clean)-1] != ".." {
clean = clean[:len(clean)-1]
} else if !rooted {
clean = append(clean, "..")
}
default:
clean = append(clean, part)
}
}
result := Join(ds, clean...)
if rooted {
result = ds + result
}
if result == "" {
if rooted {
return ds
}
return "."
}
return result
}
// PathGlob returns file paths matching a pattern.
//
// core.PathGlob("/tmp/agent-*.log")
func PathGlob(pattern string) []string {
matches, _ := filepath.Glob(pattern)
return matches
}
// lastIndex returns the index of the last occurrence of substr in s, or -1.
func lastIndex(s, substr string) int {
if substr == "" || s == "" {
return -1
}
for i := len(s) - len(substr); i >= 0; i-- {
if s[i:i+len(substr)] == substr {
return i
}
}
return -1
}