-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuilder_command.go
190 lines (150 loc) · 4.96 KB
/
builder_command.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package cmdchain
import (
"fmt"
"io"
"os"
)
func (c *chain) Apply(applier CommandApplier) CommandBuilder {
applier(len(c.cmdDescriptors)-1, c.cmdDescriptors[len(c.cmdDescriptors)-1].command)
return c
}
func (c *chain) ApplyBeforeStart(applier CommandApplier) CommandBuilder {
i := len(c.cmdDescriptors) - 1
c.cmdDescriptors[i].commandApplier = append(c.cmdDescriptors[i].commandApplier, applier)
return c
}
func (c *chain) ForwardError() CommandBuilder {
c.cmdDescriptors[len(c.cmdDescriptors)-1].errToIn = true
return c
}
func (c *chain) DiscardStdOut() CommandBuilder {
c.cmdDescriptors[len(c.cmdDescriptors)-1].outToIn = false
return c
}
func (c *chain) WithOutputForks(targets ...io.Writer) CommandBuilder {
cmdDesc := &(c.cmdDescriptors[len(c.cmdDescriptors)-1])
cmdDesc.outputStreams = targets
if len(targets) > 1 {
cmdDesc.outFork = io.MultiWriter(targets...)
} else if len(targets) == 1 {
cmdDesc.outFork = targets[0]
}
return c
}
func (c *chain) WithAdditionalOutputForks(targets ...io.Writer) CommandBuilder {
cmdDesc := &(c.cmdDescriptors[len(c.cmdDescriptors)-1])
cmdDesc.outputStreams = append(cmdDesc.outputStreams, targets...)
if len(cmdDesc.outputStreams) > 1 {
cmdDesc.outFork = io.MultiWriter(cmdDesc.outputStreams...)
} else if len(cmdDesc.outputStreams) == 1 {
cmdDesc.outFork = cmdDesc.outputStreams[0]
}
return c
}
func (c *chain) WithErrorForks(targets ...io.Writer) CommandBuilder {
cmdDesc := &(c.cmdDescriptors[len(c.cmdDescriptors)-1])
cmdDesc.errorStreams = targets
if len(targets) > 1 {
cmdDesc.errFork = io.MultiWriter(targets...)
} else if len(targets) == 1 {
cmdDesc.errFork = targets[0]
}
return c
}
func (c *chain) WithAdditionalErrorForks(targets ...io.Writer) CommandBuilder {
cmdDesc := &(c.cmdDescriptors[len(c.cmdDescriptors)-1])
cmdDesc.errorStreams = append(cmdDesc.errorStreams, targets...)
if len(cmdDesc.errorStreams) > 1 {
cmdDesc.errFork = io.MultiWriter(cmdDesc.errorStreams...)
} else if len(cmdDesc.errorStreams) == 1 {
cmdDesc.errFork = cmdDesc.errorStreams[0]
}
return c
}
func (c *chain) WithInjections(sources ...io.Reader) CommandBuilder {
cmdDesc := &(c.cmdDescriptors[len(c.cmdDescriptors)-1])
cmdDesc.inputStreams = append(cmdDesc.inputStreams, sources...)
if len(sources) > 0 {
combineSrc := make([]io.Reader, 0, len(sources)+1)
if cmdDesc.command.Stdin != nil {
combineSrc = append(combineSrc, cmdDesc.command.Stdin)
}
for _, source := range sources {
if source != nil {
combineSrc = append(combineSrc, source)
}
}
if len(combineSrc) == 1 {
cmdDesc.command.Stdin = combineSrc[0]
} else if len(combineSrc) > 1 {
var err error
cmdDesc.command.Stdin, err = c.combineStream(combineSrc...)
if err != nil {
c.streamErrors.setError(len(c.cmdDescriptors)-1, err)
}
}
}
return c
}
func (c *chain) WithEmptyEnvironment() CommandBuilder {
cmdDesc := c.cmdDescriptors[len(c.cmdDescriptors)-1]
cmdDesc.command.Env = []string{}
return c
}
func (c *chain) WithEnvironmentMap(envMap map[interface{}]interface{}) CommandBuilder {
cmdDesc := c.cmdDescriptors[len(c.cmdDescriptors)-1]
for key, value := range envMap {
cmdDesc.command.Env = append(cmdDesc.command.Env, fmt.Sprintf("%v=%v", key, value))
}
return c
}
func (c *chain) WithEnvironment(envMap ...interface{}) CommandBuilder {
if len(envMap)%2 != 0 {
c.buildErrors.addError(fmt.Errorf("invalid count of environment arguments"))
return c
}
cmdDesc := c.cmdDescriptors[len(c.cmdDescriptors)-1]
for i := 0; i < len(envMap); i += 2 {
cmdDesc.command.Env = append(cmdDesc.command.Env, fmt.Sprintf("%v=%v", envMap[i], envMap[i+1]))
}
return c
}
func (c *chain) WithEnvironmentPairs(envMap ...string) CommandBuilder {
cmdDesc := c.cmdDescriptors[len(c.cmdDescriptors)-1]
for _, entry := range envMap {
cmdDesc.command.Env = append(cmdDesc.command.Env, entry)
}
return c
}
func (c *chain) WithAdditionalEnvironmentMap(envMap map[interface{}]interface{}) CommandBuilder {
cmdDesc := c.cmdDescriptors[len(c.cmdDescriptors)-1]
if len(cmdDesc.command.Env) == 0 {
cmdDesc.command.Env = os.Environ()
}
return c.WithEnvironmentMap(envMap)
}
func (c *chain) WithAdditionalEnvironment(envMap ...interface{}) CommandBuilder {
cmdDesc := c.cmdDescriptors[len(c.cmdDescriptors)-1]
if len(cmdDesc.command.Env) == 0 {
cmdDesc.command.Env = os.Environ()
}
return c.WithEnvironment(envMap...)
}
func (c *chain) WithAdditionalEnvironmentPairs(envMap ...string) CommandBuilder {
cmdDesc := c.cmdDescriptors[len(c.cmdDescriptors)-1]
pairs := cmdDesc.command.Env
if len(pairs) == 0 {
pairs = os.Environ()
}
pairs = append(pairs, envMap...)
return c.WithEnvironmentPairs(pairs...)
}
func (c *chain) WithWorkingDirectory(workingDir string) CommandBuilder {
cmdDesc := c.cmdDescriptors[len(c.cmdDescriptors)-1]
cmdDesc.command.Dir = workingDir
return c
}
func (c *chain) WithErrorChecker(errChecker ErrorChecker) CommandBuilder {
c.cmdDescriptors[len(c.cmdDescriptors)-1].errorChecker = errChecker
return c
}