Documentation
¶
Index ¶
- Variables
- func MkHomeSubdirectory(w Worker, subdirectory string) error
- func ReadHomeDirFile(w Worker, filePath string) ([]byte, error)
- func RunExclusive(w Worker, c *Command) ([]byte, error)
- func RunMany(w Worker, commands ...*Command) error
- func RunWithRetries(w Worker, c *Command, maxDuration time.Duration) ([]byte, error)
- func WriteHomeDirFile(w Worker, filePath string, contents []byte) error
- type Command
- type DryRunWorker
- func (d *DryRunWorker) ChownAll(path string, user *user.User) error
- func (d *DryRunWorker) MkdirAll(path string, perm os.FileMode) error
- func (d *DryRunWorker) ReadFile(filePath string) ([]byte, error)
- func (d *DryRunWorker) RemovePath(path string) error
- func (d *DryRunWorker) Run(c *Command) ([]byte, error)
- func (d *DryRunWorker) SnapChannels(snap string) ([]string, error)
- func (d *DryRunWorker) SnapInfo(snap string, channel string) (*SnapInfo, error)
- func (d *DryRunWorker) User() *user.User
- func (d *DryRunWorker) WriteFile(filePath string, contents []byte, perm os.FileMode) error
- type MockCommandReturn
- type MockSystem
- func (r *MockSystem) ChownAll(path string, user *user.User) error
- func (r *MockSystem) MkdirAll(path string, perm os.FileMode) error
- func (r *MockSystem) MockCommandReturn(command string, b []byte, err error)
- func (r *MockSystem) MockFile(filePath string, contents []byte)
- func (r *MockSystem) MockSnapChannels(snap string, channels []string)
- func (r *MockSystem) MockSnapStoreLookup(name, channel string, classic, installed bool) *Snap
- func (r *MockSystem) ReadFile(filePath string) ([]byte, error)
- func (r *MockSystem) RemovePath(path string) error
- func (r *MockSystem) Run(c *Command) ([]byte, error)
- func (r *MockSystem) SnapChannels(snap string) ([]string, error)
- func (r *MockSystem) SnapInfo(snap string, channel string) (*SnapInfo, error)
- func (r *MockSystem) User() *user.User
- func (r *MockSystem) WriteFile(filePath string, contents []byte, perm os.FileMode) error
- type Snap
- type SnapInfo
- type System
- func (s *System) ChownAll(path string, user *user.User) error
- func (s *System) MkdirAll(path string, perm os.FileMode) error
- func (s *System) ReadFile(filePath string) ([]byte, error)
- func (s *System) RemovePath(path string) error
- func (s *System) Run(c *Command) ([]byte, error)
- func (s *System) SnapChannels(snap string) ([]string, error)
- func (s *System) SnapInfo(snap string, channel string) (*SnapInfo, error)
- func (s *System) User() *user.User
- func (s *System) WriteFile(filePath string, contents []byte, perm os.FileMode) error
- type Worker
Constants ¶
This section is empty.
Variables ¶
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
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
ReadHomeDirFile reads a file at a path relative to the real user's home directory.
func RunExclusive ¶ added in v1.4.0
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
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
RunWithRetries retries the command using exponential backoff, starting at 1 second. Retries will be attempted up to the specified maximum duration.
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 ¶
NewCommand constructs a command to be run as the current user/group.
func NewCommandAs ¶
NewCommandAs constructs a command to be run as the specified user/group.
func (*Command) CommandString ¶
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.
type MockCommandReturn ¶
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 (*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.
type Snap ¶
Snap represents a given snap on a given channel.
func NewSnapFromString ¶
NewSnapFromString returns a constructed snap instance, where the snap is specified in shorthand form, i.e. `charmcraft/latest/edge`.
type System ¶
type System struct {
// contains filtered or unexported fields
}
System represents a struct that can run commands.
func (*System) ChownAll ¶ added in v1.3.0
ChownAll recursively changes the ownership of a path to the specified user.
func (*System) MkdirAll ¶ added in v1.3.0
MkdirAll creates a directory and all parent directories with the specified permissions.
func (*System) RemovePath ¶ added in v1.3.0
RemovePath recursively removes a path from the filesystem.
func (*System) SnapChannels ¶
SnapChannels returns the list of channels available for a given snap.
func (*System) SnapInfo ¶
SnapInfo returns information about a given snap, looking up details in the snap store using the snapd client API where necessary.
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.