exec

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2025 License: MIT Imports: 14 Imported by: 16

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EnableEnvPrint = false //nolint:gochecknoglobals // Allowed for CLI disabling.

Functions

func WithTemplate

func WithTemplate[T any](
	c *CmdContext,
	templ string,
	data any,
	run func(c *CmdContext, file string) (T, error),
) (res T, err error)

WithTemplate renders a template to a temporary file and runs the `run` function on it.

Types

type ArgsFilter

type ArgsFilter func([]string) []string
var DefaultCredentialFilter ArgsFilter = NewCredentialFilter(defaultCredentialArgs)

DefaultCredentialFilter is the default credential filter which remove secret args from commands.

func NewCredentialFilter

func NewCredentialFilter(credArgs []string) ArgsFilter

NewCredentialFilter returns a filter you can set on `CmdContext` for filtering secret arguments you dont want in the log.

type CmdContext

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

CmdContext defines the command context to execute commands.

func NewCommandCtx

func NewCommandCtx(cwd string) *CmdContext

NewCommandCtx returns a simple command context.

func (*CmdContext) BaseArgs

func (c *CmdContext) BaseArgs() []string

BaseArgs returns the base arguments.

func (*CmdContext) BaseCmd

func (c *CmdContext) BaseCmd() string

BaseCmd returns the base command.

func (*CmdContext) Chain

func (c *CmdContext) Chain() CmdContextChain

CmdContextChain returns a command ctx where you can chain commands together. The context will fail the chain if any error happens.

func (*CmdContext) Check

func (c *CmdContext) Check(args ...string) error

Check checks if a command executed successfully (optionally use `exitCodeHandler`) Returns `CmdError` on error.

func (*CmdContext) CheckWithEC

func (c *CmdContext) CheckWithEC(handleExit ExitCodeHandler, args ...string) error

CheckWithEC executes a command and custom handles the exit code (will no use the internally set `exitCodeHandler`) Returns `CmdError` on error.

func (*CmdContext) Cwd

func (c *CmdContext) Cwd() string

GetCwd returns the working directory.

func (*CmdContext) Env added in v0.22.0

func (c *CmdContext) Env() []string

Env returns the environment values.

func (*CmdContext) Get

func (c *CmdContext) Get(args ...string) (string, error)

Get executes a command and gets the stdout (white-space trimmed). Returns `CmdError` on error.

func (*CmdContext) GetCombined

func (c *CmdContext) GetCombined(args ...string) (string, error)

GetCombined executes a command and gets the stdout and stderr (white-space trimmed). Returns `CmdError` on error.

func (*CmdContext) GetCombinedWithEC

func (c *CmdContext) GetCombinedWithEC(handleExit ExitCodeHandler, args ...string) (string, error)

GetCombinedWithEC executes a command and gets the stdout and stderr (white-space trimmed). and custom handles the exit code. Returns `CmdError` on error.

func (*CmdContext) GetSplit

func (c *CmdContext) GetSplit(args ...string) ([]string, error)

GetSplit executes a command and splits the output by newlines.

func (*CmdContext) GetSplitWithEC

func (c *CmdContext) GetSplitWithEC(handleExit ExitCodeHandler, args ...string) ([]string, error)

GetSplitWithEC executes a command with custom handling the exit code and splits the output by newlines.

func (*CmdContext) GetStdErr added in v0.22.0

func (c *CmdContext) GetStdErr(args ...string) (string, string, error)

Get executes a command and gets the stdout & stderr (white-space trimmed). Returns `CmdError` on error.

func (*CmdContext) GetStdErrWithEC added in v0.22.0

func (c *CmdContext) GetStdErrWithEC(
	handleExit ExitCodeHandler,
	args ...string,
) (string, string, error)

GetStdErrWithEC executes a command and gets the stdout & stderr (white-space trimmed). and custom handles the exit code. Returns `CmdError` on error.

func (*CmdContext) GetWithEC

func (c *CmdContext) GetWithEC(handleExit ExitCodeHandler, args ...string) (string, error)

GetWithEC executes a command and gets the stdout (white-space trimmed). and custom handles the exit code. Returns `CmdError` on error.

func (*CmdContext) WithStdin added in v0.24.0

func (c *CmdContext) WithStdin(r io.Reader) *CmdContext

StdinOnce sets a standard input reader to be used once.

type CmdContextBuilder

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

func NewCmdCtxBuilder

func NewCmdCtxBuilder() CmdContextBuilder

NewCmdCtxBuilder returns a builder to build a command context.

func (CmdContextBuilder) BaseArgs

func (c CmdContextBuilder) BaseArgs(args ...string) CmdContextBuilder

BaseArgs adds arguments to the base command.

func (CmdContextBuilder) BaseCmd

BaseCmd sets the base command.

