Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathexec.go
More file actions
Latest commit
175 lines (165 loc) · 3.95 KB
/
Copy pathexec.go
File metadata and controls
175 lines (165 loc) · 3.95 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
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
package devstatscode
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
)
// logCommand - output command and arguments
funclogCommand(ctx*Ctx, cmdAndArgs []string, envmap[string]string) {
if!ctx.ExecQuiet {
Printf("Command, arguments, environment:\n%+v\n%+v\n", cmdAndArgs, env)
fmt.Fprintf(os.Stdout, "Command and arguments:\n%+v\n%+v\n", cmdAndArgs, env)
}
}
// ExecCommand - execute command given by array of strings with eventual environment map
funcExecCommand(ctx*Ctx, cmdAndArgs []string, envmap[string]string) (string, error) {
// Execution time
dtStart:=time.Now()
// STDOUT pipe size
pipeSize:=0x100
// Command & arguments
command:=cmdAndArgs[0]
arguments:=cmdAndArgs[1:]
ifctx.CmdDebug>0 {
varargs []string
for_, arg:=rangecmdAndArgs {
argLen:=len(arg)
ifargLen>0x200 {
arg=arg[0:0x100] +"..."+arg[argLen-0x100:argLen]
}
ifstrings.Contains(arg, " ") {
args=append(args, "'"+arg+"'")
} else {
args=append(args, arg)
}
}
Printf("%s\n", strings.Join(args, " "))
}
cmd:=exec.Command(command, arguments...)
// Environment setup (if any)
iflen(env) >0 {
newEnv:=os.Environ()
forkey, value:=rangeenv {
newEnv=append(newEnv, key+"="+value)
}
cmd.Env=newEnv
ifctx.CmdDebug>0 {
Printf("Environment Override: %+v\n", env)
ifctx.CmdDebug>2 {
Printf("Full Environment: %+v\n", newEnv)
}
}
}
// Capture STDOUT (non buffered - all at once when command finishes), only used on error and when no buffered/piped version used
// Which means it is used on error when CmdDebug <= 1
// In CmdDebug > 1 mode, we're displaying STDOUT during execution, and storing results to 'outputStr'
// Capture STDERR (non buffered - all at once when command finishes)
var (
stdOut bytes.Buffer
stdErr bytes.Buffer
outputStrstring
)
cmd.Stderr=&stdErr
ifctx.CmdDebug<=1 {
cmd.Stdout=&stdOut
}
// Pipe command's STDOUT during execution (if CmdDebug > 1)
// Or just starts command when no STDOUT debug
ifctx.CmdDebug>1 {
stdOutPipe, e:=cmd.StdoutPipe()
ife!=nil {
logCommand(ctx, cmdAndArgs, env)
ifctx.ExecFatal {
FatalOnError(e)
} else {
return"", e
}
}
e=cmd.Start()
ife!=nil {
logCommand(ctx, cmdAndArgs, env)
ifctx.ExecFatal {
FatalOnError(e)
} else {
return"", e
}
}
buffer:=make([]byte, pipeSize, pipeSize)
nBytes, e:=stdOutPipe.Read(buffer)
fore==nil&&nBytes>0 {
Printf("%s", buffer[:nBytes])
outputStr+=string(buffer[:nBytes])
nBytes, e=stdOutPipe.Read(buffer)
}
ife!=io.EOF {
logCommand(ctx, cmdAndArgs, env)
ifctx.ExecFatal {
FatalOnError(e)
} else {
return"", e
}
}
} else {
e:=cmd.Start()
ife!=nil {
logCommand(ctx, cmdAndArgs, env)
ifctx.ExecFatal {
FatalOnError(e)
} else {
return"", e
}
}
}
// Wait for command to finish
err:=cmd.Wait()
// If error - then output STDOUT, STDERR and error info
iferr!=nil {
ifctx.CmdDebug<=1 {
outStr:=stdOut.String()
iflen(outStr) >0&&!ctx.ExecQuiet {
Printf("%v\n", outStr)
}
}
errStr:=stdErr.String()
iflen(errStr) >0&&!ctx.ExecQuiet {
Printf("STDERR:\n%v\n", errStr)
}
iferr!=nil {
logCommand(ctx, cmdAndArgs, env)
ifctx.ExecFatal {
FatalOnError(err)
} else {
returnstdOut.String(), err
}
}
}
// If CmdDebug > 1 display STDERR contents as well (if any)
ifctx.CmdDebug>1 {
errStr:=stdErr.String()
iflen(errStr) >0 {
Printf("Errors:\n%v\n", errStr)
}
}
ifctx.CmdDebug>0 {
info:=strings.Join(cmdAndArgs, " ")
lenInfo:=len(info)
iflenInfo>0x280 {
info=info[0:0x140] +"..."+info[lenInfo-0x140:lenInfo]
}
dtEnd:=time.Now()
Printf("%s ... %+v\n", info, dtEnd.Sub(dtStart))
}
outStr:=""
ifctx.ExecOutput {
ifctx.CmdDebug<=1 {
outStr=stdOut.String()
} else {
outStr=outputStr
}
}
returnoutStr, nil
}