-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_options.go
116 lines (99 loc) · 2.12 KB
/
command_options.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
package subproc
import (
"io"
"os"
)
var (
defaultCmdOptions = CmdOptions{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
StartBefore: func() error { return nil },
StartAfter: func() error { return nil },
WaitBefore: func() error { return nil },
WaitAfter: func() error { return nil },
FinalHook: func(error) {},
}
)
type CmdOptions struct {
CommandLineArguments []string
Environment []string
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
Response interface{}
StartBefore func() error
StartAfter func() error
WaitBefore func() error
WaitAfter func() error
FinalHook func(error)
ExtraFiles []*os.File
}
type CmdOption func(*CmdOptions)
func NewCmdOption(opts ...CmdOption) CmdOptions {
var options = defaultCmdOptions
for _, opt := range opts {
opt(&options)
}
return options
}
func CommandLineArguments(args ...string) CmdOption {
return func(o *CmdOptions) {
o.CommandLineArguments = args
}
}
func Environment(envs ...string) CmdOption {
return func(o *CmdOptions) {
o.Environment = envs
}
}
func Stdin(r io.Reader) CmdOption {
return func(o *CmdOptions) {
o.Stdin = r
}
}
func Stdout(w io.Writer) CmdOption {
return func(o *CmdOptions) {
o.Stdout = w
}
}
func Stderr(w io.Writer) CmdOption {
return func(o *CmdOptions) {
o.Stderr = w
}
}
func Response(resp interface{}) CmdOption {
return func(o *CmdOptions) {
o.Response = resp
}
}
func StartBefore(f func() error) CmdOption {
return func(o *CmdOptions) {
o.StartBefore = f
}
}
func StartAfter(f func() error) CmdOption {
return func(o *CmdOptions) {
o.StartAfter = f
}
}
func WaitBefore(f func() error) CmdOption {
return func(o *CmdOptions) {
o.WaitBefore = f
}
}
func WaitAfter(f func() error) CmdOption {
return func(o *CmdOptions) {
o.WaitAfter = f
}
}
func FinalHook(f func(error)) CmdOption {
return func(o *CmdOptions) {
o.FinalHook = f
}
}
func ExtraFiles(extrafiles ...*os.File) CmdOption {
return func(o *CmdOptions) {
o.ExtraFiles = extrafiles
}
}