generated from golang-templates/seed
/
main.go
166 lines (144 loc) · 4.51 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Example program for parameters, showcasing the following:
// Sharing of parameters, "private" parameters, and complex parameters encoded in JSON.
// This example also registers the "verbose" parameter, in order to provide output in the task.
// Execute `go run . -v -shared "hello world"` as a first example.
// Execute `go run . -h` to see all details.
package main
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"github.com/goyek/goyek"
)
func main() {
flow := &goyek.Flow{}
sharedParam := flow.RegisterStringParam(goyek.StringParam{
Name: "shared",
Usage: "An example parameter shared between tasks",
Default: "default-value",
})
first := flow.Register(taskFirst(sharedParam))
flow.Register(taskSecond(flow, sharedParam))
flow.Register(taskComplexParam(flow))
flow.Register(taskListParam(flow))
flow.DefaultTask = first
flow.Main()
}
func taskFirst(sharedParam goyek.RegisteredStringParam) goyek.Task {
return goyek.Task{
Name: "first",
Usage: "Showcases a simple parameter",
Params: goyek.Params{sharedParam},
Action: func(tf *goyek.TF) {
tf.Log("Shared parameter named '" + sharedParam.Name() + "', value '" + sharedParam.Get(tf) + "'")
},
}
}
func taskSecond(flow *goyek.Flow, sharedParam goyek.RegisteredStringParam) goyek.Task {
// The following is a "private" parameter, only available to this task.
privateParam := flow.RegisterStringParam(goyek.StringParam{
Name: "private",
Usage: "A task-specific parameter",
Default: "special-default",
})
return goyek.Task{
Name: "second",
Usage: "Showcases shared and task-specific parameters",
Params: goyek.Params{sharedParam, privateParam},
Action: func(tf *goyek.TF) {
tf.Log("Shared parameter named '" + sharedParam.Name() + "', value '" + sharedParam.Get(tf) + "'")
tf.Log("Private parameter named '" + privateParam.Name() + "', value '" + privateParam.Get(tf) + "'")
},
}
}
// complexParam is an example for a serialized complex type.
type complexParam struct {
StringValue string `json:"stringValue"`
IntValue int `json:"intValue"`
}
// complexParamValue is a wrapper over the complex type, serializing it as JSON.
// While it is possible to implement the goyek.Value interface on the
// complex parameter itself, it is better to separate these concerns.
type complexParamValue complexParam
func (value *complexParamValue) Set(s string) error {
err := json.Unmarshal([]byte(s), value)
if err != nil {
err = errors.New("parse error")
}
return err
}
func (value *complexParamValue) Get() interface{} {
return complexParam(*value)
}
func (value *complexParamValue) String() string {
bytes, _ := json.Marshal(value)
return string(bytes)
}
func (value *complexParamValue) IsBool() bool {
return false
}
// taskComplexParam showcases complex parameters, JSON encoded.
//
// Execute `go run . -v complex -json "{\"stringValue\":\"abc\"}"` as an example.
func taskComplexParam(flow *goyek.Flow) goyek.Task {
privateParam := flow.RegisterValueParam(goyek.ValueParam{
Name: "json",
Usage: "A complex parameter",
NewValue: func() goyek.ParamValue {
return &complexParamValue{
StringValue: "default",
IntValue: 123,
}
},
})
return goyek.Task{
Name: "complex",
Usage: "Showcases complex parameters",
Params: goyek.Params{privateParam},
Action: func(tf *goyek.TF) {
tf.Log("Private parameter named '" + privateParam.Name() +
"', value '" + fmt.Sprintf("%v", privateParam.Get(tf).(complexParam)) + "'")
},
}
}
type listParamValue []int
func (value *listParamValue) Set(s string) error {
v, err := strconv.Atoi(s)
if err != nil {
err = errors.New("parse error")
}
*value = append(*value, v)
return err
}
func (value *listParamValue) Get() interface{} {
return []int(*value)
}
func (value *listParamValue) String() string {
return fmt.Sprintf("%v", *value)
}
func (value *listParamValue) IsBool() bool {
return false
}
// taskListParam showcases repeatable parameters.
//
// Execute `go run . -v list -port 1 -port 2 -port 3` as an example.
func taskListParam(flow *goyek.Flow) goyek.Task {
privateParam := flow.RegisterValueParam(goyek.ValueParam{
Name: "port",
Usage: "Integer parameter, can be repeated",
NewValue: func() goyek.ParamValue {
var value listParamValue
return &value
},
})
return goyek.Task{
Name: "list",
Usage: "Showcases list parameters",
Params: goyek.Params{privateParam},
Action: func(tf *goyek.TF) {
tf.Log("Private parameter named '" + privateParam.Name() +
"', value " + fmt.Sprintf("%v", privateParam.Get(tf).([]int)))
},
}
}