system

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotInstalled = errors.New("command not installed")

ErrNotInstalled is returned by DryRunWorker when a read-only command's binary is not found on the system.

Functions

func MkHomeSubdirectory added in v1.3.0

func MkHomeSubdirectory(w Worker, subdirectory string) error

MkHomeSubdirectory is a helper function that takes a relative folder path and creates it recursively in the real user's home directory using the Worker interface.

func ReadHomeDirFile added in v1.4.0

func ReadHomeDirFile(w Worker, filePath string) ([]byte, error)

ReadHomeDirFile reads a file at a path relative to the real user's home directory.

func RunExclusive added in v1.4.0

func RunExclusive(w Worker, c *Command) ([]byte, error)

RunExclusive acquires a per-executable mutex before running the command, ensuring only one instance of that executable runs at a time.

func RunMany added in v1.4.0

func RunMany(w Worker, commands ...*Command) error

RunMany takes multiple commands and runs them in sequence via the Worker, returning an error on the first error encountered.

func RunWithRetries added in v1.4.0

func RunWithRetries(w Worker, c *Command, maxDuration time.Duration) ([]byte, error)

RunWithRetries retries the command using exponential backoff, starting at 1 second. Retries will be attempted up to the specified maximum duration.

func WriteHomeDirFile added in v1.4.0

func WriteHomeDirFile(w Worker, filePath string, contents []byte) error

WriteHomeDirFile writes contents to a path relative to the real user's home directory, creating parent directories and adjusting ownership as needed.

Types

type Command

type Command struct {
	Executable string
	Args       []string
	User       string
	Group      string
	// ReadOnly indicates that the command only reads system state and makes no changes.
	// In dry-run mode, read-only commands are executed for real to provide accurate
	// conditional logic (e.g., checking if a service is already bootstrapped).
	ReadOnly bool
}

Command represents a given command to be executed by Concierge, along with the user and group that should be assumed if required.

func NewCommand

func NewCommand(executable string, args []string) *Command

NewCommand constructs a command to be run as the current user/group.

func NewCommandAs

func NewCommandAs(user string, group string, executable string, args []string) *Command

NewCommandAs constructs a command to be run as the specified user/group.

func (*Command) CommandString

func (c *Command) CommandString() string

CommandString puts together a command to be executed in a shell, including the `sudo` command and its arguments where appropriate. It verifies the executable exists via LookPath for consistent error behavior, but uses the short name in output.

type DryRunWorker added in v1.4.0

type DryRunWorker struct {
	// contains filtered or unexported fields
}

DryRunWorker is a Worker implementation that outputs what would be done without actually executing any commands or making any changes.

func NewDryRunWorker added in v1.4.0

func NewDryRunWorker(realSystem Worker) *DryRunWorker

NewDryRunWorker constructs a new DryRunWorker that wraps a real System for read operations while skipping execution operations.

func (*DryRunWorker) ChownAll added in v1.4.0

func (d *DryRunWorker) ChownAll(path string, user *user.User) error

ChownAll prints what ownership change would occur and returns success.

func (*DryRunWorker) MkdirAll added in v1.4.0

func (d *DryRunWorker) MkdirAll(path string, perm os.FileMode) error

MkdirAll prints what directory would be created and returns success.

func (*DryRunWorker) ReadFile added in v1.4.0

func (d *DryRunWorker) ReadFile(filePath string) ([]byte, error)

ReadFile delegates to real system for accurate conditional logic.

func (*DryRunWorker) RemovePath added in v1.4.0

func (d *DryRunWorker) RemovePath(path string) error

RemovePath prints what path would be removed and returns success.

func (*DryRunWorker) Run added in v1.4.0

func (d *DryRunWorker) Run(c *Command) ([]byte, error)

Run prints the command that would be executed and returns success. Read-only commands are delegated to the real system for accurate results.

func (*DryRunWorker) SnapChannels added in v1.4.0

func (d *DryRunWorker) SnapChannels(snap string) ([]string, error)

SnapChannels delegates to real system for accurate conditional logic.

func (*DryRunWorker) SnapInfo added in v1.4.0

func (d *DryRunWorker) SnapInfo(snap string, channel string) (*SnapInfo, error)

SnapInfo delegates to real system for accurate conditional logic.

func (*DryRunWorker) User added in v1.4.0

func (d *DryRunWorker) User() *user.User

User returns the real user - delegates to real system.

func (*DryRunWorker) WriteFile added in v1.4.0

func (d *DryRunWorker) WriteFile(filePath string, contents []byte, perm os.FileMode) error

WriteFile prints what file would be written and returns success.

type MockCommandReturn

type MockCommandReturn struct {
	Output []byte
	Error  error
}

MockCommandReturn contains mocked Output and Error from a given command.

type MockSystem

type MockSystem struct {
	ExecutedCommands   []string
	CreatedFiles       map[string]string
	CreatedDirectories []string
	Deleted            []string
	RemovedPaths       []string
	// contains filtered or unexported fields
}

MockSystem represents a struct that can emulate running commands.

func NewMockSystem

func NewMockSystem() *MockSystem

NewMockSystem constructs a new mock command

func (*MockSystem) ChownAll added in v1.3.0

func (r *MockSystem) ChownAll(path string, user *user.User) error

ChownAll recursively changes the ownership of a path to the specified user.

func (*MockSystem) MkdirAll added in v1.3.0

func (r *MockSystem) MkdirAll(path string, perm os.FileMode) error

MkdirAll creates a directory and all parent directories (mocked).

