run

package
v0.0.0-...-44346bb Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package run is a package with utilities for running command and handling results.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Client is the Runner running commands.
	Client RunnerInterface

	// ErrCommandTemplate is the error returned when a CommandSpec's Command template is boggus.
	ErrCommandTemplate = errors.New("invalid command format template")

	// ErrTemplateError is the error returned when a CommandSpec's Error template is boggus.
	ErrTemplateError = errors.New("invalid error format template")
)

Functions

func Quiet

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

Quiet runs the current RunClient's Quiet() function.

Types

type CommandSet

type CommandSet []CommandSpec

CommandSet is set of commands to be executed together, IOW a command batch.

func (CommandSet) RunQuiet

func (s CommandSet) RunQuiet(ctx context.Context, data any) error

RunQuiet runs all the commands in a CommandSet, no command output is handled. All commands are run as a batch.

type CommandSpec

type CommandSpec struct {
	// Command is the command template i.e: "echo '{{.MyDataString}}'".
	Command string

	// Error is the error template, if the command fails this template is
	// used to build the error message, i.e: "failed to parse file {{.FileName}}".
	Error string
}

CommandSpec defines a Command template and an Error template. The data structure to be used with the templates is up to the user to define.

func (CommandSpec) RunQuiet

func (c CommandSpec) RunQuiet(ctx context.Context, data any) error

RunQuiet runs a CommandSpec command, no command output is handled.

type Result

type Result struct {
	// Exit code. Set to -1 if we failed to run the command.
	ExitCode int
	// Stderr or err.Error if we failed to run the command.
	StdErr string
	// Stdout or "" if we failed to run the command.
	StdOut string
	// Combined is the process' stdout and stderr combined.
	Combined string
}

Result wraps a command execution result.

func WithCombinedOutput

func WithCombinedOutput(ctx context.Context, name string, args ...string) *Result

WithCombinedOutput runs the current RunCLient's WithCombinedOutput function.

func WithOutput

func WithOutput(ctx context.Context, name string, args ...string) *Result

WithOutput runs the current RunClient's WithOutput() function.

func WithOutputTimeout

func WithOutputTimeout(ctx context.Context, timeout time.Duration, name string, args ...string) *Result

WithOutputTimeout runs the current RunClient's WithOutputTimeout() function.

func (Result) Error

func (e Result) Error() string

Error return an error containing the stderr content.

type Runner

type Runner struct{}

Runner implements the RunnerInterface and represents the runner running commands.

func (Runner) Quiet

func (r Runner) Quiet(ctx context.Context, name string, args ...string) error

Quiet runs a command and doesn't return a result, but an error in case of failure.

func (Runner) WithCombinedOutput

func (r Runner) WithCombinedOutput(ctx context.Context, name string, args ...string) *Result

WithCombinedOutput returns a result with stderr and stdout combined in the Combined member of Result.

func (Runner) WithOutput

func (r Runner) WithOutput(ctx context.Context, name string, args ...string) *Result

WithOutput runs a command and returns the result.

func (Runner) WithOutputTimeout

func (r Runner) WithOutputTimeout(ctx context.Context, timeout time.Duration, name string, args ...string) *Result

WithOutputTimeout runs a command with a defined timeout and returns its result.

type RunnerInterface

type RunnerInterface interface {
	// Quiet runs a command and doesn't return a result, but errors in case of failure.
	Quiet(ctx context.Context, name string, args ...string) error

	// WithOutput runs a command and returns the result.
	WithOutput(ctx context.Context, name string, args ...string) *Result

	// WithOutputTimeout runs a command with a defined timeout and returns its result.
	WithOutputTimeout(ctx context.Context, timeout time.Duration, name string, args ...string) *Result

	// WithCombinedOutput runs a command and returns a result with stderr and stdout
	// combined in the Combined member of Result.
	WithCombinedOutput(ctx context.Context, name string, args ...string) *Result
}

RunnerInterface defines the runner running commands.

Jump to

Keyboard shortcuts

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