-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpackages.go
366 lines (337 loc) · 8.92 KB
/
packages.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright 2021 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 (
"bytes"
"encoding/json"
"eonza/lib"
es "eonza/script"
"fmt"
"net/http"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"github.com/kataras/golog"
"github.com/labstack/echo/v4"
"gopkg.in/yaml.v3"
)
const PackagesURL = `https://www.eonza.org/downloads/packages`
type PackageInfo struct {
Title string `json:"title" yaml:"title"`
Desc string `json:"desc,omitempty" yaml:"desc,omitempty"`
Help string `json:"help,omitempty" yaml:"help,omitempty"`
HelpLang string `json:"helplang,omitempty" yaml:"helplang,omitempty"`
App bool `json:"app,omitempty" yaml:"app,omitempty"`
// Calculated fields
Installed bool `json:"installed"`
}
type PackageReview struct {
PackageInfo
Name string `json:"name"`
HasParams bool `json:"hasparams"`
}
type PackagesResponse struct {
List []PackageReview `json:"list,omitempty"`
Error string `json:"error,omitempty"`
}
type PackageResponse struct {
Params []es.ScriptParam `json:"params,omitempty"`
Values map[string]interface{} `json:"values,omitempty"`
Error string `json:"error,omitempty"`
}
type Package struct {
PackageInfo `yaml:"info"`
Version string `yaml:"version"`
Langs map[string]map[string]string `json:"langs,omitempty" yaml:"langs,omitempty"`
Params []es.ScriptParam `json:"params,omitempty" yaml:"params,omitempty"`
json string // json of package values
}
type PackageRelease struct {
File string `yaml:"file"`
MD5 string `yaml:"md5,omitempty"`
}
type PackageDownload struct {
Version string `yaml:"version"`
Releases map[string]PackageRelease `yaml:"releases"`
}
func GetPackageJSON(name string) string {
pkg := Assets.Packages[name]
if pkg == nil || !pkg.Installed || len(pkg.Params) == 0 {
return ``
}
if len(pkg.json) == 0 {
if out, err := json.Marshal(storage.PkgValues[name]); err == nil {
pkg.json = string(out)
}
}
return pkg.json
}
func LoadPackageScripts(name string) {
isfolder := func(script *Script) bool {
return script.Settings.Name == SourceCode ||
strings.Contains(script.Code, `%body%`)
}
for _, f := range PackagesFS.List {
if strings.HasPrefix(f.Name, name) && filepath.Ext(f.Name) == `.yaml` &&
filepath.Base(f.Name) != `package.yaml` {
var script Script
if err := yaml.Unmarshal(f.Data, &script); err != nil {
golog.Error(err)
}
script.embedded = true
script.folder = isfolder(&script)
script.pkg = name
if override := overrides[script.Settings.Name]; override != nil {
override.pkg = script.pkg
script.override = override
}
if err := setScript(&script); err != nil {
golog.Error(err)
}
}
}
hotVersion++
}
func UnloadPackageScripts(c echo.Context, name string) error {
todel := make(map[string]bool)
for _, f := range PackagesFS.List {
if strings.HasPrefix(f.Name, name) && filepath.Ext(f.Name) == `.yaml` &&
filepath.Base(f.Name) != `package.yaml` {
sname := filepath.Base(f.Name)
todel[sname[:len(sname)-5]] = true
}
}
for key := range todel {
if script := getScript(key); script != nil {
if err := checkDep(c, key, script.Settings.Title); err != nil {
return err
}
}
}
for key := range todel {
delScript(key)
}
hotVersion++
return nil
}
func LoadPackages() {
for name, ext := range Assets.Packages {
if _, err := os.Stat(filepath.Join(cfg.PackagesDir, name)); err == nil {
ext.Installed = true
LoadPackageScripts(name)
Assets.Packages[name] = ext
}
}
}
func PackagesList(c echo.Context) *PackagesResponse {
lang := c.(*Auth).Lang
glob := &langRes[GetLangId(c.(*Auth).User)]
ret := make([]PackageReview, 0)
for name, pkg := range Assets.Packages {
pkg.Title = es.ReplaceVars(pkg.Title, pkg.Langs[lang], glob)
pkg.Desc = es.ReplaceVars(pkg.Desc, pkg.Langs[lang], glob)
ret = append(ret, PackageReview{
PackageInfo: pkg.PackageInfo,
Name: name,
HasParams: len(pkg.Params) > 0,
})
}
sort.Slice(ret, func(i, j int) bool {
if ret[i].Installed == ret[j].Installed {
return ret[i].Title < ret[j].Title
}
return ret[i].Installed
})
return &PackagesResponse{
List: ret,
}
}
func packagesHandle(c echo.Context) error {
if err := CheckAdmin(c); err != nil {
return jsonError(c, err)
}
return c.JSON(http.StatusOK, PackagesList(c))
}
func findPackage(c echo.Context, name string) (*Package, error) {
if err := CheckAdmin(c); err != nil {
return nil, err
}
v, ok := Assets.Packages[name]
if !ok {
return nil, fmt.Errorf(`Cannot find %s package`, name)
}
return v, nil
}
func packageHandle(c echo.Context) error {
var (
err error
pkg *Package
)
name := c.Param("name")
if pkg, err = findPackage(c, name); err != nil {
return jsonError(c, err)
}
ret := make([]es.ScriptParam, len(pkg.Params))
copy(ret, pkg.Params)
lang := c.(*Auth).Lang
glob := &langRes[GetLangId(c.(*Auth).User)]
for i, par := range ret {
ret[i].Title = es.ReplaceVars(par.Title, pkg.Langs[lang], glob)
}
return c.JSON(http.StatusOK, &PackageResponse{
Params: ret,
Values: storage.PkgValues[name],
})
}
func savePackageHandle(c echo.Context) error {
var (
err error
pkg *Package
)
errResult := func() error {
return c.JSON(http.StatusOK, Response{Error: fmt.Sprint(err)})
}
if err = CheckAdmin(c); err != nil {
return errResult()
}
name := c.Param("name")
if pkg, err = findPackage(c, name); err != nil {
return jsonError(c, err)
}
values := make(map[string]interface{})
if err = c.Bind(&values); err != nil {
return errResult()
}
delete(values, `name`)
storage.PkgValues[name] = values
pkg.json = ""
if err = SaveStorage(); err != nil {
return errResult()
}
return c.JSON(http.StatusOK, Response{Success: true})
}
func packageInstallHandle(c echo.Context) error {
var (
err error
pkg *Package
)
if cfg.playground {
return jsonError(c, fmt.Errorf(`Access denied`))
}
name := c.Param("name")
if pkg, err = findPackage(c, name); err != nil {
return jsonError(c, err)
}
if name == `tests` {
storage.Events[`test`] = &Event{
ID: lib.RndNum(),
Name: `test`,
Script: `data-print`,
Token: `TEST_TOKEN`,
Whitelist: `::1/128, 127.0.0.0/31`,
Active: true,
}
}
vals := make(map[string]interface{})
cur := storage.PkgValues[name]
for _, par := range pkg.Params {
vals[par.Name] = par.Options.Initial
if cur != nil {
if v, ok := cur[par.Name]; ok {
vals[par.Name] = v
}
}
}
if len(vals) > 0 {
storage.PkgValues[name] = vals
if err = SaveStorage(); err != nil {
return jsonError(c, err)
}
}
path := filepath.Join(cfg.PackagesDir, name)
if err := os.MkdirAll(path, 0777); err != nil {
return jsonError(c, err)
}
pkgError := func(err error) error {
os.RemoveAll(path)
return jsonError(c, err)
}
if pkg.App {
var data []byte
if data, err = lib.Download(PackagesURL + `/` + name + `/package.yaml`); err != nil {
return pkgError(err)
}
var release PackageDownload
if err = yaml.Unmarshal(data, &release); err != nil {
return pkgError(err)
}
if err = os.WriteFile(filepath.Join(path, `package.yaml`), data, 0777); err != nil {
return pkgError(err)
}
if r, ok := release.Releases[runtime.GOOS+`-`+runtime.GOARCH]; !ok {
return pkgError(fmt.Errorf(`cannot find package's release`))
} else {
if data, err = lib.Download(PackagesURL + `/` + name + `/` + r.File); err != nil {
return pkgError(err)
}
if data, err = lib.GzipDecompress(data); err != nil {
return pkgError(err)
}
buf := bytes.NewBuffer(data)
if err = lib.UnpackTar(buf, path); err != nil {
return pkgError(err)
}
}
}
for _, f := range PackagesFS.List {
if strings.HasPrefix(f.Name, name+`/files`) {
fullName := filepath.Join(cfg.PackagesDir, f.Name)
if f.Dir {
if err := os.MkdirAll(fullName, 0777); err != nil {
return pkgError(err)
}
} else if err = os.WriteFile(fullName, f.Data, 0666); err != nil {
return pkgError(err)
}
}
}
LoadPackageScripts(name)
pkg.Installed = true
return c.JSON(http.StatusOK, PackagesList(c))
}
func packageUninstallHandle(c echo.Context) error {
var (
err error
pkg *Package
)
if cfg.playground {
return jsonError(c, fmt.Errorf(`Access denied`))
}
name := c.Param("name")
if pkg, err = findPackage(c, name); err != nil {
return jsonError(c, err)
}
if err = UnloadPackageScripts(c, name); err != nil {
return jsonError(c, err)
}
path := filepath.Join(cfg.PackagesDir, name)
if err := os.RemoveAll(path); err != nil {
return jsonError(c, err)
}
pkg.Installed = false
if name == `tests` {
delete(storage.Events, `test`)
SaveStorage()
}
return c.JSON(http.StatusOK, PackagesList(c))
}
func PkgFile(assetname, filename string) error {
data, err := os.ReadFile(filepath.Join(scriptTask.Header.PkgPath, assetname))
if err != nil {
return err
}
return os.WriteFile(filename, data, 0666)
}