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 ¶
func SleepCommand ¶ added in v0.1.13
SleepCommand returns the appropriate sleep exec command for the current platform. This is here for testing purposes.
Windows Batch does not support sleep. Use powershell sleep there instead.
Types ¶
type Command ¶ added in v0.1.10
type Command struct { Command []string Setting ExecuteSetting }
Command is a test struct which stores a call. Options which are added by the Run-method itself are omitted.
type ExecuteSetting ¶
type ExecuteSetting struct { StdinFile string StdoutFile string StderrFile string // 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 (WithDeadline) ¶
package main import ( "context" "fmt" "time" "aduu.dev/utils/exe2" ) func main() { r := exe2.NewRunner() _ = 3 err := r.RunE(context.Background(), exe2.TemplateSplitExpand(exe2.SleepCommand("1"), ""), exe2.WithTimeout(time.Microsecond)) if err == nil { panic("should time out") } fmt.Println(err) }
Output: context deadline exceeded
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 (WithStdPipes) ¶
package main import ( "context" "os" "path/filepath" "aduu.dev/utils/exe2" ) func main() { file1 := filepath.Join(os.TempDir(), "file1") r := exe2.NewRunner() err := r.RunE(context.Background(), exe2.TemplateSplitExpand("printf hi\n\n", ""), exe2.WithStdoutFile(file1)) if err != nil { panic(err) } err = r.RunE(context.Background(), exe2.Split("cat"), exe2.WithStdinFile(file1), ) if err != nil { panic(err) } }
Output: hi
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 WithStderrFile ¶ added in v0.1.10
func WithStderrFile(path string) SettingsFunc
WithStderrFile opens the given file for writing with Stderr. Does not append.
func WithStdinFile ¶ added in v0.1.10
func WithStdinFile(path string) SettingsFunc
WithStdinFile opens the given file for reading with Stdin.
func WithStdoutFile ¶ added in v0.1.10
func WithStdoutFile(path string) SettingsFunc
WithStdoutFile opens the given file for writing with Stdout. Does not append.
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 Split ¶ added in v0.1.10
func Split(arg string) SplitResult
Split just splits the argument and returns the result.
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() []Command
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) RunWithOutputE ¶ added in v0.1.10
func (r *TestRunner) RunWithOutputE(ctx context.Context, splitResult SplitResult, settings ...SettingsFunc) (string, error)