-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd.go
269 lines (212 loc) · 4.17 KB
/
cmd.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
package hype
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/markbates/clam"
"github.com/mattn/go-shellwords"
)
// Cmd is a tag representing a command to be executed.
type Cmd struct {
*Element
Args []string
Env []string
ExpectedExit int
Timeout time.Duration
res *CmdResult
}
func (c *Cmd) MarshalJSON() ([]byte, error) {
if c == nil {
return nil, ErrIsNil("cmd")
}
c.RLock()
defer c.RUnlock()
m, err := c.JSONMap()
if err != nil {
return nil, err
}
m["type"] = toType(c)
m["expected_exit"] = c.ExpectedExit
m["timeout"] = c.Timeout.String()
if len(c.Args) > 0 {
m["args"] = c.Args
}
if len(c.Env) > 0 {
m["env"] = c.Env
}
if c.res != nil {
m["result"] = c.res
}
return json.MarshalIndent(m, "", " ")
}
func (c *Cmd) MD() string {
if c == nil {
return ""
}
return c.Children().MD()
}
// Result returns the result of executing the command.
func (c *Cmd) Result() *CmdResult {
c.RLock()
defer c.RUnlock()
return c.res
}
// Execute the command.
func (c *Cmd) Execute(ctx context.Context, doc *Document) error {
if c == nil {
return ErrIsNil("cmd")
}
if c.Element == nil {
return ErrIsNil("element")
}
if doc == nil {
return ErrIsNil("document")
}
if c.Timeout == 0 {
c.Timeout = time.Second * 30
}
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, c.Timeout)
defer cancel()
cmd := &clam.Cmd{
Env: c.Env,
}
src, ok := c.Get("src")
if ok {
dir := filepath.Join(doc.Root, src)
cmd.Dir = dir
}
// Check the args for the ~ character and replace it with the home directory
// Make a copy of those args as we only want to change them when we pass it to the clam.Cmd
args := make([]string, len(c.Args))
copy(args, c.Args)
for i, arg := range args {
if strings.HasPrefix(arg, "~") {
args[i] = filepath.Join(homeDirectory(), arg[1:])
}
}
res, err := cmd.Run(ctx, args...)
if err != nil {
switch c.ExpectedExit {
case -1:
if res.Exit == 0 {
return c.newError(err)
}
default:
if res.Exit != c.ExpectedExit {
return c.newError(err)
}
}
}
cres, err := NewCmdResult(doc.Parser, c, res)
if err != nil {
return c.newError(err)
}
c.Lock()
c.res = cres
c.Nodes = Nodes{cres}
c.Unlock()
return nil
}
func (c *Cmd) newError(err error) error {
if c == nil {
return err
}
re, ok := err.(clam.RunError)
if !ok {
return CmdError{
RunError: clam.RunError{
Err: err,
},
Filename: c.Filename,
}
}
return CmdError{
RunError: re,
Filename: c.Filename,
}
}
func NewCmd(el *Element) (*Cmd, error) {
if el == nil {
return nil, ErrIsNil("element")
}
c := &Cmd{
Element: el,
Timeout: time.Second * 30,
}
ex, err := el.ValidAttr("exec")
if err != nil {
return nil, err
}
args, err := shellwords.Parse(ex)
if err != nil {
return nil, err
}
if len(args) == 0 {
return nil, c.WrapErr(fmt.Errorf("no command specified"))
}
c.Args = args
if en, ok := el.Get("environ"); ok {
c.Env = append(c.Env, strings.Split(en, ",")...)
}
if ee, ok := el.Get("exit"); ok {
c.ExpectedExit, err = strconv.Atoi(ee)
if err != nil {
return nil, err
}
}
if to, ok := el.Get("timeout"); ok {
c.Timeout, err = time.ParseDuration(to)
if err != nil {
return nil, err
}
}
return c, nil
}
func NewAttrCode(p *Parser, el *Element) (Nodes, error) {
if el == nil {
return nil, ErrIsNil("element")
}
var nodes Nodes
code, ok := el.Get("code")
if !ok {
return nodes, nil
}
el.Delete("code")
src, ok := el.Get("src")
if ok {
code = filepath.Join(src, code)
}
ats := &Attributes{}
if err := ats.Set("src", code); err != nil {
return nil, err
}
cel := NewEl("code", el.Parent)
cel.Attributes = ats
codes, err := NewCodeNodes(p, cel)
if err != nil {
return nil, err
}
nodes = append(nodes, codes...)
nodes = append(nodes, NewEl("hr", nil))
return nodes, nil
}
func NewCmdNodes(p *Parser, el *Element) (Nodes, error) {
if el == nil {
return nil, ErrIsNil("element")
}
nodes, err := NewAttrCode(p, el)
if err != nil {
return nil, err
}
cmd, err := NewCmd(el)
if err != nil {
return nil, err
}
nodes = append(nodes, cmd)
return nodes, nil
}