generated from golang-templates/seed
/
example_test.go
119 lines (101 loc) · 2.37 KB
/
example_test.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
package goyek_test
import (
"flag"
"fmt"
"os"
"os/exec"
"github.com/goyek/goyek/v2"
"github.com/goyek/goyek/v2/middleware"
)
func Example() {
// define a task printing the message (configurable via flag)
hi := goyek.Define(goyek.Task{
Name: "hi",
Usage: "Greetings",
Action: func(a *goyek.A) {
a.Log("Hello world!")
},
})
// define a task running a command
goVer := goyek.Define(goyek.Task{
Name: "go-ver",
Usage: `Run "go version"`,
Action: func(a *goyek.A) {
cmd := exec.CommandContext(a.Context(), "go", "version")
cmd.Stdout = a.Output()
cmd.Stderr = a.Output()
if err := cmd.Run(); err != nil {
a.Error(err)
}
},
})
// define a pipeline
all := goyek.Define(goyek.Task{
Name: "all",
Deps: goyek.Deps{hi, goVer},
})
// configure middlewares
goyek.Use(middleware.ReportStatus)
// set the pipeline as the default task
goyek.SetDefault(all)
// run the build pipeline
goyek.Main(os.Args[1:])
/*
$ go run .
===== TASK hi
main.go:15: Hello world!
----- PASS: hi (0.00s)
===== TASK go-ver
go version go1.19.2 windows/amd64
----- PASS: go-ver (0.04s)
===== TASK all
----- NOOP: all (0.00s)
ok 0.039s
*/
}
func Example_flag() {
// use the same output for flow and flag
flag.CommandLine.SetOutput(goyek.Output())
// define a flag to configure flow output verbosity
verbose := flag.Bool("v", true, "print all tasks as they are run")
// define a flag used by a task
msg := flag.String("msg", "hello world", `message to display by "hi" task`)
// define a task printing the message (configurable via flag)
goyek.Define(goyek.Task{
Name: "hi",
Usage: "Greetings",
Action: func(a *goyek.A) {
a.Log(*msg)
},
})
// set the help message
usage := func() {
fmt.Println("Usage of build: [flags] [--] [tasks]")
goyek.Print()
fmt.Println("Flags:")
flag.PrintDefaults()
}
// parse the args
flag.Usage = usage
flag.Parse()
// configure middlewares
goyek.Use(middleware.ReportStatus)
if !*verbose {
goyek.Use(middleware.SilentNonFailed)
}
// run the build pipeline
goyek.SetUsage(usage)
goyek.Main(flag.Args())
/*
$ go run .
no task provided
Usage of build: [flags] [--] [tasks]
Tasks:
hi Greetings
Flags:
-msg string
message to display by "hi" task (default "hello world")
-v print all tasks as they are run (default true)
exit status 2
*/
}