-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathshell.go
More file actions
215 lines (179 loc) · 5.13 KB
/
shell.go
File metadata and controls
215 lines (179 loc) · 5.13 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package cmdchain
import (
"context"
"fmt"
"io"
"mvdan.cc/sh/v3/syntax"
"os/exec"
"strings"
)
func (c *chain) JoinShellCmd(command string) CommandBuilder {
return &shellChain{
command: command,
chain: c,
}
}
func (c *chain) JoinShellCmdWithContext(ctx context.Context, command string) CommandBuilder {
return &shellChain{
ctx: ctx,
command: command,
chain: c,
}
}
type shellChain struct {
ctx context.Context
command string
actions []func(CommandBuilder)
chain *chain
}
var buildShellChain = func(s *shellChain) CommandBuilder {
var err error
defer func() {
if err != nil {
s.chain.buildErrors.addError(fmt.Errorf("error parsing shell command: %w", err))
}
}()
parser := &shellParser{
chain: s.chain,
ctx: s.ctx,
actions: s.actions,
}
parser.program, err = syntax.NewParser().Parse(strings.NewReader(s.command), "")
if err != nil {
return s.chain
}
err = parser.Parse()
return s.chain
}
////
// Before joining the next command, here we build the current shell-command and apply all collected actions.
////
func (s *shellChain) Join(name string, args ...string) CommandBuilder {
return buildShellChain(s).Join(name, args...)
}
func (s *shellChain) JoinCmd(cmd *exec.Cmd) CommandBuilder {
return buildShellChain(s).JoinCmd(cmd)
}
func (s *shellChain) JoinWithContext(ctx context.Context, name string, args ...string) CommandBuilder {
return buildShellChain(s).JoinWithContext(ctx, name, args...)
}
func (s *shellChain) JoinShellCmd(command string) CommandBuilder {
return buildShellChain(s).JoinShellCmd(command)
}
func (s *shellChain) JoinShellCmdWithContext(ctx context.Context, command string) CommandBuilder {
return buildShellChain(s).JoinShellCmdWithContext(ctx, command)
}
func (s *shellChain) Finalize() FinalizedBuilder {
return buildShellChain(s).Finalize()
}
////
// Here we have to "collect" the actions which must be applied BEFORE the next command is joined.
////
func (s *shellChain) Apply(applier CommandApplier) CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.Apply(applier)
})
return s
}
func (s *shellChain) ApplyBeforeStart(applier CommandApplier) CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.ApplyBeforeStart(applier)
})
return s
}
func (s *shellChain) ForwardError() CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.ForwardError()
})
return s
}
func (s *shellChain) DiscardStdOut() CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.DiscardStdOut()
})
return s
}
func (s *shellChain) WithOutputForks(targets ...io.Writer) CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.WithOutputForks(targets...)
})
return s
}
func (s *shellChain) WithAdditionalOutputForks(targets ...io.Writer) CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.WithAdditionalOutputForks(targets...)
})
return s
}
func (s *shellChain) WithErrorForks(targets ...io.Writer) CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.WithErrorForks(targets...)
})
return s
}
func (s *shellChain) WithAdditionalErrorForks(targets ...io.Writer) CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.WithAdditionalErrorForks(targets...)
})
return s
}
func (s *shellChain) WithInjections(sources ...io.Reader) CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.WithInjections(sources...)
})
return s
}
func (s *shellChain) WithEmptyEnvironment() CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.WithEmptyEnvironment()
})
return s
}
func (s *shellChain) WithEnvironment(envMap ...interface{}) CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.WithEnvironment(envMap...)
})
return s
}
func (s *shellChain) WithEnvironmentMap(envMap map[interface{}]interface{}) CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.WithEnvironmentMap(envMap)
})
return s
}
func (s *shellChain) WithEnvironmentPairs(envMap ...string) CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.WithEnvironmentPairs(envMap...)
})
return s
}
func (s *shellChain) WithAdditionalEnvironment(envMap ...interface{}) CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.WithAdditionalEnvironment(envMap...)
})
return s
}
func (s *shellChain) WithAdditionalEnvironmentMap(envMap map[interface{}]interface{}) CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.WithAdditionalEnvironmentMap(envMap)
})
return s
}
func (s *shellChain) WithAdditionalEnvironmentPairs(envMap ...string) CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.WithAdditionalEnvironmentPairs(envMap...)
})
return s
}
func (s *shellChain) WithWorkingDirectory(workingDir string) CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.WithWorkingDirectory(workingDir)
})
return s
}
func (s *shellChain) WithErrorChecker(checker ErrorChecker) CommandBuilder {
s.actions = append(s.actions, func(c CommandBuilder) {
c.WithErrorChecker(checker)
})
return s
}