-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.go
84 lines (70 loc) · 2 KB
/
exec.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
package drone
import (
"fmt"
"github.com/goexl/gex"
"github.com/goexl/gox"
"github.com/goexl/gox/field"
)
func (b *Base) Exec(command string, opts ...execOption) error {
_options := defaultExecOptions(b.Pwe)
for _, opt := range opts {
opt.applyExec(_options)
}
return b.exec(command, _options)
}
func (b *Base) exec(command string, options *execOptions) (err error) {
fields := gox.Fields{
field.String(`command`, command),
field.Any(`args`, options.args),
field.Bool(`verbose`, b.Verbose),
field.Bool(`debug`, b.Debug),
}
// 记录日志
if b.Debug {
b.Info(fmt.Sprintf(`开始执行%s命令`, options.name), fields...)
}
gexOptions := gex.NewOptions(gex.Args(options.args...))
if `` != options.dir {
gexOptions = append(gexOptions, gex.Dir(options.dir))
}
if 0 != len(options.environments) {
gexOptions = append(gexOptions, gex.StringEnvs(options.environments...))
}
if options.async {
gexOptions = append(gexOptions, gex.Async())
} else {
gexOptions = append(gexOptions, gex.Sync())
}
// 增加检查
for _, _checker := range options.checkers {
switch _checker.mode {
case checkerModeContains:
gexOptions = append(gexOptions, gex.ContainsChecker(_checker.args.(string)))
case checkerModeEqual:
gexOptions = append(gexOptions, gex.EqualChecker(_checker.args.(string)))
}
}
// 增加输出
for _, _collector := range options.collectors {
switch _collector.mode {
case collectorModeString:
gexOptions = append(gexOptions, gex.StringCollector(_collector.args.(*string)))
}
}
// PWE处理
if !options.pwe {
gexOptions = append(gexOptions, gex.DisablePwe())
}
if !b.Debug {
gexOptions = append(gexOptions, gex.Quiet())
} else {
gexOptions = append(gexOptions, gex.Terminal())
}
// 执行命令
if _, err = gex.Exec(command, gexOptions...); nil != err {
b.Error(fmt.Sprintf(`执行%s命令出错`, options.name), fields.Connect(field.Error(err))...)
} else if b.Debug {
b.Info(fmt.Sprintf(`执行%s命令成功`, options.name), fields...)
}
return
}