-
Notifications
You must be signed in to change notification settings - Fork 54
/
step.go
65 lines (54 loc) · 1.45 KB
/
step.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package pipeline
import (
"fmt"
"github.com/fatih/color"
)
// Result is returned by a step to dispatch data to the next step or stage
type Result struct {
Error error
// dispatch any type
Data interface{}
// dispatch key value pairs
KeyVal map[string]interface{}
}
// Request is the result dispatched in a previous step.
type Request struct {
Data interface{}
KeyVal map[string]interface{}
}
// Step is the unit of work which can be concurrently or sequentially staged with other steps
type Step interface {
out
// Exec is invoked by the pipeline when it is run
Exec(*Request) *Result
// Cancel is invoked by the pipeline when one of the concurrent steps set Result{Error:err}
Cancel() error
}
type out interface {
Status(line string)
getCtx() *stepContextVal
setCtx(ctx *stepContextVal)
}
type stepContextVal struct {
pipelineKey string
name string
index int
concurrent bool
}
// StepContext type is embedded in types which need to statisfy the Step interface
type StepContext struct {
ctx *stepContextVal
}
func (sc *StepContext) getCtx() *stepContextVal {
return sc.ctx
}
func (sc *StepContext) setCtx(ctx *stepContextVal) {
sc.ctx = ctx
}
// Status is used to log status from a step
func (sc *StepContext) Status(line string) {
stepText := fmt.Sprintf("[step-%d]", sc.getCtx().index)
blue := color.New(color.FgBlue).SprintFunc()
line = blue(stepText) + "[" + sc.getCtx().name + "]: " + line
send(sc.getCtx().pipelineKey, line)
}