-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathscript.go
271 lines (247 loc) · 7.22 KB
/
script.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
270
271
// Copyright 2020 Alexey Krivonogov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package main
import (
"encoding/json"
"eonza/lib"
"fmt"
"strings"
es "eonza/script"
"github.com/kataras/golog"
"github.com/labstack/echo/v4"
"gopkg.in/yaml.v2"
)
var (
scripts map[string]*Script
overrides map[string]*Script
)
const (
Override = `.override`
)
type scriptSettings struct {
Name string `json:"name" yaml:"name"`
Title string `json:"title" yaml:"title"`
Path string `json:"path,omitempty" yaml:"path,omitempty"`
Desc string `json:"desc,omitempty" yaml:"desc,omitempty"`
LogLevel int `json:"loglevel" yaml:"loglevel"`
Unrun bool `json:"unrun,omitempty" yaml:"unrun,omitempty"`
Help string `json:"help,omitempty" yaml:"help,omitempty"`
HelpLang string `json:"helplang,omitempty" yaml:"helplang,omitempty"`
}
type scriptTree struct {
Name string `json:"name" yaml:"name"`
Open bool `json:"open,omitempty" yaml:"open,omitempty"`
Disable bool `json:"disable,omitempty" yaml:"disable,omitempty"`
Values map[string]interface{} `json:"values,omitempty" yaml:"values,omitempty"`
Children []scriptTree `json:"children,omitempty" yaml:"children,omitempty"`
}
type Script struct {
Settings scriptSettings `json:"settings" yaml:"settings"`
Params []es.ScriptParam `json:"params,omitempty" yaml:"params,omitempty"`
Tree []scriptTree `json:"tree,omitempty" yaml:"tree,omitempty"`
Langs map[string]map[string]string `json:"langs,omitempty" yaml:"langs,omitempty"`
Code string `json:"code,omitempty" yaml:"code,omitempty"`
folder bool // can have other commands inside
embedded bool // Embedded script
initial string // Initial value
pkg string // Package name
override *Script // Override script
}
func getScript(name string) *Script {
return scripts[lib.IdName(name)]
}
func getRunScript(name string) (script *Script) {
script = scripts[lib.IdName(name)]
if script != nil && script.override != nil {
script = script.override
}
return
}
func retypeValues(value interface{}) interface{} {
switch v := value.(type) {
case map[string]interface{}:
for key, item := range v {
if val := retypeValues(item); val != nil {
v[key] = val
}
}
case []interface{}:
for i, item := range v {
if val := retypeValues(item); val != nil {
v[i] = val
}
}
case map[interface{}]interface{}:
ret := make(map[string]interface{})
for key, item := range v {
if val := retypeValues(item); val != nil {
item = val
}
ret[fmt.Sprint(key)] = item
}
return ret
}
return nil
}
func retypeTree(tree []scriptTree) {
for _, item := range tree {
retypeValues(item.Values)
retypeTree(item.Children)
}
}
func setScript(script *Script) error {
var ivalues map[string]interface{} //string
if !script.embedded && strings.HasSuffix(script.Settings.Name, Override) {
ownerName := script.Settings.Name[:len(script.Settings.Name)-len(Override)]
overrides[ownerName] = script
if owner := getScript(ownerName); owner != nil {
owner.override = script
script.pkg = owner.pkg
}
}
scripts[lib.IdName(script.Settings.Name)] = script
if len(script.Params) > 0 {
ivalues = make(map[string]interface{}) //string)
}
for _, par := range script.Params {
if par.Type == es.PList {
ivalues[par.Name] = []interface{}{}
} else if len(par.Options.Initial) > 0 {
ivalues[par.Name] = par.Options.Initial
}
}
if len(ivalues) > 0 {
initial, err := json.Marshal(ivalues)
if err != nil {
return err
}
script.initial = string(initial)
}
retypeTree(script.Tree)
return nil
}
func delScript(name string) {
if strings.HasSuffix(name, Override) {
ownerName := name[:len(name)-len(Override)]
if owner := getScript(ownerName); owner != nil {
owner.override = nil
}
delete(overrides, ownerName)
}
name = lib.IdName(name)
delete(scripts, name)
delete(storage.Scripts, name)
}
func InitScripts() {
scripts = make(map[string]*Script)
overrides = make(map[string]*Script)
isfolder := func(script *Script) bool {
return script.Settings.Name == SourceCode ||
strings.Contains(script.Code, `%body%`)
}
for _, f := range StdlibFS.List {
var script Script
if err := yaml.Unmarshal(f.Data, &script); err != nil {
golog.Fatal(err)
}
script.embedded = true
script.folder = isfolder(&script)
if err := setScript(&script); err != nil {
golog.Fatal(err)
}
}
LoadPackages()
for name, item := range storage.Scripts {
if scripts[lib.IdName(name)] != nil {
golog.Errorf(`The '%s' script has been loaded as embedded script`, name)
continue
}
//
item.folder = isfolder(item)
if err := setScript(item); err != nil {
golog.Fatal(err)
}
}
}
func (script *Script) Validate() error {
if !lib.ValidateSysName(script.Settings.Name) {
return fmt.Errorf(Lang(DefLang, `invalidfield`), Lang(DefLang, `name`))
}
if len(script.Settings.Title) == 0 {
return fmt.Errorf(Lang(DefLang, `invalidfield`), Lang(DefLang, `title`))
}
if len(script.Settings.Path) > 0 {
script.Settings.Path = strings.ReplaceAll(script.Settings.Path, "\\", "/")
script.Settings.Path = strings.Trim(script.Settings.Path, "/")
}
return nil
}
func scanTree(tree []scriptTree, name string) bool {
for _, item := range tree {
if item.Name == name {
return true
}
if len(item.Children) > 0 && scanTree(item.Children, name) {
return true
}
}
return false
}
func ScriptDependences(c echo.Context, name string) []ScriptItem {
var ret []ScriptItem
for _, item := range scripts {
if item.embedded {
continue
}
if scanTree(item.Tree, name) {
ret = append(ret, ScriptToItem(c, item))
}
}
return ret
}
func checkDep(c echo.Context, name, title string) error {
if deps := ScriptDependences(c, name); len(deps) > 0 {
ret := make([]string, len(deps))
for i, item := range deps {
ret[i] = item.Title
}
return fmt.Errorf(Lang(DefLang, `depscript`), title, strings.Join(ret, `,`))
}
return nil
}
func (script *Script) SaveScript(c echo.Context, original string) error {
if curScript := getScript(original); curScript != nil && curScript.embedded {
return fmt.Errorf(Lang(DefLang, `errembed`))
}
if len(original) > 0 && original != script.Settings.Name {
if getScript(script.Settings.Name) != nil {
return fmt.Errorf(Lang(DefLang, `errscriptname`), script.Settings.Name)
}
if err := checkDep(c, original, script.Settings.Title); err != nil {
return err
}
delScript(original)
}
script.folder = script.Settings.Name == SourceCode ||
strings.Contains(script.Code, `%body%`)
if err := setScript(script); err != nil {
return err
}
storage.Scripts[lib.IdName(script.Settings.Name)] = script
return SaveStorage()
}
func DeleteScript(c echo.Context, name string) error {
script := getScript(name)
if script == nil {
return fmt.Errorf(Lang(DefLang, `erropen`, name))
}
if script.embedded {
return fmt.Errorf(Lang(DefLang, `errembed`))
}
if err := checkDep(c, name, script.Settings.Title); err != nil {
return err
}
delScript(name)
return SaveStorage()
}