-
Notifications
You must be signed in to change notification settings - Fork 7
/
prepared_command.go
217 lines (189 loc) · 5.44 KB
/
prepared_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
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
package shx
import (
"bytes"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
"github.com/carolynvs/magex/mgx"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
type PreparedCommand struct {
Cmd *exec.Cmd
StopOnError bool
}
// Command creates a default command. Stdout is logged in verbose mode. Stderr
// is sent to os.Stderr.
func Command(cmd string, args ...string) PreparedCommand {
c := exec.Command(cmd, args...)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Env = os.Environ()
return PreparedCommand{Cmd: c}
}
// String prints the command-line representation of the PreparedCommand.
func (c PreparedCommand) String() string {
return strings.Join(c.Cmd.Args, " ")
}
// Must immediately stops the build when the command fails.
func (c PreparedCommand) Must(stopOnError ...bool) PreparedCommand {
switch len(stopOnError) {
case 0:
c.StopOnError = true
case 1:
c.StopOnError = stopOnError[0]
default:
mgx.Must(fmt.Errorf("More than one value for Must(stopOnError ...string) was passed to the command %s", c))
}
return c
}
// Args appends additional arguments to the command.
func (c PreparedCommand) Args(args ...string) PreparedCommand {
c.Cmd.Args = append(c.Cmd.Args, args...)
return c
}
// CollapseArgs removes empty arguments from the argument list.
//
// This is helpful when sometimes a flag should be specified and
// sometimes it shouldn't.
func (c PreparedCommand) CollapseArgs() PreparedCommand {
result := make([]string, 0, len(c.Cmd.Args))
for _, arg := range c.Cmd.Args {
if arg != "" {
result = append(result, arg)
}
}
c.Cmd.Args = result
return c
}
// Env defines additional environment variables for the command.
// All ambient environment variables are included by default.
// Example:
// c.Env("X=1", "Y=2")
func (c PreparedCommand) Env(vars ...string) PreparedCommand {
for _, v := range vars {
c.Cmd.Env = append(c.Cmd.Env, v)
}
return c
}
// In sets the working directory of the command.
func (c PreparedCommand) In(dir string) PreparedCommand {
c.Cmd.Dir = dir
return c
}
// Stdin sets the command's stdin.
func (c PreparedCommand) Stdin(stdin io.Reader) PreparedCommand {
c.Cmd.Stdin = stdin
return c
}
// Stdout directs stdout from the command.
func (c PreparedCommand) Stdout(stdout io.Writer) PreparedCommand {
c.Cmd.Stdout = stdout
return c
}
// Stderr directs stderr from the command.
func (c PreparedCommand) Stderr(stderr io.Writer) PreparedCommand {
c.Cmd.Stderr = stderr
return c
}
// Runs a command silently, without writing to stdout/stderr.
func (c PreparedCommand) Silent() PreparedCommand {
c.Cmd.Stdout = nil
c.Cmd.Stderr = nil
return c
}
// Exec the prepared command, returning if the command was run and its
// exit code. Does not modify the configured outputs.
func (c PreparedCommand) Exec() (ran bool, code int, err error) {
if mg.Verbose() {
log.Println("exec:", c.Cmd.Path, strings.Join(c.Cmd.Args, " "))
}
err = c.Cmd.Run()
ran = sh.CmdRan(err)
code = sh.ExitStatus(err)
if err != nil {
if ran {
err = mg.Fatalf(code, `running "%s" failed with exit code %d`, c, code)
} else {
err = fmt.Errorf(`failed to run "%s: %v"`, c, err)
}
if c.StopOnError {
mgx.Must(err)
}
}
return ran, code, err
}
// Run the given command, directing stderr to os.Stderr and
// printing stdout to os.Stdout if mage was run with -v.
func (c PreparedCommand) Run() error {
if mg.Verbose() {
c.Cmd.Stdout = os.Stdout
} else {
c.Cmd.Stdout = nil
}
_, _, err := c.Exec()
return err
}
// RunV is like Run, but always writes the command output to os.Stdout.
func (c PreparedCommand) RunV() error {
c.Stdout(os.Stdout)
_, _, err := c.Exec()
return err
}
// RunE is like Run, but it only writes the command combined to os.Stderr when it fails.
func (c PreparedCommand) RunE() error {
output := &bytes.Buffer{}
c.Stdout(output)
c.Stderr(output)
_, _, err := c.Exec()
if err != nil {
fmt.Fprint(os.Stderr, output.String())
}
return err
}
// RunS is like Run, but the command output is not written to stdout/stderr.
func (c PreparedCommand) RunS() error {
_, _, err := c.Silent().Exec()
return err
}
// Output executes the prepared command, directing stderr to os.Stderr and
// printing stdout to os.Stdout if mage was run with -v. The command's stdout
// is always returned.
func (c PreparedCommand) Output() (string, error) {
stdout := &bytes.Buffer{}
if mg.Verbose() {
c.Cmd.Stdout = io.MultiWriter(stdout, os.Stdout)
} else {
c.Cmd.Stdout = stdout
}
_, _, err := c.Exec()
return strings.TrimSuffix(stdout.String(), "\n"), err
}
// OutputV is like Output, but it always writes the command output to os.Stdout.
func (c PreparedCommand) OutputV() (string, error) {
stdout := &bytes.Buffer{}
c.Cmd.Stdout = io.MultiWriter(stdout, os.Stdout)
_, _, err := c.Exec()
return strings.TrimSuffix(stdout.String(), "\n"), err
}
// OutputE is like Output, but it only writes the command output to os.Stderr when it fails.
func (c PreparedCommand) OutputE() (string, error) {
stdout := &bytes.Buffer{}
output := &bytes.Buffer{}
c.Stdout(io.MultiWriter(stdout, output))
c.Stderr(output)
_, _, err := c.Exec()
if err != nil {
fmt.Fprint(os.Stderr, output.String())
}
return strings.TrimSuffix(stdout.String(), "\n"), err
}
// Outputs is like Output, but the command output is not written to stdout/stderr.
func (c PreparedCommand) OutputS() (string, error) {
stdout := &bytes.Buffer{}
_, _, err := c.Stdout(stdout).Stderr(nil).Exec()
return strings.TrimSuffix(stdout.String(), "\n"), err
}