executor

package
v1.10.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MockDirEntry added in v1.10.0

func MockDirEntry(name string, isDir bool) os.DirEntry

MockDirEntry creates an os.DirEntry for use with SetDirEntries.

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)
	// 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
}

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

func NewUserAwareExecutor(inner Executor, username string) Executor

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 NewMock

func NewMock() *Mock

func (*Mock) CurrentUser

func (m *Mock) CurrentUser() (*user.User, error)

func (*Mock) DirExists

func (m *Mock) DirExists(path string) bool

func (*Mock) FileExists

func (m *Mock) FileExists(path string) bool

func (*Mock) GOOS

func (m *Mock) GOOS() string

func (*Mock) Getenv

func (m *Mock) Getenv(key string) string

func (*Mock) Glob

func (m *Mock) Glob(pattern string) ([]string, error)

func (*Mock) HomeDir

func (m *Mock) HomeDir(_ string) (string, error)

func (*Mock) Hostname

func (m *Mock) Hostname() (string, error)

func (*Mock) IsRoot

func (m *Mock) IsRoot() bool

func (*Mock) LoggedInUser added in v1.9.2

func (m *Mock) LoggedInUser() (*user.User, error)

func (*Mock) LookPath

func (m *Mock) LookPath(name string) (string, error)

func (*Mock) ReadDir

func (m *Mock) ReadDir(path string) ([]os.DirEntry, error)

func (*Mock) ReadFile

func (m *Mock) ReadFile(path string) ([]byte, error)

func (*Mock) Run

func (m *Mock) Run(_ context.Context, name string, args ...string) (string, string, int, error)

func (*Mock) RunAsUser

func (m *Mock) RunAsUser(ctx context.Context, _ string, command string) (string, error)

func (*Mock) RunInDir added in v1.10.1

func (m *Mock) RunInDir(ctx context.Context, _ string, _ time.Duration, name string, args ...string) (string, string, int, error)

func (*Mock) RunWithTimeout

func (m *Mock) RunWithTimeout(ctx context.Context, _ time.Duration, name string, args ...string) (string, string, int, error)

func (*Mock) SetCommand

func (m *Mock) SetCommand(stdout, stderr string, exitCode int, name string, args ...string)

func (*Mock) SetCommandError

func (m *Mock) SetCommandError(err error, name string, args ...string)

func (*Mock) SetDir

func (m *Mock) SetDir(path string)

func (*Mock) SetDirEntries

func (m *Mock) SetDirEntries(path string, entries []os.DirEntry)

func (*Mock) SetEnv

func (m *Mock) SetEnv(key, value string)

func (*Mock) SetFile

func (m *Mock) SetFile(path string, content []byte)

func (*Mock) SetFileInfo

func (m *Mock) SetFileInfo(path string, info os.FileInfo)

func (*Mock) SetGOOS added in v1.9.2

func (m *Mock) SetGOOS(goos string)

func (*Mock) SetGlob

func (m *Mock) SetGlob(pattern string, matches []string)

func (*Mock) SetHomeDir

func (m *Mock) SetHomeDir(h string)

func (*Mock) SetHostname

func (m *Mock) SetHostname(h string)

func (*Mock) SetIsRoot

func (m *Mock) SetIsRoot(v bool)

func (*Mock) SetPath

func (m *Mock) SetPath(name, path string)

func (*Mock) SetUsername

func (m *Mock) SetUsername(u string)

func (*Mock) Stat

func (m *Mock) Stat(path string) (os.FileInfo, error)

type Real

type Real struct{}

Real implements Executor using actual OS calls.

func NewReal

func NewReal() *Real

func (*Real) CurrentUser

func (r *Real) CurrentUser() (*user.User, error)

func (*Real) DirExists

func (r *Real) DirExists(path string) bool

func (*Real) FileExists

func (r *Real) FileExists(path string) bool

func (*Real) GOOS

func (r *Real) GOOS() string

func (*Real) Getenv

func (r *Real) Getenv(key string) string

func (*Real) Glob

func (r *Real) Glob(pattern string) ([]string, error)

func (*Real) HomeDir

func (r *Real) HomeDir(username string) (string, error)

func (*Real) Hostname

func (r *Real) Hostname() (string, error)

func (*Real) IsRoot

func (r *Real) IsRoot() bool

func (*Real) LoggedInUser added in v1.9.2

func (r *Real) LoggedInUser() (*user.User, error)

func (*Real) LookPath

func (r *Real) LookPath(name string) (string, error)

func (*Real) ReadDir

func (r *Real) ReadDir(path string) ([]os.DirEntry, error)

func (*Real) ReadFile

func (r *Real) ReadFile(path string) ([]byte, error)

func (*Real) Run

func (r *Real) Run(ctx context.Context, name string, args ...string) (string, string, int, error)

func (*Real) RunAsUser

func (r *Real) RunAsUser(ctx context.Context, username, command string) (string, error)

func (*Real) RunInDir added in v1.10.1

func (r *Real) RunInDir(ctx context.Context, dir string, timeout time.Duration, name string, args ...string) (string, string, int, error)

func (*Real) RunWithTimeout

func (r *Real) RunWithTimeout(ctx context.Context, timeout time.Duration, name string, args ...string) (string, string, int, error)

func (*Real) Stat

func (r *Real) Stat(path string) (os.FileInfo, error)

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) 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) 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)

func (*UserAwareExecutor) Run added in v1.10.0

func (e *UserAwareExecutor) Run(ctx context.Context, name string, args ...string) (string, string, int, error)

func (*UserAwareExecutor) RunAsUser added in v1.10.0

func (e *UserAwareExecutor) RunAsUser(ctx context.Context, username, command string) (string, error)

func (*UserAwareExecutor) RunInDir added in v1.10.1

func (e *UserAwareExecutor) RunInDir(ctx context.Context, dir string, timeout time.Duration, name string, args ...string) (string, string, int, error)

func (*UserAwareExecutor) RunWithTimeout added in v1.10.0

func (e *UserAwareExecutor) RunWithTimeout(ctx context.Context, timeout time.Duration, name string, args ...string) (string, string, int, error)

func (*UserAwareExecutor) Stat added in v1.10.0

func (e *UserAwareExecutor) Stat(path string) (os.FileInfo, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL