-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcommand.go
200 lines (166 loc) · 4.08 KB
/
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
package command
import (
"errors"
"fmt"
"io"
"os/exec"
"strings"
"github.com/bitrise-io/go-utils/v2/env"
)
// ErrorFinder ...
type ErrorFinder func(out string) []string
// Opts ...
type Opts struct {
Stdout io.Writer
Stderr io.Writer
Stdin io.Reader
Env []string
Dir string
ErrorFinder ErrorFinder
}
// Factory ...
type Factory interface {
Create(name string, args []string, opts *Opts) Command
}
type factory struct {
envRepository env.Repository
}
// NewFactory ...
func NewFactory(envRepository env.Repository) Factory {
return factory{envRepository: envRepository}
}
// Create ...
func (f factory) Create(name string, args []string, opts *Opts) Command {
cmd := exec.Command(name, args...)
var collector *errorCollector
if opts != nil {
if opts.ErrorFinder != nil {
collector = &errorCollector{errorFinder: opts.ErrorFinder}
}
cmd.Stdout = opts.Stdout
cmd.Stderr = opts.Stderr
cmd.Stdin = opts.Stdin
// If Env is nil, the new process uses the current process's
// environment.
// If we pass env vars we want to append them to the
// current process's environment.
cmd.Env = append(f.envRepository.List(), opts.Env...)
cmd.Dir = opts.Dir
}
return &command{
cmd: cmd,
errorCollector: collector,
}
}
// Command ...
type Command interface {
PrintableCommandArgs() string
Run() error
RunAndReturnExitCode() (int, error)
RunAndReturnTrimmedOutput() (string, error)
RunAndReturnTrimmedCombinedOutput() (string, error)
Start() error
Wait() error
}
type command struct {
cmd *exec.Cmd
errorCollector *errorCollector
}
// PrintableCommandArgs ...
func (c command) PrintableCommandArgs() string {
return printableCommandArgs(false, c.cmd.Args)
}
// Run ...
func (c *command) Run() error {
c.wrapOutputs()
if err := c.cmd.Run(); err != nil {
return c.wrapError(err)
}
return nil
}
// RunAndReturnExitCode ...
func (c command) RunAndReturnExitCode() (int, error) {
c.wrapOutputs()
err := c.cmd.Run()
if err != nil {
err = c.wrapError(err)
}
exitCode := c.cmd.ProcessState.ExitCode()
return exitCode, err
}
// RunAndReturnTrimmedOutput ...
func (c command) RunAndReturnTrimmedOutput() (string, error) {
outBytes, err := c.cmd.Output()
outStr := string(outBytes)
if err != nil {
if c.errorCollector != nil {
c.errorCollector.collectErrors(outStr)
}
err = c.wrapError(err)
}
return strings.TrimSpace(outStr), err
}
// RunAndReturnTrimmedCombinedOutput ...
func (c command) RunAndReturnTrimmedCombinedOutput() (string, error) {
outBytes, err := c.cmd.CombinedOutput()
outStr := string(outBytes)
if err != nil {
if c.errorCollector != nil {
c.errorCollector.collectErrors(outStr)
}
err = c.wrapError(err)
}
return strings.TrimSpace(outStr), err
}
// Start ...
func (c command) Start() error {
c.wrapOutputs()
return c.cmd.Start()
}
// Wait ...
func (c command) Wait() error {
err := c.cmd.Wait()
if err != nil {
err = c.wrapError(err)
}
return err
}
func printableCommandArgs(isQuoteFirst bool, fullCommandArgs []string) string {
var cmdArgsDecorated []string
for idx, anArg := range fullCommandArgs {
quotedArg := fmt.Sprintf("\"%s\"", anArg)
if idx == 0 && !isQuoteFirst {
quotedArg = anArg
}
cmdArgsDecorated = append(cmdArgsDecorated, quotedArg)
}
return strings.Join(cmdArgsDecorated, " ")
}
func (c command) wrapError(err error) error {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
errorLines := []string{}
if c.errorCollector != nil {
errorLines = c.errorCollector.errorLines
}
return NewExitStatusError(c.PrintableCommandArgs(), exitErr, errorLines)
}
return fmt.Errorf("executing command failed (%s): %w", c.PrintableCommandArgs(), err)
}
func (c command) wrapOutputs() {
if c.errorCollector == nil {
return
}
if c.cmd.Stdout != nil {
outWriter := io.MultiWriter(c.errorCollector, c.cmd.Stdout)
c.cmd.Stdout = outWriter
} else {
c.cmd.Stdout = c.errorCollector
}
if c.cmd.Stderr != nil {
errWriter := io.MultiWriter(c.errorCollector, c.cmd.Stderr)
c.cmd.Stderr = errWriter
} else {
c.cmd.Stderr = c.errorCollector
}
}