-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.go
More file actions
162 lines (142 loc) · 3.68 KB
/
Copy pathscript.go
File metadata and controls
162 lines (142 loc) · 3.68 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
package dbot
import (
"bytes"
"fmt"
"strconv"
"github.com/robertkrimen/otto"
)
func parseValueToEnv(path string, value otto.Value) (Env, error) {
ret := Env{}
if !value.IsObject() {
return ret, fmt.Errorf("%s must be object", path)
}
for _, key := range value.Object().Keys() {
if value.Object() == nil {
return ret, fmt.Errorf("%s.%s is nil", path, key)
} else if item, e := value.Object().Get(key); e != nil {
return ret, fmt.Errorf("%s.%s error: %s", path, key, e.Error())
} else if !item.IsString() {
return ret, fmt.Errorf("%s.%s must be string", path, key)
} else {
ret[key] = item.String()
}
}
return ret, nil
}
func parseValueToStdin(path string, value otto.Value) ([]string, error) {
ret := []string{}
if !value.IsObject() {
return ret, fmt.Errorf("%s must be object", path)
}
for _, key := range value.Object().Keys() {
if strconv.FormatInt(int64(len(ret)), 10) != key {
return ret, fmt.Errorf("%s must be array", path)
} else if value.Object() == nil {
return ret, fmt.Errorf("%s[%s] is nil", path, key)
} else if item, e := value.Object().Get(key); e != nil {
return ret, fmt.Errorf("%s[%s] error: %s", path, key, e.Error())
} else if !item.IsString() {
return ret, fmt.Errorf("%s[%s] must be string", path, key)
} else {
ret = append(ret, item.String())
}
}
return ret, nil
}
func parseObjectToCommand(object *otto.Object) (*Command, error) {
ret := &Command{}
keys := object.Keys()
for _, key := range keys {
value, e := object.Get(key)
if e != nil {
return nil, fmt.Errorf("%s error: %s", key, e.Error())
}
switch key {
case "tag":
if !value.IsString() {
return nil, fmt.Errorf("tag must be string")
}
ret.Tag = value.String()
case "exec":
if !value.IsString() {
return nil, fmt.Errorf("exec must be string")
}
ret.Exec = value.String()
case "on":
if !value.IsString() {
return nil, fmt.Errorf("on must be string")
}
ret.On = value.String()
case "stdin":
stdin, e := parseValueToStdin("stdin", value)
if e != nil {
return nil, e
}
ret.Stdin = stdin
case "env":
env, e := parseValueToEnv("env", value)
if e != nil {
return nil, e
}
ret.Env = env
case "args":
args, e := parseValueToEnv("args", value)
if e != nil {
return nil, e
}
ret.Args = args
case "file":
if !value.IsString() {
return nil, fmt.Errorf("file must be string")
}
ret.File = value.String()
default:
return nil, fmt.Errorf("%s is not supported", key)
}
}
return ret, nil
}
type DbotObject struct {
vm *otto.Otto
stdout *bytes.Buffer
stderr *bytes.Buffer
ctx *Context
seed int64
}
func (p *DbotObject) LogInfo(call otto.FunctionCall) otto.Value {
for _, v := range call.ArgumentList {
p.stdout.WriteString(v.String())
}
return otto.Value{}
}
func (p *DbotObject) LogError(call otto.FunctionCall) otto.Value {
for _, v := range call.ArgumentList {
p.stderr.WriteString(v.String())
}
return otto.Value{}
}
func (p *DbotObject) Command(call otto.FunctionCall) otto.Value {
retFalse, _ := p.vm.ToValue(false)
retTrue, _ := p.vm.ToValue(true)
idx := p.seed
p.seed++
if len(call.ArgumentList) != 1 {
_, _ = p.vm.Call("new Error", nil, "arguments length error")
return retFalse
}
arg0 := call.ArgumentList[0].Object()
if arg0 == nil {
_, _ = p.vm.Call("new Error", nil, "argument is nil")
return retFalse
}
if cmd, e := parseObjectToCommand(arg0); e != nil {
_, _ = p.vm.Call("new Error", nil, e.Error())
return retFalse
} else if ctx := p.ctx.subContext(cmd); ctx == nil {
return retFalse
} else if !ctx.Clone("%s.script.dbot.Command[%d]", p.ctx.path, idx).Run() {
return retFalse
} else {
return retTrue
}
}