Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
Latest commit
146 lines (125 loc) · 3.62 KB
/
Copy pathcommand.go
File metadata and controls
146 lines (125 loc) · 3.62 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
package git
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
)
// Command represents a command with its subcommands or arguments.
typeCommandstruct {
namestring
args []string
envs []string
}
func (c*Command) String() string {
iflen(c.args) ==0 {
returnc.name
}
returnfmt.Sprintf("%s %s", c.name, strings.Join(c.args, " "))
}
// NewCommand creates and returns a new Git Command based on given command and arguments.
funcNewCommand(args...string) *Command {
return&Command{
name: "git",
args: args,
}
}
// AddArguments adds new argument(s) to the command.
func (c*Command) AddArguments(args...string) *Command {
c.args=append(c.args, args...)
returnc
}
// AddEnvs adds new environment variables to the command.
func (c*Command) AddEnvs(envs...string) *Command {
c.envs=append(c.envs, envs...)
returnc
}
constDEFAULT_TIMEOUT=60*time.Second
// RunInDirTimeoutPipeline executes the command in given directory with given timeout,
// it pipes stdout and stderr to given io.Writer.
func (c*Command) RunInDirTimeoutPipeline(timeout time.Duration, dirstring, stdout, stderr io.Writer) error {
iftimeout==-1 {
timeout=DEFAULT_TIMEOUT
}
iflen(dir) ==0 {
log(c.String())
} else {
log("%s: %v", dir, c)
}
cmd:=exec.Command(c.name, c.args...)
ifc.envs!=nil {
cmd.Env=append(os.Environ(), c.envs...)
}
cmd.Dir=dir
cmd.Stdout=stdout
cmd.Stderr=stderr
iferr:=cmd.Start(); err!=nil {
returnerr
}
done:=make(chanerror)
gofunc() {
done<-cmd.Wait()
}()
varerrerror
select {
case<-time.After(timeout):
ifcmd.Process!=nil&&cmd.ProcessState!=nil&&!cmd.ProcessState.Exited() {
iferr:=cmd.Process.Kill(); err!=nil {
returnfmt.Errorf("fail to kill process: %v", err)
}
}
<-done
returnErrExecTimeout{timeout}
caseerr=<-done:
}
returnerr
}
// RunInDirTimeout executes the command in given directory with given timeout,
// and returns stdout in []byte and error (combined with stderr).
func (c*Command) RunInDirTimeout(timeout time.Duration, dirstring) ([]byte, error) {
stdout:=new(bytes.Buffer)
stderr:=new(bytes.Buffer)
iferr:=c.RunInDirTimeoutPipeline(timeout, dir, stdout, stderr); err!=nil {
returnnil, concatenateError(err, stderr.String())
}
ifstdout.Len() >0 {
log("stdout:\n%s", stdout.Bytes()[:1024])
}
returnstdout.Bytes(), nil
}
// RunInDirPipeline executes the command in given directory,
// it pipes stdout and stderr to given io.Writer.
func (c*Command) RunInDirPipeline(dirstring, stdout, stderr io.Writer) error {
returnc.RunInDirTimeoutPipeline(-1, dir, stdout, stderr)
}
// RunInDir executes the command in given directory
// and returns stdout in []byte and error (combined with stderr).
func (c*Command) RunInDirBytes(dirstring) ([]byte, error) {
returnc.RunInDirTimeout(-1, dir)
}
// RunInDir executes the command in given directory
// and returns stdout in string and error (combined with stderr).
func (c*Command) RunInDir(dirstring) (string, error) {
stdout, err:=c.RunInDirTimeout(-1, dir)
iferr!=nil {
return"", err
}
returnstring(stdout), nil
}
// RunTimeout executes the command in defualt working directory with given timeout,
// and returns stdout in string and error (combined with stderr).
func (c*Command) RunTimeout(timeout time.Duration) (string, error) {
stdout, err:=c.RunInDirTimeout(timeout, "")
iferr!=nil {
return"", err
}
returnstring(stdout), nil
}
// Run executes the command in defualt working directory
// and returns stdout in string and error (combined with stderr).
func (c*Command) Run() (string, error) {
returnc.RunTimeout(-1)
}