-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand.go
188 lines (173 loc) · 3.81 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
package executor
import (
"fmt"
"log"
"strings"
"time"
)
type CmdController struct {
cmd *Command
}
type ProcessHandler = func(m *CmdController)
// Command 命令
type Command struct {
// 任务名
Name string
// 命令
Code string
// 描述
Desc string
// 执行环境
Session func() (execSession, error)
// 是否允许错误 - 异步执行不关心错误
AllowError bool
// 是否异步执行
Async bool
// 是否完成
completed bool
// 初始化钩子
Init ProcessHandler
// 完成钩子
Done ProcessHandler
// 是否打印log
Logging bool
// 延迟执行,单位毫秒, 100毫秒以下设置无效
Delay int // Delay - run after ${delay} millisecond, ignore when < 100
// payload
payload *map[string]interface{}
// 是否放弃执行
abandon bool
// 输出内容
output string
// 错误内容
errMsg string
// 任务序号
index int
// 环境变量
Env []string
}
func (c *CmdController) SetDelay(n int) {
if c.cmd.completed {
return
}
c.cmd.Delay = n
}
func (c *CmdController) SetCode(code string) {
if c.cmd.completed {
fmt.Println("Can not SetCode, command is completed.")
return
}
c.cmd.Code = code
}
// ReplaceCode
func (c *CmdController) ReplaceCode(old string, new string, n int) {
if c.cmd.completed {
fmt.Println("Can not ReplaceCode, command is completed.")
return
}
c.cmd.Code = strings.Replace(c.cmd.Code, old, new, n)
}
// SetState
func (c *CmdController) SetState(key string, value interface{}) {
payload := *(c.cmd.payload)
payload[key] = value
}
// GetState
func (c *CmdController) GetState(key string) interface{} {
payload := *(c.cmd.payload)
return payload[key]
}
// Abandon
func (c *CmdController) Abandon() {
if c.cmd.completed {
fmt.Println("Can not Abandon, command is completed.")
return
}
c.cmd.abandon = true
}
// GetOutput - get command Output
func (c *CmdController) GetOutput() string {
if !c.cmd.completed {
return ""
}
return c.cmd.output + c.cmd.errMsg
}
// Inspect - inspect if command is valid
func (c *Command) Inspect() (err error) {
if c.Code == "" {
c.abandon = true
err = fmt.Errorf("Abandon Command \"%s\" for missing \"Code\" attr.\n", c.Name)
return
}
if c.Session == nil {
c.abandon = true
err = fmt.Errorf("Abandon Command \"%s\" for missing \"Session\" attr.\n", c.Name)
return
}
return
}
func (c *Command) Exec() error {
var (
err error
session execSession
beginTime = time.Now().UnixNano() / 1e6
)
// initialize
if c.Init != nil {
log.Printf("[INIT](%d): %s\n", c.index, c.Name)
c.Init(&CmdController{c})
}
// abandon
if c.abandon {
goto ABANDON
} else {
log.Printf("[START](%d): %s, code: \"%s\"\n", c.index, c.Name, c.Code)
}
// delay task if require
if c.Delay >= 100 {
<-time.After(time.Duration(c.Delay) * time.Millisecond)
}
// create session
if session, err = c.Session(); err != nil {
goto ERR
}
// exec command
if err = session.Run(c.Code, c.Env); err != nil {
goto ERR
}
goto OK
ABANDON:
log.Printf("[DROP](%d): %s", c.index, c.Name)
return nil
ERR:
c.completed = true
if session == nil {
log.Printf("[LOG](%d): Session create fail.%s\n", c.index, err.Error())
} else {
c.errMsg = session.ErrMsg()
c.output = session.Output()
}
if c.Logging {
fmt.Printf("[LOG](%d): %s %s\n", c.index, c.output, c.errMsg)
}
if c.AllowError {
if c.Done != nil {
log.Printf("[DONE](%d): %s\n", c.index, c.Name)
c.Done(&CmdController{c})
}
}
log.Printf("[END](%d): %s X (%dms)\n", c.index, c.Name, time.Now().UnixNano()/1e6-beginTime)
return err
OK:
c.completed = true
c.output = session.Output()
c.errMsg = session.ErrMsg()
if c.Logging {
fmt.Printf("[LOG](%d): %s %s\n", c.index, c.output, c.errMsg)
}
log.Printf("[END](%d): %s √ (%dms)\n", c.index, c.Name, time.Now().UnixNano()/1e6-beginTime)
if c.Done != nil {
c.Done(&CmdController{c})
}
return nil
}