v0.3.5 Latest Latest

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

Go to latest
Published: Nov 16, 2021 License: MIT Imports: 11 Imported by: 0



Package dash 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 dash.SplitResult{Name:Args:Err:}.




This section is empty.


This section is empty.


func SleepCommand

func SleepCommand(sleepTime string) string

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.

func WithStart

func WithStart(s *ExecuteSetting)

WithStart sets the command to run async.


type Command

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 {
	Dir string

	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 (


func main() {
	r := dash.NewRunner()

	// sleep does not exist on windows runner.
	if runtime.GOOS == "windows" {
		fmt.Println("context deadline exceeded")

	err := r.RunE(context.Background(),
		dash.TemplateSplitExpand(dash.SleepCommand("1"), ""),

	if err == nil {
		panic("should time out")

	// Make error message the same on all platforms.
	errStr := err.Error()
	if errStr == "signal: killed" {
		errStr = "context deadline exceeded"


context deadline exceeded
Example (WithDir)
package main

import (


func main() {
	r := dash.NewRunner()

	err := r.RunE(context.Background(),
		dash.TemplateSplitExpand(`ls`, ""),
	if err != nil {

Example (WithStdPipes)
package main

import (


func main() {
	file1 := filepath.Join(os.TempDir(), "file1")

	r := dash.NewRunner()

	err := r.RunE(context.Background(),
		dash.TemplateSplitExpand("printf hi\n\n", ""),
	if err != nil {

	err = r.RunE(context.Background(),
	if err != nil {


func NewRunner

func NewRunner(opts ...RunnerOpt) Runner

NewRunner creates the default runner. The default log-level is 5 (the highest) to hide debug messages.

type RunnerOpt added in v0.2.24

type RunnerOpt func(*runner)

RunnerOpt functions can be passed to NewRunner to configure it for all the runners' calls.

func LogLevel added in v0.2.24

func LogLevel(level klog.Level) RunnerOpt

LogLevel sets the log-level for log-messages like "Executing ..." .

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

func WithStderrFile(path string) SettingsFunc

WithStderrFile opens the given file for writing with Stderr. Does not append.

func WithStdinFile

func WithStdinFile(path string) SettingsFunc

WithStdinFile opens the given file for reading with Stdin.

func WithStdoutFile

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

type SplitResult struct {
	Name string
	Args []string

	Err error

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

func Split(arg string) *SplitResult

Split just splits the argument and returns the result.

func SplitTemplateExpand added in v0.2.1

func SplitTemplateExpand(s string, obj interface{}) *SplitResult

SplitTemplateExpand splits the argument, then templates the result one by one and then expands that one by one.

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

func (r *TestRunner) RunWithOutputE(ctx context.Context, splitResult *SplitResult, settings ...SettingsFunc) (string, error)


Path Synopsis

Jump to

Keyboard shortcuts

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