Documentation
¶
Overview ¶
Package exec provides command execution utilities.
Index ¶
- Constants
- type CmdResult
- type CommandExecutor
- type Exec
- func (e *Exec) RunCmd(ctx context.Context, name string, args []string) (string, error)
- func (e *Exec) RunCmdFull(ctx context.Context, name string, args []string, cwd string, timeout int) (*CmdResult, error)
- func (e *Exec) RunCmdImpl(ctx context.Context, name string, args []string, cwd string) (string, error)
- func (e *Exec) RunCmdInDir(ctx context.Context, name string, args []string, cwd string) (string, error)
- func (e *Exec) RunPrivilegedCmd(ctx context.Context, name string, args []string) (string, error)
- func (e *Exec) RunPrivilegedCmdFull(ctx context.Context, name string, args []string, stdin string) (string, string, int, error)
- func (e *Exec) RunPrivilegedCmdWithStdin(ctx context.Context, name string, args []string, stdin string) (string, error)
- func (e *Exec) RunReadCmd(ctx context.Context, name string, args []string) ([]byte, error)
- type Manager
Constants ¶
const DefaultCommandTimeout = 10 * time.Minute
DefaultCommandTimeout bounds a command whose caller supplied no deadline of its own. It is a backstop rather than a budget: long enough for a package installation over a slow mirror, short enough that a command waiting on a prompt nobody will answer does not outlive the agent.
A caller's own deadline still wins whenever it falls earlier, because the deadline is derived from the context it passed.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CmdResult ¶
type CmdResult struct {
// Stdout contains the standard output of the command.
Stdout string
// Stderr contains the standard error output of the command.
Stderr string
// ExitCode is the exit code of the command.
ExitCode int
// DurationMs is the execution time in milliseconds.
DurationMs int64
}
CmdResult contains the full result of a command execution with separate stdout and stderr streams.
type CommandExecutor ¶
type CommandExecutor interface {
Execute(
ctx context.Context,
name string,
args []string,
cwd string,
) (string, error)
// ExecuteWithStdin runs the command with stdin written to its standard
// input. Implementations must not log stdin.
ExecuteWithStdin(
ctx context.Context,
name string,
args []string,
cwd string,
stdin string,
) (string, error)
}
CommandExecutor executes OS commands. The default implementation runs real commands via os/exec. Tests inject a mock to assert commands without executing them.
Every method takes a context: it is what cancels a command that is still running when the agent shuts down, and what bounds one that would otherwise run forever.
type Exec ¶
type Exec struct {
// contains filtered or unexported fields
}
Exec disk implementation.
func (*Exec) RunCmd ¶
RunCmd executes the provided command with arguments, using the current working directory.
func (*Exec) RunCmdFull ¶
func (e *Exec) RunCmdFull( ctx context.Context, name string, args []string, cwd string, timeout int, ) (*CmdResult, error)
RunCmdFull executes the provided command with separate stdout and stderr capture, an optional working directory, and a timeout in seconds. A timeout of 0 defaults to 30 seconds.
func (*Exec) RunCmdImpl ¶
func (e *Exec) RunCmdImpl( ctx context.Context, name string, args []string, cwd string, ) (string, error)
RunCmdImpl executes the provided command with the specified arguments and an optional working directory. It delegates to the CommandExecutor.
func (*Exec) RunCmdInDir ¶
func (e *Exec) RunCmdInDir( ctx context.Context, name string, args []string, cwd string, ) (string, error)
RunCmdInDir executes the provided command with arguments, using the current provided directory.
func (*Exec) RunPrivilegedCmd ¶
RunPrivilegedCmd executes the provided command with arguments. When sudo is enabled, the original command is passed as an argument to "sudo".
func (*Exec) RunPrivilegedCmdFull ¶ added in v0.3.0
func (e *Exec) RunPrivilegedCmdFull( ctx context.Context, name string, args []string, stdin string, ) (string, string, int, error)
RunPrivilegedCmdFull runs name with args as RunPrivilegedCmd does, through sudo when privilege escalation is on, and gives the standard output, the standard error and the exit code apart. An exit code other than 0 is not an error.
stdin, when it is not empty, goes to the command's standard input: how a command gets a secret, which must never be in its arguments.
func (*Exec) RunPrivilegedCmdWithStdin ¶
func (e *Exec) RunPrivilegedCmdWithStdin( ctx context.Context, name string, args []string, stdin string, ) (string, error)
RunPrivilegedCmdWithStdin executes the provided command with arguments and writes stdin to the command's standard input. When sudo is enabled, the command is prepended with "sudo".
Pass secrets this way rather than in arguments: stdin is not interpreted by a shell, does not appear in the process list, and is never logged.
func (*Exec) RunReadCmd ¶ added in v0.3.0
RunReadCmd runs name with args to read the host, as gohai runs a command: its standard output is the data, its standard error is in the error, it runs with LC_ALL=C so that its output is the same in each locale, it stops after source.CommandTimeout, and it keeps at most source.MaxOutput of output. gohai's runner of a host is this, so that each read of a fact runs one way.
type Manager ¶
type Manager interface {
// RunCmd executes the provided command with arguments, using the current
// working directory.
RunCmd(
ctx context.Context,
name string,
args []string,
) (string, error)
// RunPrivilegedCmd executes the provided command with arguments.
// When sudo is enabled, the command is prepended with "sudo".
RunPrivilegedCmd(
ctx context.Context,
name string,
args []string,
) (string, error)
// RunPrivilegedCmdWithStdin executes the provided command with arguments
// and writes stdin to its standard input. When sudo is enabled, the
// command is prepended with "sudo". Use it to pass secrets, which must
// never be placed in arguments.
RunPrivilegedCmdWithStdin(
ctx context.Context,
name string,
args []string,
stdin string,
) (string, error)
// RunPrivilegedCmdFull runs a command through sudo when privilege
// escalation is on, with the standard output, the standard error and
// the exit code apart.
// stdin, when it is not empty, goes to the command's standard input.
RunPrivilegedCmdFull(
ctx context.Context,
name string,
args []string,
stdin string,
) (string, string, int, error)
// RunReadCmd runs a command to read the host, as gohai runs one: its
// standard output is the data and its standard error is in the error,
// with LC_ALL=C, a timeout and a limit on its output.
RunReadCmd(
ctx context.Context,
name string,
args []string,
) ([]byte, error)
// RunCmdFull executes a command with separate stdout/stderr capture,
// an optional working directory, and a timeout in seconds.
RunCmdFull(
ctx context.Context,
name string,
args []string,
cwd string,
timeout int,
) (*CmdResult, error)
}
Manager manager responsible for exec operations.
Every method takes a context. A command with no deadline of its own is bounded by DefaultCommandTimeout, and one still running when the agent shuts down is killed with it rather than outliving it.