-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.go
261 lines (232 loc) · 5.93 KB
/
run.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// This file is part of run.
//
// Copyright (C) 2020-2024 David Gamba Rios
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
/*
Package run provides a wrapper around os/exec with method chaining for modifying behaviour.
*/
package run
import (
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"os/exec"
)
var Logger = log.New(os.Stderr, "", log.LstdFlags)
var osStdout io.Writer = os.Stdout
var osStderr io.Writer = os.Stderr
type RunInfo struct {
Cmd []string // exposed for mocking purposes only
debug bool
env []string
dir string
Stdout io.Writer // exposed for mocking purposes only
Stderr io.Writer // exposed for mocking purposes only
stdin io.Reader
saveErr bool
printErr bool
ctx context.Context
mockFn MockFn
dryRun bool
}
type runInfoContextKey string
func ContextWithRunInfo(ctx context.Context, value *RunInfo) context.Context {
return context.WithValue(ctx, runInfoContextKey("runInfo"), value)
}
// CMD - Normal constructor.
func CMD(cmd ...string) *RunInfo {
r := &RunInfo{Cmd: cmd}
r.env = os.Environ()
r.Stdout = nil
r.Stderr = nil
r.ctx = context.Background()
r.printErr = true
return r
}
// CMD - Pulls RunInfo from context if it exists and if not it initializes a new one.
// Useful when loading a RunInfo from context to ease testing.
func CMDCtx(ctx context.Context, cmd ...string) *RunInfo {
v, ok := ctx.Value(runInfoContextKey("runInfo")).(*RunInfo)
if ok {
v.Cmd = cmd
return v
}
r := CMD(cmd...)
r.ctx = ctx
return r
}
func (r *RunInfo) Log() *RunInfo {
r.debug = true
return r
}
func (r *RunInfo) DryRun(b bool) *RunInfo {
r.dryRun = b
return r
}
// Stdin - connect caller's os.Stdin to command stdin.
func (r *RunInfo) Stdin() *RunInfo {
r.stdin = os.Stdin
return r
}
// In - Pass input to stdin.
func (r *RunInfo) In(input []byte) *RunInfo {
reader := bytes.NewReader(input)
r.stdin = reader
return r
}
// Env - Add key=value pairs to the environment of the process.
func (r *RunInfo) Env(env ...string) *RunInfo {
r.env = append(r.env, env...)
return r
}
// GetEnv - used for testing
func (r *RunInfo) GetEnv() []string {
return r.env
}
// Dir - specifies the working directory of the command.
func (r *RunInfo) Dir(dir string) *RunInfo {
r.dir = dir
return r
}
// GetDir - used for testing
func (r *RunInfo) GetDir() string {
return r.dir
}
// Ctx - specifies the context of the command to allow for timeouts.
func (r *RunInfo) Ctx(ctx context.Context) *RunInfo {
r.ctx = ctx
return r
}
// SaveErr - If the command starts but does not complete successfully, the error is of
// type *ExitError. In this case, save the error output into *ExitError.Stderr for retrieval.
//
// Retrieval can be done as shown below:
//
// err := run.CMD("./command", "arg").SaveErr().Run() // or .STDOutOutput() or .CombinedOutput()
// if err != nil {
// var exitErr *exec.ExitError
// if errors.As(err, &exitErr) {
// errOutput := exitErr.Stderr
func (r *RunInfo) SaveErr() *RunInfo {
r.saveErr = true
return r
}
// PrintErr - Regardless of operation mode, print errors to os.Stderr as they occur.
//
// Deprecated: This is the default behaviour, setting this is unnecessary and will be removed in the future.
func (r *RunInfo) PrintErr() *RunInfo {
r.printErr = true
return r
}
// DiscardErr - Don't print command error to stderr by default.
func (r *RunInfo) DiscardErr() *RunInfo {
r.printErr = false
return r
}
// CombinedOutput - Runs given CMD and returns STDOut and STDErr combined.
func (r *RunInfo) CombinedOutput() ([]byte, error) {
var b bytes.Buffer
r.Stdout = &b
r.Stderr = &b
err := r.Run()
return b.Bytes(), err
}
// STDOutOutput - Runs given CMD and returns STDOut only.
//
// Stderr output is discarded unless a call to SaveErr() or PrintErr() was made.
func (r *RunInfo) STDOutOutput() ([]byte, error) {
var b bytes.Buffer
r.Stdout = &b
err := r.Run()
return b.Bytes(), err
}
type MockFn func(*RunInfo) error
func (r *RunInfo) Mock(fn MockFn) *RunInfo {
r.mockFn = fn
return r
}
// Run - wrapper around os/exec CMD.Run()
//
// Run starts the specified command and waits for it to complete.
//
// The returned error is nil if the command runs, has no problems copying
// stdin, stdout, and stderr, and exits with a zero exit status.
//
// If the command starts but does not complete successfully, the error is of
// type *ExitError. Other error types may be returned for other situations.
//
// Examples:
//
// Run() // Output goes to os.Stdout and os.Stderr
// Run(out) // Sets the command's os.Stdout and os.Stderr to out.
// Run(out, outErr) // Sets the command's os.Stdout to out and os.Stderr to outErr.
func (r *RunInfo) Run(w ...io.Writer) error {
if r.debug {
msg := ""
if r.dryRun {
msg += "DRY-RUN "
}
msg += fmt.Sprintf("run %v", r.Cmd)
if r.dir != "" {
msg += fmt.Sprintf(" on %s", r.dir)
}
Logger.Println(msg)
}
if len(w) == 0 {
if r.Stdout == nil {
r.Stdout = osStdout
}
} else if len(w) == 1 {
r.Stdout = w[0]
r.Stderr = w[0]
} else if len(w) > 1 {
r.Stdout = w[0]
r.Stderr = w[1]
}
if r.printErr {
if r.Stderr == nil {
r.Stderr = osStderr
} else if r.Stderr != osStderr {
r.Stderr = io.MultiWriter(r.Stderr, osStderr)
}
}
var b bytes.Buffer
if r.saveErr {
if r.Stderr == nil {
r.Stderr = &b
} else {
r.Stderr = io.MultiWriter(r.Stderr, &b)
}
}
if r.mockFn != nil {
err := r.mockFn(r)
if err != nil && r.saveErr {
if exitErr, ok := err.(*exec.ExitError); ok {
exitErr.Stderr = b.Bytes()
}
}
return err
}
if r.dryRun {
return nil
}
c := exec.CommandContext(r.ctx, r.Cmd[0], r.Cmd[1:]...)
c.Dir = r.dir
c.Env = r.env
c.Stdout = r.Stdout
c.Stderr = r.Stderr
c.Stdin = r.stdin
err := c.Run()
if err != nil && r.saveErr {
if exitErr, ok := err.(*exec.ExitError); ok {
exitErr.Stderr = b.Bytes()
}
}
return err
}