func (*MockSystem) MockCommandReturn

func (r *MockSystem) MockCommandReturn(command string, b []byte, err error)

MockCommandReturn sets a static return value representing command combined output, and a desired error return for the specified command.

func (*MockSystem) MockFile

func (r *MockSystem) MockFile(filePath string, contents []byte)

MockFile sets a faked expected file contents for a given file.

func (*MockSystem) MockSnapChannels

func (r *MockSystem) MockSnapChannels(snap string, channels []string)

MockSnapChannels mocks the set of available channels for a snap in the store.

func (*MockSystem) MockSnapStoreLookup

func (r *MockSystem) MockSnapStoreLookup(name, channel string, classic, installed bool) *Snap

MockSnapStoreLookup gets a new test snap and adds a mock snap into the mock test

func (*MockSystem) ReadFile

func (r *MockSystem) ReadFile(filePath string) ([]byte, error)

ReadFile takes a path and reads the content from the specified file.

func (*MockSystem) RemovePath added in v1.3.0

func (r *MockSystem) RemovePath(path string) error

RemovePath recursively removes a path from the filesystem (mocked).

func (*MockSystem) Run

func (r *MockSystem) Run(c *Command) ([]byte, error)

Run executes the command, returning the stdout/stderr where appropriate.

func (*MockSystem) SnapChannels

func (r *MockSystem) SnapChannels(snap string) ([]string, error)

SnapChannels returns the list of channels available for a given snap.

func (*MockSystem) SnapInfo

func (r *MockSystem) SnapInfo(snap string, channel string) (*SnapInfo, error)

SnapInfo returns information about a given snap, looking up details in the snap store using the snapd client API where necessary.

func (*MockSystem) User

func (r *MockSystem) User() *user.User

User returns the user the system executes commands on behalf of.

func (*MockSystem) WriteFile added in v1.4.0

func (r *MockSystem) WriteFile(filePath string, contents []byte, perm os.FileMode) error

WriteFile writes the given contents to the specified file path (mocked).

type Snap

type Snap struct {
	Name        string
	Channel     string
	Connections []string
}

Snap represents a given snap on a given channel.

func NewSnap

func NewSnap(name, channel string, connections []string) *Snap

NewSnap returns a new Snap package.

func NewSnapFromString

func NewSnapFromString(snap string) *Snap

NewSnapFromString returns a constructed snap instance, where the snap is specified in shorthand form, i.e. `charmcraft/latest/edge`.

type SnapInfo

type SnapInfo struct {
	Installed       bool
	Classic         bool
	TrackingChannel string
}

SnapInfo represents information about a snap fetched from the snapd API.

type System

type System struct {
	// contains filtered or unexported fields
}

System represents a struct that can run commands.

func NewSystem

func NewSystem(trace bool) (*System, error)

NewSystem constructs a new command system.

func (*System) ChownAll added in v1.3.0

func (s *System) ChownAll(path string, user *user.User) error

ChownAll recursively changes the ownership of a path to the specified user.

func (*System) MkdirAll added in v1.3.0

func (s *System) MkdirAll(path string, perm os.FileMode) error

MkdirAll creates a directory and all parent directories with the specified permissions.

func (*System) ReadFile

func (s *System) ReadFile(filePath string) ([]byte, error)

ReadFile takes a path and reads the content from the specified file.

func (*System) RemovePath added in v1.3.0

func (s *System) RemovePath(path string) error

RemovePath recursively removes a path from the filesystem.

func (*System) Run

func (s *System) Run(c *Command) ([]byte, error)

Run executes the command, returning the stdout/stderr where appropriate.

func (*System) SnapChannels

func (s *System) SnapChannels(snap string) ([]string, error)

SnapChannels returns the list of channels available for a given snap.

func (*System) SnapInfo

func (s *System) SnapInfo(snap string, channel string) (*SnapInfo, error)

SnapInfo returns information about a given snap, looking up details in the snap store using the snapd client API where necessary.

func (*System) User

func (s *System) User() *user.User

User returns a user struct containing details of the "real" user, which may differ from the current user when concierge is executed with `sudo`.

func (*System) WriteFile added in v1.4.0

func (s *System) WriteFile(filePath string, contents []byte, perm os.FileMode) error

WriteFile writes the given contents to the specified file path with the given permissions.

type Worker

type Worker interface {
	// User returns the 'real user' the system executes command as. This may be different from
	// the current user since the command is often executed with `sudo`.
	User() *user.User
	// Run takes a single command and runs it, returning the combined output and an error value.
	Run(c *Command) ([]byte, error)
	// ReadFile reads a file with an arbitrary path from the system.
	ReadFile(filePath string) ([]byte, error)
	// WriteFile writes the given contents to the specified file path with the given permissions.
	WriteFile(filePath string, contents []byte, perm os.FileMode) error
	// SnapInfo returns information about a given snap, looking up details in the snap
	// store using the snapd client API where necessary.
	SnapInfo(snap string, channel string) (*SnapInfo, error)
	// SnapChannels returns the list of channels available for a given snap.
	SnapChannels(snap string) ([]string, error)
	// RemovePath recursively removes a path from the filesystem.
	RemovePath(path string) error
	// MkdirAll creates a directory and all parent directories with the specified permissions.
	MkdirAll(path string, perm os.FileMode) error
	// ChownAll recursively changes the ownership of a path to the specified user.
	ChownAll(path string, user *user.User) error
}

Worker is an interface for a struct that can run commands on the underlying system.

Jump to

Keyboard shortcuts

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