Files
core/paths/resolver.go
T

133 lines
5.0 KiB
Go
Raw Normal View History

package paths
import (
"runtime"
"yetano.dev/yetano/core"
)
// Resolver computes filesystem paths for one Application. Obtain one with
// New or For; a Resolver is immutable and safe for concurrent use once
// constructed.
type Resolver struct {
app core.Application
env Environment
platform string // runtime.GOOS-shaped; see WithPlatform
root string
envPrefix string
overrides map[PathKind]string
}
// Option configures a Resolver constructed by New/For.
type Option func(*Resolver)
// New constructs a Resolver for app. It validates app (see
// Application.Validate) and applies opts in order.
func New(app core.Application, opts ...Option) (*Resolver, error) {
if err := app.Validate(); err != nil {
return nil, err
}
r := &Resolver{
app: app,
env: osEnvironment{},
platform: runtime.GOOS,
envPrefix: app.EnvPrefix(),
overrides: map[PathKind]string{},
}
for _, opt := range opts {
opt(r)
}
return r, nil
}
// For is an alias for New, matching the spec's `paths.For(app)` spelling.
func For(app core.Application, opts ...Option) (*Resolver, error) {
return New(app, opts...)
}
// Application returns the Application this Resolver was built for.
func (r *Resolver) Application() core.Application { return r.app }
// Platform returns the platform family this Resolver resolves paths for
// (runtime.GOOS by default; see WithPlatform).
func (r *Resolver) Platform() string { return r.platform }
// WithEnvironment injects a fixed environment variable map, for tests
// and for simulating another platform's environment. It replaces any
// previously set environment provider.
func WithEnvironment(env map[string]string) Option {
return func(r *Resolver) { r.env = MapEnvironment(env) }
}
// WithEnvironmentProvider injects a custom Environment implementation.
func WithEnvironmentProvider(env Environment) Option {
return func(r *Resolver) { r.env = env }
}
// WithPlatform overrides the platform family used for resolution
// (normally runtime.GOOS): "linux", "darwin", "freebsd", "windows", or
// any other GOOS value (treated as a generic Unix variant). This exists
// so platform logic can be unit-tested deterministically on any host --
// combine with WithEnvironment to simulate e.g. Windows env vars while
// running the tests on Linux/macOS. It only affects path *computation*;
// Ensure* methods still perform real filesystem I/O using the host's
// native os.MkdirAll, so combining WithPlatform (to a non-native
// platform) with the Ensure* methods is not meaningful and is the
// caller's responsibility to avoid.
func WithPlatform(goos string) Option {
return func(r *Resolver) { r.platform = goos }
}
// WithRoot rebases every resolved absolute path under root, stripping
// the original path's volume/leading separator first (e.g. "/etc/yet"
// under root "/tmp/sandbox" becomes "/tmp/sandbox/etc/yet"). root must be
// given in the same address family as the platform being resolved for
// (forward slashes for Unix, backslashes -- optionally with a drive
// letter or UNC prefix -- for Windows). Useful for tests, containers, and
// a future portable install mode.
func WithRoot(root string) Option {
return func(r *Resolver) { r.root = root }
}
// WithEnvPrefix overrides the application-specific environment variable
// prefix (default: Application.EnvPrefix(), e.g. "YET"). The prefix is
// used as "<PREFIX>_CONFIG_HOME", "<PREFIX>_DATA_HOME", etc.
func WithEnvPrefix(prefix string) Option {
return func(r *Resolver) { r.envPrefix = prefix }
}
// WithoutAppEnv disables the application-specific environment variable
// tier entirely, so only the shared YETANO_* variables (and below) are
// consulted.
func WithoutAppEnv() Option {
return func(r *Resolver) { r.envPrefix = "" }
}
// withOverride sets an explicit, highest-priority value for one
// category. Used by the WithConfigDir/WithDataDir/... family below.
func withOverride(kind PathKind, path string) Option {
return func(r *Resolver) { r.overrides[kind] = path }
}
// WithConfigDir explicitly overrides ConfigDir (and makes it the sole,
// highest-priority entry in ConfigSearchDirs).
func WithConfigDir(path string) Option { return withOverride(ConfigPath, path) }
// WithDataDir explicitly overrides DataDir.
func WithDataDir(path string) Option { return withOverride(DataPath, path) }
// WithStateDir explicitly overrides StateDir.
func WithStateDir(path string) Option { return withOverride(StatePath, path) }
// WithCacheDir explicitly overrides CacheDir.
func WithCacheDir(path string) Option { return withOverride(CachePath, path) }
// WithRuntimeDir explicitly overrides RuntimeDir, and makes it available
// even when the platform's native runtime directory mechanism is not.
func WithRuntimeDir(path string) Option { return withOverride(RuntimePath, path) }
// WithLogDir explicitly overrides LogDir.
func WithLogDir(path string) Option { return withOverride(LogPath, path) }
// WithBinDir explicitly overrides BinDir.
func WithBinDir(path string) Option { return withOverride(BinPath, path) }