func (CmdContextBuilder) Build

func (c CmdContextBuilder) Build() *CmdContext

Build finalizes the context.

func (CmdContextBuilder) Clone added in v0.22.0

Clone clones the builder.

func (CmdContextBuilder) CredentialFilter

func (c CmdContextBuilder) CredentialFilter(args []string) CmdContextBuilder

CredentialFilter sets the argument credential filter. If `args` is `nil`, the default filter is set.

func (CmdContextBuilder) Cwd

Cwd sets the working dir.

func (CmdContextBuilder) EnableCaptureError

func (c CmdContextBuilder) EnableCaptureError() CmdContextBuilder

EnableCaptureError enables capturing the `stderr`. When `SetQuiet` is used this is always the case.

func (CmdContextBuilder) EnableEnvPrint

func (c CmdContextBuilder) EnableEnvPrint() CmdContextBuilder

EnableEnvPrint enables printing the environment on failed commands.

func (CmdContextBuilder) Env

Env adds environment variables to the command. NOTE: To set completely no env. variables you need to use `EnvEmpty()` and not `nil`.

func (CmdContextBuilder) EnvEmpty

func (c CmdContextBuilder) EnvEmpty() CmdContextBuilder

EnvEmpty uses a completely empty environment.

func (CmdContextBuilder) EnvRemove added in v0.18.0

func (c CmdContextBuilder) EnvRemove(key ...string) CmdContextBuilder

func (CmdContextBuilder) ExitCodeHandler

func (c CmdContextBuilder) ExitCodeHandler(handler ExitCodeHandler) CmdContextBuilder

ExitCodeHandler set the exit code handler for this context.

func (CmdContextBuilder) NoQuiet

NoQuiet pipes stdout,stderr and logs the commands (default) Error reporting is not affected.

func (CmdContextBuilder) Paths

func (c CmdContextBuilder) Paths(paths ...string) CmdContextBuilder

Paths adds `paths` to the `PATH` env. variable on the context. Also enables to lookup the command executable and resolve it to an absolute path before calling the `exec` stdlib module. This is needed since the `exec.LookPath` does consult the normal `os.Getenv("PATH")` which is not what we want.

func (CmdContextBuilder) PrependCommand

func (c CmdContextBuilder) PrependCommand(cmd string, args ...string) CmdContextBuilder

PrependCommand prepends a command `cmd` with) `args` infront of the command.

func (CmdContextBuilder) Quiet

Quiet disables pipeing stdout,stderr and logging the commands. Error reporting is not affected.

type CmdContextChain

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

func (CmdContextChain) Check

func (c CmdContextChain) Check(args ...string) CmdContextChain

Check executes `Check` over the context and return the chain.

func (CmdContextChain) CheckWithEC

func (c CmdContextChain) CheckWithEC(handleExit ExitCodeHandler, args ...string) CmdContextChain

CheckWithEC executes `CheckWithEC` over the context and return the chain.

func (CmdContextChain) Error

func (c CmdContextChain) Error() error

Error returns the error on the chain.

func (CmdContextChain) Get

func (c CmdContextChain) Get(args ...string) (string, error)

Get executes `Get` over the context and returns the result.

func (CmdContextChain) GetSplit

func (c CmdContextChain) GetSplit(args ...string) ([]string, error)

GetSplit executes `GetSplit` over the context and returns the result.

func (CmdContextChain) GetSplitWithEC

func (c CmdContextChain) GetSplitWithEC(
	handleExit ExitCodeHandler,
	args ...string,
) ([]string, error)

GetSplitWithEC executes `GetSplitWithEC` over the context and returns the result.

func (CmdContextChain) GetWithEC

func (c CmdContextChain) GetWithEC(handleExit ExitCodeHandler, args ...string) (string, error)

GetWithEC executes `GetWithEC` over the context and returns the result.

type CmdError

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

func NewCmdError

func NewCmdError(
	cmd *exec.Cmd,
	stderr string,
	exitCode int,
	enableEnvPrint bool,
) CmdError

NewCmdError returns a CmdError.

func (CmdError) Error

func (c CmdError) Error() string

func (*CmdError) ExitCode

func (c *CmdError) ExitCode() int

func (*CmdError) Stderr

func (c *CmdError) Stderr() string

type ExitCodeHandler

type ExitCodeHandler func(cmdError *CmdError) error

func ExitCode0And1Success

func ExitCode0And1Success() ExitCodeHandler

ExitCode0And1Success is a exit code handler which reports success for exit code 1 and 0.

func ExitCode0And1SuccessVar

func ExitCode0And1SuccessVar(success *bool) ExitCodeHandler

ExitCode0And1SuccessVar is a exit code handler which reports success for exit code 1 and 0 and sets the boolean `success`.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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