-
Notifications
You must be signed in to change notification settings - Fork 128
/
command.go
310 lines (271 loc) · 8.11 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package git
import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
)
// Command contains the name, arguments and environment variables of a command.
type Command struct {
name string
args []string
envs []string
timeout time.Duration
ctx context.Context
}
// CommandOptions contains options for running a command.
// If timeout is zero, DefaultTimeout will be used.
// If timeout is less than zero, no timeout will be set.
// If context is nil, context.Background() will be used.
type CommandOptions struct {
Args []string
Envs []string
Timeout time.Duration
Context context.Context
}
// String returns the string representation of the command.
func (c *Command) String() string {
if len(c.args) == 0 {
return c.name
}
return fmt.Sprintf("%s %s", c.name, strings.Join(c.args, " "))
}
// NewCommand creates and returns a new Command with given arguments for "git".
func NewCommand(args ...string) *Command {
return NewCommandWithContext(context.Background(), args...)
}
// NewCommandWithContext creates and returns a new Command with given arguments
// and context for "git".
func NewCommandWithContext(ctx context.Context, args ...string) *Command {
return &Command{
name: "git",
args: args,
ctx: ctx,
}
}
// AddArgs appends given arguments to the command.
func (c *Command) AddArgs(args ...string) *Command {
c.args = append(c.args, args...)
return c
}
// AddEnvs appends given environment variables to the command.
func (c *Command) AddEnvs(envs ...string) *Command {
c.envs = append(c.envs, envs...)
return c
}
// WithContext returns a new Command with the given context.
func (c Command) WithContext(ctx context.Context) *Command {
c.ctx = ctx
return &c
}
// WithTimeout returns a new Command with given timeout.
func (c Command) WithTimeout(timeout time.Duration) *Command {
c.timeout = timeout
return &c
}
// SetTimeout sets the timeout for the command.
func (c *Command) SetTimeout(timeout time.Duration) {
c.timeout = timeout
}
// AddOptions adds options to the command.
// Note: only the last option will take effect if there are duplicated options.
func (c *Command) AddOptions(opts ...CommandOptions) *Command {
for _, opt := range opts {
c.timeout = opt.Timeout
c.ctx = opt.Context
c.AddArgs(opt.Args...)
c.AddEnvs(opt.Envs...)
}
return c
}
// AddCommitter appends given committer to the command.
func (c *Command) AddCommitter(committer *Signature) *Command {
c.AddEnvs("GIT_COMMITTER_NAME="+committer.Name, "GIT_COMMITTER_EMAIL="+committer.Email)
return c
}
// DefaultTimeout is the default timeout duration for all commands.
const DefaultTimeout = time.Minute
// A limitDualWriter writes to W but limits the amount of data written to just N
// bytes. On the other hand, it passes everything to w.
type limitDualWriter struct {
W io.Writer // underlying writer
N int64 // max bytes remaining
prompted bool
w io.Writer
}
func (w *limitDualWriter) Write(p []byte) (int, error) {
if w.N > 0 {
limit := int64(len(p))
if limit > w.N {
limit = w.N
}
n, _ := w.W.Write(p[:limit])
w.N -= int64(n)
}
if !w.prompted && w.N <= 0 {
w.prompted = true
_, _ = w.W.Write([]byte("... (more omitted)"))
}
return w.w.Write(p)
}
// RunInDirOptions contains options for running a command in a directory.
type RunInDirOptions struct {
// Stdin is the input to the command.
Stdin io.Reader
// Stdout is the outputs from the command.
Stdout io.Writer
// Stderr is the error output from the command.
Stderr io.Writer
// Timeout is the duration to wait before timing out.
//
// Deprecated: Use CommandOptions.Timeout or *Command.WithTimeout instead.
Timeout time.Duration
}
// RunInDirWithOptions executes the command in given directory and options. It
// pipes stdin from supplied io.Reader, and pipes stdout and stderr to supplied
// io.Writer. DefaultTimeout will be used if the timeout duration is less than
// time.Nanosecond (i.e. less than or equal to 0). It returns an ErrExecTimeout
// if the execution was timed out.
func (c *Command) RunInDirWithOptions(dir string, opts ...RunInDirOptions) (err error) {
var opt RunInDirOptions
if len(opts) > 0 {
opt = opts[0]
}
timeout := c.timeout
// TODO: remove this in newer version
if opt.Timeout > 0 {
timeout = opt.Timeout
}
if timeout == 0 {
timeout = DefaultTimeout
}
buf := new(bytes.Buffer)
w := opt.Stdout
if logOutput != nil {
buf.Grow(512)
w = &limitDualWriter{
W: buf,
N: int64(buf.Cap()),
w: opt.Stdout,
}
}
defer func() {
if len(dir) == 0 {
log("[timeout: %v] %s\n%s", timeout, c, buf.Bytes())
} else {
log("[timeout: %v] %s: %s\n%s", timeout, dir, c, buf.Bytes())
}
}()
ctx := context.Background()
if c.ctx != nil {
ctx = c.ctx
}
if timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
defer func() {
cancel()
if err == context.DeadlineExceeded {
err = ErrExecTimeout
}
}()
}
cmd := exec.CommandContext(ctx, c.name, c.args...)
if len(c.envs) > 0 {
cmd.Env = append(os.Environ(), c.envs...)
}
cmd.Dir = dir
cmd.Stdin = opt.Stdin
cmd.Stdout = w
cmd.Stderr = opt.Stderr
if err = cmd.Start(); err != nil {
return err
}
result := make(chan error)
go func() {
result <- cmd.Wait()
}()
select {
case <-ctx.Done():
<-result
if cmd.Process != nil && cmd.ProcessState != nil && !cmd.ProcessState.Exited() {
if err := cmd.Process.Kill(); err != nil {
return fmt.Errorf("kill process: %v", err)
}
}
return ErrExecTimeout
case err = <-result:
return err
}
}
// RunInDirPipeline executes the command in given directory and default timeout
// duration. It pipes stdout and stderr to supplied io.Writer.
func (c *Command) RunInDirPipeline(stdout, stderr io.Writer, dir string) error {
return c.RunInDirWithOptions(dir, RunInDirOptions{
Stdin: nil,
Stdout: stdout,
Stderr: stderr,
})
}
// RunInDirPipelineWithTimeout executes the command in given directory and
// timeout duration. It pipes stdout and stderr to supplied io.Writer.
// DefaultTimeout will be used if the timeout duration is less than
// time.Nanosecond (i.e. less than or equal to 0). It returns an ErrExecTimeout
// if the execution was timed out.
//
// Deprecated: Use RunInDirPipeline and CommandOptions instead.
// TODO: remove this in the next major version
func (c *Command) RunInDirPipelineWithTimeout(timeout time.Duration, stdout, stderr io.Writer, dir string) (err error) {
if timeout != 0 {
c = c.WithTimeout(timeout)
}
return c.RunInDirPipeline(stdout, stderr, dir)
}
// RunInDirWithTimeout executes the command in given directory and timeout
// duration. It returns stdout in []byte and error (combined with stderr).
//
// Deprecated: Use RunInDir and CommandOptions instead.
// TODO: remove this in the next major version
func (c *Command) RunInDirWithTimeout(timeout time.Duration, dir string) ([]byte, error) {
if timeout != 0 {
c = c.WithTimeout(timeout)
}
return c.RunInDir(dir)
}
// RunInDir executes the command in given directory and default timeout
// duration. It returns stdout and error (combined with stderr).
func (c *Command) RunInDir(dir string) ([]byte, error) {
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
if err := c.RunInDirPipeline(stdout, stderr, dir); err != nil {
return nil, concatenateError(err, stderr.String())
}
return stdout.Bytes(), nil
}
// RunWithTimeout executes the command in working directory and given timeout
// duration. It returns stdout in string and error (combined with stderr).
//
// Deprecated: Use RunInDir and CommandOptions instead.
// TODO: remove this in the next major version
func (c *Command) RunWithTimeout(timeout time.Duration) ([]byte, error) {
if timeout != 0 {
c = c.WithTimeout(timeout)
}
return c.Run()
}
// Run executes the command in working directory and default timeout duration.
// It returns stdout in string and error (combined with stderr).
func (c *Command) Run() ([]byte, error) {
stdout, err := c.RunInDir("")
if err != nil {
return nil, err
}
return stdout, nil
}