platform

package
v0.16.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package platform provides OS / package-manager detection and the small set of privileged and unprivileged host operations the CLI relies on.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dispatch

func Dispatch(mac, linux func() error) error

Dispatch runs the handler matching the current OS, erroring on unsupported platforms. It is the single place per-OS behavior branches.

func Has

func Has(name string) bool

Has reports whether an executable is available on PATH.

func InstallPackage

func InstallPackage(pkg string) error

InstallPackage installs a package using the detected package manager.

func IsActiveService

func IsActiveService(unit string) bool

IsActiveService reports whether a systemd unit is active (Linux only).

func Output

func Output(name string, args ...string) (string, error)

Output runs a command and returns its standard output as a string.

func OutputContext added in v0.13.0

func OutputContext(ctx context.Context, name string, args ...string) (string, error)

OutputContext runs a command under a context and returns its standard output. Every host reading that shells out goes through here: a check that runs out of time must fail rather than hang.

func Pick added in v0.13.0

func Pick[T any](mac, linux T) (T, error)

Pick returns the value matching the current OS, erroring on unsupported platforms. It is Dispatch for the answers that are a value rather than an error, so per-OS knowledge keeps to the one branch point instead of growing a switch at every call site.

func RemoveFilePrivileged

func RemoveFilePrivileged(path string) error

RemoveFilePrivileged removes a root-owned file, ignoring a missing file.

func Run

func Run(name string, args ...string) error

Run executes a command, inheriting the parent's stdio.

func Sudo

func Sudo(args ...string) error

Sudo runs a command with elevated privileges, prompting once if needed. When already running as root it executes the command directly.

func SudoPrime

func SudoPrime(purpose string)

SudoPrime primes the sudo credential cache so subsequent privileged calls do not each prompt for a password. It is best-effort and never fatal: `sudo -v` demands a password unless *every* sudoers entry matching the user is NOPASSWD, so where sudo itself asks nothing it would still prompt — and without a TTY fail — an install every later `sudo <cmd>` would have completed. The non-interactive probe settles that without asking, and only the privileged calls themselves can say whether the host is usable.

func WriteFilePrivileged

func WriteFilePrivileged(path string, content []byte, mode os.FileMode) error

WriteFilePrivileged writes content to a root-owned path, creating parent directories as needed.

Types

type ExecRunner added in v0.3.0

type ExecRunner struct{}

ExecRunner is the production Runner. It carries no state and forwards every call to the package-level functions, so routing host steps through a Runner changes no observable behavior.

func (ExecRunner) RemoveFilePrivileged added in v0.3.0

func (ExecRunner) RemoveFilePrivileged(path string) error

func (ExecRunner) Run added in v0.3.0

func (ExecRunner) Run(name string, args ...string) error

func (ExecRunner) Sudo added in v0.3.0

func (ExecRunner) Sudo(args ...string) error

func (ExecRunner) WriteFilePrivileged added in v0.3.0

func (ExecRunner) WriteFilePrivileged(path string, content []byte, mode os.FileMode) error

type OS

type OS string

OS identifies a supported operating system.

const (
	// MacOS is Apple macOS (Darwin).
	MacOS OS = "macos"
	// Linux is Ubuntu/Debian Linux (the only supported family in v1).
	Linux OS = "linux"
)

func Current

func Current() (OS, error)

Current returns the running OS, erroring on unsupported platforms.

type PackageManager

type PackageManager string

PackageManager identifies a supported package manager.

const (
	// Brew is Homebrew (macOS).
	Brew PackageManager = "brew"
	// Apt is APT (Ubuntu/Debian).
	Apt PackageManager = "apt"
)

func DetectPackageManager

func DetectPackageManager() (PackageManager, error)

DetectPackageManager returns the available package manager for the host.

type Runner added in v0.3.0

type Runner interface {
	Run(name string, args ...string) error
	Sudo(args ...string) error
	WriteFilePrivileged(path string, content []byte, mode os.FileMode) error
	RemoveFilePrivileged(path string) error
}

Runner is the privileged/host exec surface the install/uninstall host steps mutate through. ExecRunner is the production adapter — each method delegates to the matching package function unchanged — so production behavior is identical; tests pass a fake that records calls to assert step ordering and rollback without touching the host.

Jump to

Keyboard shortcuts

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