system

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2025 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	Executable string
	Args       []string
	User       string
	Group      string
}

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.

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

func (r *MockSystem) MkHomeSubdirectory(subdirectory string) error

MkHomeSubdirectory takes a relative folder path and creates it recursively in the real user's home directory.

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 added in v0.10.0

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

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

ReadHomeDirFile takes a path relative to the real user's home dir, and reads the content from the file

func (*MockSystem) RemoveAllHome

func (r *MockSystem) RemoveAllHome(filePath string) error

RemoveAllHome recursively removes a file path from the user's home directory.

func (*MockSystem) Run

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

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

func (*MockSystem) RunExclusive

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

RunExclusive is a wrapper around Run that uses a mutex to ensure that only one of that particular command can be run at a time.

func (*MockSystem) RunMany

func (r *MockSystem) RunMany(commands ...*Command) error

RunMany takes a variadic number of Command's, and runs them in a loop, returning and error if any command fails.

func (*MockSystem) RunWithRetries

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

RunWithRetries executes the command, retrying utilising an exponential backoff pattern, which starts at 1 second. Retries will be attempted up to the specified maximum duration.

func (*MockSystem) SnapChannels added in v0.10.0

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

func (r *MockSystem) WriteHomeDirFile(filepath string, contents []byte) error

WriteHomeDirFile takes a path relative to the real user's home dir, and writes the contents specified to it.

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
}

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

func (s *System) MkHomeSubdirectory(subdirectory string) error

MkHomeSubdirectory takes a relative folder path and creates it recursively in the real user's home directory.

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

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

ReadHomeDirFile takes a path relative to the real user's home dir, and reads the content from the file

func (*System) RemoveAllHome

func (s *System) RemoveAllHome(filePath string) error

RemoveAllHome recursively removes a file path from the user's home directory.

func (*System) Run

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

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

func (*System) RunExclusive

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

RunExclusive is a wrapper around Run that uses a mutex to ensure that only one of that particular command can be run at a time.

func (*System) RunMany

func (s *System) RunMany(commands ...*Command) error

RunMany takes a variadic number of Command's, and runs them in a loop, returning and error if any command fails.

func (*System) RunWithRetries

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

RunWithRetries executes the command, retrying utilising an exponential backoff pattern, which starts at 1 second. Retries will be attempted up to the specified maximum duration.

func (*System) SnapChannels added in v0.10.0

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

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

WriteHomeDirFile takes a path relative to the real user's home dir, and writes the contents specified to it.

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)
	// RunMany takes multiple commands and runs them in sequence, returning an error on the
	// first error encountered.
	RunMany(commands ...*Command) error
	// RunExclusive is a wrapper around Run that uses a mutex to ensure that only one of that
	// particular command can be run at a time.
	RunExclusive(c *Command) ([]byte, error)
	// RunWithRetries executes the command, retrying utilising an exponential backoff pattern,
	// which starts at 1 second. Retries will be attempted up to the specified maximum duration.
	RunWithRetries(c *Command, maxDuration time.Duration) ([]byte, error)
	// WriteHomeDirFile takes a path relative to the real user's home dir, and writes the contents
	// specified to it.
	WriteHomeDirFile(filepath string, contents []byte) error
	// MkHomeSubdirectory takes a relative folder path and creates it recursively in the real
	// user's home directory.
	MkHomeSubdirectory(subdirectory string) error
	// RemoveAllHome recursively removes a file path from the user's home directory.
	RemoveAllHome(filePath string) error
	// ReadHomeDirFile reads a file from the user's home directory.
	ReadHomeDirFile(filepath string) ([]byte, error)
	// ReadFile reads a file with an arbitrary path from the system.
	ReadFile(filePath string) ([]byte, 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)
}

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