Documentation
¶
Overview ¶
Package exe2 splits templating/env expansion and command running into their own method calls.
This allows using a test runner which simply logs executions instead of returning something. He is useful if there are no outputs or changes following from the command.
The order of executing the template, splitting the command into args and expanding the env variables may be important to you. If so simply switch the TemplateSplitExpand method with another implementation. It is only important that it returns exe2.SplitResult{Name:Args:Err:}.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ExecuteSetting ¶
type ExecuteSetting struct {
// contains filtered or unexported fields
}
ExecuteSetting holds options to apply to a command runner.
type Runner ¶
type Runner interface { RunE(ctx context.Context, splitResult SplitResult, settings ...SettingsFunc) (err error) RunWithOutputE(ctx context.Context, splitResult SplitResult, settings ...SettingsFunc) (out string, err error) }
Runner combines all run functions into one interface.
Example (WithDir) ¶
package main import ( "context" "os" "aduu.dev/utils/exe2" ) func main() { r := exe2.NewRunner() err := r.RunE(context.Background(), exe2.TemplateSplitExpand(`ls`, ""), exe2.WithDir(os.Getenv("HOME")), ) if err != nil { panic(err) } }
Output:
Example (WithTimeout) ¶
package main import ( "context" "time" "aduu.dev/utils/exe2" ) func main() { r := exe2.NewRunner() err := r.RunE(context.Background(), exe2.TemplateSplitExpand(`sleep 1`, ""), exe2.WithTimeout(time.Millisecond*10)) if err != nil { panic(err) } }
Output:
type SettingsFunc ¶
type SettingsFunc func(s *ExecuteSetting)
SettingsFunc is a function which modifies the execution setting.
func WithDir ¶
func WithDir(path string) SettingsFunc
WithDir sets working directory to use when running the command.
func WithTimeout ¶
func WithTimeout(duration time.Duration) SettingsFunc
WithTimeout sets a maximum duration to wait.
type SplitResult ¶
SplitResult stores the result of a split method and is passed to Runner.RunE or Runner.RunWithOutputE.
If SplitResult.Err != nil the Runner methods will return this error without executing the command.
func TemplateSplitExpand ¶
func TemplateSplitExpand(s string, obj interface{}) SplitResult
TemplateSplitExpand executes the template with obj, splits the commands into its parts and then expands env variables.
The expanding is done after the splitting. This is to avoid env variables injecting more arguments.
type TestRunner ¶
type TestRunner struct {
// contains filtered or unexported fields
}
TestRunner is a runner which does not run anything, but just stores given commands.
func NewTestRunner ¶
func NewTestRunner() *TestRunner
NewTestRunner creates a new Test Runner. It only stores commands inside its internal buffer.
func (*TestRunner) Commands ¶
func (r *TestRunner) Commands() [][]string
Commands returns all commands executed with the runner.
func (*TestRunner) RunE ¶
func (r *TestRunner) RunE(ctx context.Context, splitResult SplitResult, settings ...SettingsFunc) (err error)
func (*TestRunner) RunWithOutput ¶
func (r *TestRunner) RunWithOutput(ctx context.Context, splitResult SplitResult, settings ...SettingsFunc) string