Documentation
¶
Index ¶
- func MockDirEntry(name string, isDir bool) os.DirEntry
- type Executor
- type Mock
- func (m *Mock) CurrentUser() (*user.User, error)
- func (m *Mock) DirExists(path string) bool
- func (m *Mock) DiskCapacityBytes(path string) uint64
- func (m *Mock) EvalSymlinks(path string) (string, error)
- func (m *Mock) FileExists(path string) bool
- func (m *Mock) GOOS() string
- func (m *Mock) Getenv(key string) string
- func (m *Mock) Glob(pattern string) ([]string, error)
- func (m *Mock) HomeDir(_ string) (string, error)
- func (m *Mock) Hostname() (string, error)
- func (m *Mock) IsAppleCLTStub(_ context.Context, binPath string) bool
- func (m *Mock) IsRoot() bool
- func (m *Mock) LoggedInUser() (*user.User, error)
- func (m *Mock) LookPath(name string) (string, error)
- func (m *Mock) ReadDir(path string) ([]os.DirEntry, error)
- func (m *Mock) ReadFile(path string) ([]byte, error)
- func (m *Mock) Run(_ context.Context, name string, args ...string) (string, string, int, error)
- func (m *Mock) RunAsUser(ctx context.Context, _ string, command string) (string, error)
- func (m *Mock) RunInDir(ctx context.Context, _ string, _ time.Duration, name string, args ...string) (string, string, int, error)
- func (m *Mock) RunWithTimeout(ctx context.Context, _ time.Duration, name string, args ...string) (string, string, int, error)
- func (m *Mock) SetAppleCLTInstalled(installed bool)
- func (m *Mock) SetCommand(stdout, stderr string, exitCode int, name string, args ...string)
- func (m *Mock) SetCommandError(err error, name string, args ...string)
- func (m *Mock) SetDir(path string)
- func (m *Mock) SetDirEntries(path string, entries []os.DirEntry)
- func (m *Mock) SetDiskCapacityBytes(path string, bytes uint64)
- func (m *Mock) SetEnv(key, value string)
- func (m *Mock) SetFile(path string, content []byte)
- func (m *Mock) SetFileInfo(path string, info os.FileInfo)
- func (m *Mock) SetGOOS(goos string)
- func (m *Mock) SetGlob(pattern string, matches []string)
- func (m *Mock) SetHomeDir(h string)
- func (m *Mock) SetHostname(h string)
- func (m *Mock) SetIsRoot(v bool)
- func (m *Mock) SetLoggedInUserError(err error)
- func (m *Mock) SetPath(name, path string)
- func (m *Mock) SetSymlink(path, target string)
- func (m *Mock) SetUsername(u string)
- func (m *Mock) Stat(path string) (os.FileInfo, error)
- type Real
- func (r *Real) CurrentUser() (*user.User, error)
- func (r *Real) DirExists(path string) bool
- func (r *Real) DiskCapacityBytes(path string) uint64
- func (r *Real) EvalSymlinks(path string) (string, error)
- func (r *Real) FileExists(path string) bool
- func (r *Real) GOOS() string
- func (r *Real) Getenv(key string) string
- func (r *Real) Glob(pattern string) ([]string, error)
- func (r *Real) HomeDir(username string) (string, error)
- func (r *Real) Hostname() (string, error)
- func (r *Real) IsAppleCLTStub(_ context.Context, binPath string) bool
- func (r *Real) IsRoot() bool
- func (r *Real) LoggedInUser() (*user.User, error)
- func (r *Real) LookPath(name string) (string, error)
- func (r *Real) ReadDir(path string) ([]os.DirEntry, error)
- func (r *Real) ReadFile(path string) ([]byte, error)
- func (r *Real) Run(ctx context.Context, name string, args ...string) (string, string, int, error)
- func (r *Real) RunAsUser(ctx context.Context, username, command string) (string, error)
- func (r *Real) RunInDir(ctx context.Context, dir string, timeout time.Duration, name string, ...) (string, string, int, error)
- func (r *Real) RunWithTimeout(ctx context.Context, timeout time.Duration, name string, args ...string) (string, string, int, error)
- func (r *Real) Stat(path string) (os.FileInfo, error)
- type UserAwareExecutor
- func (e *UserAwareExecutor) CurrentUser() (*user.User, error)
- func (e *UserAwareExecutor) DirExists(path string) bool
- func (e *UserAwareExecutor) DiskCapacityBytes(path string) uint64
- func (e *UserAwareExecutor) EvalSymlinks(path string) (string, error)
- func (e *UserAwareExecutor) FileExists(path string) bool
- func (e *UserAwareExecutor) GOOS() string
- func (e *UserAwareExecutor) Getenv(key string) string
- func (e *UserAwareExecutor) Glob(pattern string) ([]string, error)
- func (e *UserAwareExecutor) HomeDir(username string) (string, error)
- func (e *UserAwareExecutor) Hostname() (string, error)
- func (e *UserAwareExecutor) IsAppleCLTStub(ctx context.Context, binPath string) bool
- func (e *UserAwareExecutor) IsRoot() bool
- func (e *UserAwareExecutor) LoggedInUser() (*user.User, error)
- func (e *UserAwareExecutor) LookPath(name string) (string, error)
- func (e *UserAwareExecutor) ReadDir(path string) ([]os.DirEntry, error)
- func (e *UserAwareExecutor) ReadFile(path string) ([]byte, error)
- func (e *UserAwareExecutor) Run(ctx context.Context, name string, args ...string) (string, string, int, error)
- func (e *UserAwareExecutor) RunAsUser(ctx context.Context, username, command string) (string, error)
- func (e *UserAwareExecutor) RunInDir(ctx context.Context, dir string, timeout time.Duration, name string, ...) (string, string, int, error)
- func (e *UserAwareExecutor) RunWithTimeout(ctx context.Context, timeout time.Duration, name string, args ...string) (string, string, int, error)
- func (e *UserAwareExecutor) Stat(path string) (os.FileInfo, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Executor ¶
type Executor interface {
// Run executes a command and returns stdout, stderr, and exit code.
Run(ctx context.Context, name string, args ...string) (stdout, stderr string, exitCode int, err error)
// RunWithTimeout executes a command with a timeout.
RunWithTimeout(ctx context.Context, timeout time.Duration, name string, args ...string) (stdout, stderr string, exitCode int, err error)
// RunInDir executes a command with a working directory and timeout.
// Avoids shell quoting issues with cd on Windows.
RunInDir(ctx context.Context, dir string, timeout time.Duration, name string, args ...string) (stdout, stderr string, exitCode int, err error)
// RunAsUser runs a shell command as a specific user (for root -> user delegation).
RunAsUser(ctx context.Context, username, command string) (string, error)
// LookPath searches for an executable in PATH.
LookPath(name string) (string, error)
// FileExists checks if a file exists and is not a directory.
FileExists(path string) bool
// DirExists checks if a directory exists.
DirExists(path string) bool
// ReadFile reads a file's contents.
ReadFile(path string) ([]byte, error)
// ReadDir lists directory entries.
ReadDir(path string) ([]os.DirEntry, error)
// Stat returns file info.
Stat(path string) (os.FileInfo, error)
// Hostname returns the system hostname.
Hostname() (string, error)
// Getenv reads an environment variable.
Getenv(key string) string
// IsRoot returns true if the process is running as root.
IsRoot() bool
// CurrentUser returns the current OS user.
CurrentUser() (*user.User, error)
// HomeDir returns the home directory for a given username.
HomeDir(username string) (string, error)
// Glob returns filenames matching a pattern.
Glob(pattern string) ([]string, error)
// EvalSymlinks resolves symbolic links in a path. Returns the resolved
// canonical path. If the path is not a symlink, returns it unchanged.
EvalSymlinks(path string) (string, error)
// LoggedInUser returns the actual logged-in console user.
// When running as root on macOS (e.g., via LaunchDaemon), this detects the
// real console user via /dev/console rather than returning root.
// Falls back to CurrentUser() when not root or on non-macOS platforms.
LoggedInUser() (*user.User, error)
// GOOS returns the runtime operating system.
GOOS() string
// IsAppleCLTStub reports whether binPath is an Apple Command Line Tools
// shim (under /usr/bin/) that would trigger the macOS "install developer
// tools" GUI prompt when invoked. Returns false on non-darwin systems, for
// paths outside /usr/bin/, or when CLT is installed. Detectors must guard
// every Run/RunWithTimeout on /usr/bin/-resolved binaries with this check
// to avoid disrupting end users on machines without CLT installed.
IsAppleCLTStub(ctx context.Context, binPath string) bool
// DiskCapacityBytes returns the total bytes on the filesystem containing
// path. Returns 0 on any error (lookup failures shouldn't block a scan).
DiskCapacityBytes(path string) uint64
}
Executor defines the interface for all OS interactions. Every detector depends on this interface, enabling full unit-test coverage via mocks.
func NewUserAwareExecutor ¶ added in v1.10.0
NewUserAwareExecutor returns a wrapped executor that delegates command execution to the given user when running as root on Unix. If username is empty or the process is not root, all calls pass through to the inner executor unchanged.
type Mock ¶
type Mock struct {
// contains filtered or unexported fields
}
Mock implements Executor for unit testing.
func (*Mock) DiskCapacityBytes ¶ added in v1.11.3
func (*Mock) EvalSymlinks ¶ added in v1.11.1
func (*Mock) FileExists ¶
func (*Mock) IsAppleCLTStub ¶ added in v1.11.3
func (*Mock) RunWithTimeout ¶
func (*Mock) SetAppleCLTInstalled ¶ added in v1.11.3
SetAppleCLTInstalled controls the value returned by IsAppleCLTStub. Default is false (CLT not installed → binaries in the appleCLTStubBinaries allowlist are reported as stubs; other /usr/bin/ paths are unaffected).
func (*Mock) SetCommand ¶
func (*Mock) SetCommandError ¶
func (*Mock) SetDiskCapacityBytes ¶ added in v1.11.3
SetDiskCapacityBytes stubs the result of DiskCapacityBytes(path).
func (*Mock) SetHomeDir ¶
func (*Mock) SetHostname ¶
func (*Mock) SetLoggedInUserError ¶ added in v1.11.3
SetLoggedInUserError makes LoggedInUser return (nil, err). Pass nil to reset to default behavior (delegating to CurrentUser).
func (*Mock) SetSymlink ¶ added in v1.11.1
SetSymlink stubs a symlink resolution: EvalSymlinks(path) -> target. If a path is not registered, EvalSymlinks returns the path unchanged (matching the behavior of filepath.EvalSymlinks on a non-symlink).
func (*Mock) SetUsername ¶
type Real ¶
type Real struct {
// contains filtered or unexported fields
}
Real implements Executor using actual OS calls.
func (*Real) DiskCapacityBytes ¶ added in v1.11.3
func (*Real) EvalSymlinks ¶ added in v1.11.1
func (*Real) FileExists ¶
func (*Real) IsAppleCLTStub ¶ added in v1.11.3
IsAppleCLTStub reports whether binPath is an Apple Command Line Tools shim that would trigger the macOS "install developer tools" GUI prompt when invoked. Returns true iff:
- GOOS is darwin,
- binPath is a known Apple CLT shim (see appleCLTStubBinaries), AND
- Xcode Command Line Tools are not installed (`xcode-select -p` fails).
The CLT-presence result is cached per Real instance via sync.Once. The probe deliberately uses context.Background() (with its own 5 s timeout) rather than the caller-provided ctx: sync.Once consumes the slot on the first call, so a caller arriving with a canceled or near-deadline ctx could otherwise poison the cache with cltPresent=false and make every subsequent check treat real binaries as stubs. The caller's ctx is retained in the signature for symmetry with the Executor interface.
`xcode-select -p` itself does NOT trigger the install prompt — it just prints the developer-dir path or exits non-zero when CLT is absent.
func (*Real) RunWithTimeout ¶
type UserAwareExecutor ¶ added in v1.10.0
type UserAwareExecutor struct {
// contains filtered or unexported fields
}
UserAwareExecutor wraps an Executor and delegates LookPath and RunWithTimeout to the logged-in user when the process is running as root. This ensures that commands like "brew list", "pip3 list", "npm --version" etc. execute in the correct user context, since many tools refuse to run as root or return different results for different users.
All other Executor methods are forwarded unchanged.
func (*UserAwareExecutor) CurrentUser ¶ added in v1.10.0
func (e *UserAwareExecutor) CurrentUser() (*user.User, error)
func (*UserAwareExecutor) DirExists ¶ added in v1.10.0
func (e *UserAwareExecutor) DirExists(path string) bool
func (*UserAwareExecutor) DiskCapacityBytes ¶ added in v1.11.3
func (e *UserAwareExecutor) DiskCapacityBytes(path string) uint64
func (*UserAwareExecutor) EvalSymlinks ¶ added in v1.11.1
func (e *UserAwareExecutor) EvalSymlinks(path string) (string, error)
func (*UserAwareExecutor) FileExists ¶ added in v1.10.0
func (e *UserAwareExecutor) FileExists(path string) bool
func (*UserAwareExecutor) GOOS ¶ added in v1.10.0
func (e *UserAwareExecutor) GOOS() string
func (*UserAwareExecutor) Getenv ¶ added in v1.10.0
func (e *UserAwareExecutor) Getenv(key string) string
func (*UserAwareExecutor) Glob ¶ added in v1.10.0
func (e *UserAwareExecutor) Glob(pattern string) ([]string, error)
func (*UserAwareExecutor) HomeDir ¶ added in v1.10.0
func (e *UserAwareExecutor) HomeDir(username string) (string, error)
func (*UserAwareExecutor) Hostname ¶ added in v1.10.0
func (e *UserAwareExecutor) Hostname() (string, error)
func (*UserAwareExecutor) IsAppleCLTStub ¶ added in v1.11.3
func (e *UserAwareExecutor) IsAppleCLTStub(ctx context.Context, binPath string) bool
func (*UserAwareExecutor) IsRoot ¶ added in v1.10.0
func (e *UserAwareExecutor) IsRoot() bool
func (*UserAwareExecutor) LoggedInUser ¶ added in v1.10.0
func (e *UserAwareExecutor) LoggedInUser() (*user.User, error)
func (*UserAwareExecutor) LookPath ¶ added in v1.10.0
func (e *UserAwareExecutor) LookPath(name string) (string, error)
func (*UserAwareExecutor) ReadDir ¶ added in v1.10.0
func (e *UserAwareExecutor) ReadDir(path string) ([]os.DirEntry, error)
func (*UserAwareExecutor) ReadFile ¶ added in v1.10.0
func (e *UserAwareExecutor) ReadFile(path string) ([]byte, error)