Documentation
¶
Overview ¶
Package taskflow helps implementing build automation. It is intended to be used in concert with the "go run" command, to run a program which implements the build pipeline (called taskflow). A taskflow consists of a set of registered tasks. A task has a name, can have a defined command, which is a function with signature
func (*taskflow.TF)
and can have dependencies (already defined tasks).
When the taskflow is executed for given tasks, then the tasks' commands are run in the order defined by their dependencies. The task's dependencies are run in a recusrive manner, however each is going to be run at most once.
The taskflow is interupted in case a command fails. Within these functions, use the Error, Fail or related methods to signal failure.
Example ¶
package main import ( "github.com/pellared/taskflow" ) func main() { flow := taskflow.New() task1 := flow.MustRegister(taskflow.Task{ Name: "task-1", Description: "Print Go version", Command: taskflow.Exec("go", "version"), }) task2 := flow.MustRegister(taskflow.Task{ Name: "task-2", Command: func(tf *taskflow.TF) { tf.Skip("skipping") }, }) task3 := flow.MustRegister(taskflow.Task{ Name: "task-3", Command: func(tf *taskflow.TF) { tf.Error("hello from", tf.Name()) tf.Log("this will be printed") }, }) flow.MustRegister(taskflow.Task{ Name: "all", Dependencies: taskflow.Deps{task1, task2, task3}, }) flow.Main() }
Output:
Index ¶
- Constants
- Variables
- func Exec(name string, args ...string) func(*TF)
- type Deps
- type ParamError
- type Params
- func (p Params) SetBool(key string, v bool)
- func (p Params) SetDate(key string, v time.Time, layout string)
- func (p Params) SetDuration(key string, v time.Duration)
- func (p Params) SetInt(key string, v int)
- func (p Params) SetJSON(key string, v interface{}) error
- func (p Params) SetText(key string, v encoding.TextMarshaler) error
- type RegisteredTask
- type RunResult
- type Runner
- type TF
- func (tf *TF) Cmd(name string, args ...string) *exec.Cmd
- func (tf *TF) Context() context.Context
- func (tf *TF) Error(args ...interface{})
- func (tf *TF) Errorf(format string, args ...interface{})
- func (tf *TF) Fail()
- func (tf *TF) FailNow()
- func (tf *TF) Failed() bool
- func (tf *TF) Fatal(args ...interface{})
- func (tf *TF) Fatalf(format string, args ...interface{})
- func (tf *TF) Log(args ...interface{})
- func (tf *TF) Logf(format string, args ...interface{})
- func (tf *TF) Name() string
- func (tf *TF) Output() io.Writer
- func (tf *TF) Params() TFParams
- func (tf *TF) Skip(args ...interface{})
- func (tf *TF) SkipNow()
- func (tf *TF) Skipf(format string, args ...interface{})
- func (tf *TF) Skipped() bool
- func (tf *TF) Verbose() bool
- type TFParams
- func (p TFParams) Bool(key string) bool
- func (p TFParams) Date(key string, layout string) time.Time
- func (p TFParams) Duration(key string) time.Duration
- func (p TFParams) Float64(key string) float64
- func (p TFParams) Int(key string) int
- func (p TFParams) ParseJSON(key string, v interface{})
- func (p TFParams) ParseText(key string, v encoding.TextUnmarshaler)
- func (p TFParams) String(key string) string
- type Task
- type Taskflow
Examples ¶
Constants ¶
const ( // CodePass indicates that taskflow passed. CodePass = 0 // CodeFailure indicates that taskflow failed. CodeFailure = 1 // CodeInvalidArgs indicates that got invalid input. CodeInvalidArgs = 2 )
Variables ¶
var DefaultOutput io.Writer = os.Stdout
DefaultOutput is the default output used by Taskflow if it is not set.
Functions ¶
Types ¶
type ParamError ¶
type ParamError struct { Key string // the parameter's key Err error // the reason the conversion failure, e.g. *strconv.NumError }
ParamError records an error during parameter conversion.
func (*ParamError) Error ¶
func (e *ParamError) Error() string
type Params ¶
Params represents Taskflow parameters used within Taskflow. The default values set in the struct are overridden in Run method.
func (Params) SetDuration ¶
SetDuration sets default value for given parameter using time.Duration.String.
type RegisteredTask ¶
type RegisteredTask struct {
// contains filtered or unexported fields
}
RegisteredTask represents a task that has been registered to a Taskflow. It can be used as a dependency for another Task.
type RunResult ¶
type RunResult struct {
// contains filtered or unexported fields
}
RunResult contains the results of a Command run.
func (RunResult) Failed ¶
Failed returns true if a command failed. Failure can be caused by invocation of Error, Fail or related methods or a panic.
type Runner ¶
type Runner struct { Ctx context.Context TaskName string Output io.Writer Params Params Verbose bool }
Runner is used to run a Command.
type TF ¶
type TF struct {
// contains filtered or unexported fields
}
TF is a type passed to Task's Command function to manage task state.
A Task ends when its Command function returns or calls any of the methods FailNow, Fatal, Fatalf, SkipNow, Skip, or Skipf.
All methods must be called only from the goroutine running the Command function.
func (*TF) Cmd ¶
Cmd is like exec.Command, but it assignes tf's context and assigns Stdout and Stderr to tf's output.
func (*TF) Error ¶
func (tf *TF) Error(args ...interface{})
Error is equivalent to Log followed by Fail.
func (*TF) Fail ¶
func (tf *TF) Fail()
Fail marks the function as having failed but continues execution.
func (*TF) FailNow ¶
func (tf *TF) FailNow()
FailNow marks the function as having failed and stops its execution by calling runtime.Goexit (which then runs all deferred calls in the current goroutine). It finishes the whole taskflow.
func (*TF) Fatal ¶
func (tf *TF) Fatal(args ...interface{})
Fatal is equivalent to Log followed by FailNow.
func (*TF) Log ¶
func (tf *TF) Log(args ...interface{})
Log formats its arguments using default formatting, analogous to Println, and prints the text to Output. A final newline is added. The text will be printed only if the task fails or taskflow is run in Verbose mode.
func (*TF) Logf ¶
Logf formats its arguments according to the format, analogous to Printf, and prints the text to Output. A final newline is added. The text will be printed only if the task fails or taskflow is run in Verbose mode.
func (*TF) Skip ¶
func (tf *TF) Skip(args ...interface{})
Skip is equivalent to Log followed by SkipNow.
func (*TF) SkipNow ¶
func (tf *TF) SkipNow()
SkipNow marks the task as having been skipped and stops its execution by calling runtime.Goexit (which then runs all deferred calls in the current goroutine). If a test fails (see Error, Errorf, Fail) and is then skipped, it is still considered to have failed. Taskflow will continue at the next task.
type TFParams ¶
type TFParams struct {
// contains filtered or unexported fields
}
TFParams represents Taskflow parameters accessible in task's command.
func (TFParams) Bool ¶
Bool converts the parameter to bool. It fails the task if the conversion failed. False is returned if the parameter was not set. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.
func (TFParams) Date ¶
Date converts the parameter to time.Time using time.Parse. It fails the task if the conversion failed. Zero value is returned if the parameter was not set. The layout defines the format by showing how the reference time, defined to be
Mon Jan 2 15:04:05 -0700 MST 2006
would be interpreted if it were the value; it serves as an example of the input format. The same interpretation will then be made to the input string.
func (TFParams) Duration ¶
Duration converts the parameter to time.Duration using time.ParseDuration. It fails the task if the conversion failed. 0 is returned if the parameter was not set. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
func (TFParams) Float64 ¶
Float64 converts the parameter to float64 accepting decimal and hexadecimal floating-point number syntax. It fails the task if the conversion failed. 0 is returned if the parameter was not set.
func (TFParams) Int ¶
Int converts the parameter to int using the Go syntax for integer literals. It fails the task if the conversion failed. 0 is returned if the parameter was not set.
func (TFParams) ParseJSON ¶
ParseJSON parses the parameter and stores the result in the value pointed to by v by using json.Unmarshal. It fails the task if the conversion failed or v is nil or not a pointer.
type Task ¶
Task represents a named task that can be registered. It can consist of a command (function that will be called when task is run) and dependencies (tasks which has to be run before this one).
type Taskflow ¶
type Taskflow struct { Output io.Writer // output where text is printed; os.Stdout by default Params Params // default parameters' values Verbose bool // when enabled, then the whole output will be always streamed DefaultTask RegisteredTask // task which is run when non is explicitly provided // contains filtered or unexported fields }
Taskflow is the root type of the package. Use Register methods to register all tasks and Run or Main method to execute provided tasks.
func (*Taskflow) Main ¶
func (f *Taskflow) Main()
Main parses the command-line arguments and runs the provided tasks. The usage is printed when invalid arguments are passed.
func (*Taskflow) MustRegister ¶
func (f *Taskflow) MustRegister(task Task) RegisteredTask
MustRegister registers the task. It panics in case of any error.