Scripting

/app/controller/clib/scripting.go (3.9 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
package clib

import (
"net/url"

"github.com/pkg/errors"
"github.com/valyala/fasthttp"

"{{{ .Package }}}/app"
"{{{ .Package }}}/app/controller"
"{{{ .Package }}}/app/controller/cutil"
"{{{ .Package }}}/app/lib/scripting"
"{{{ .Package }}}/views/vscripting"
)

func ScriptingList(rc *fasthttp.RequestCtx) {
controller.Act("scripting.list", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
ps.Title = "Scripting"
ret, sizes := as.Services.Script.ListScriptSizes(ps.Logger)
ps.Data = ret
return controller.Render(rc, as, &vscripting.List{Scripts: ret, Sizes: sizes}, ps, "scripting")
})
}

func ScriptingDetail(rc *fasthttp.RequestCtx) {
controller.Act("scripting.detail", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
key, err := cutil.RCRequiredString(rc, "key", true)
if err != nil {
return "", err
}
key, src, err := as.Services.Script.LoadScript(key, ps.Logger)
if err != nil {
return "", err
}
loadResult, vm, err := scripting.LoadVM(key, src, ps.Logger)
if err != nil {
return "", err
}
res, err := scripting.RunExamples(vm)
if err != nil {
return "", err
}
ps.Title = key
ps.Data = map[string]any{"script": src, "results": res}
page := &vscripting.Detail{Path: key, Script: src, LoadResult: loadResult, Results: res}
return controller.Render(rc, as, page, ps, "scripting", key)
})
}

func ScriptingNew(rc *fasthttp.RequestCtx) {
controller.Act("scripting.new", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
ps.Title = "New Script"
return controller.Render(rc, as, &vscripting.Form{}, ps, "scripting", "New")
})
}

func ScriptingCreate(rc *fasthttp.RequestCtx) {
controller.Act("scripting.create", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
frm, err := cutil.ParseForm(rc)
if err != nil {
return "", err
}
pth := frm.GetStringOpt("path")
if pth == "" {
return "", errors.New("must provide path")
}
content := frm.GetStringOpt("content")
if content == "" {
return "", errors.New("content is required")
}
err = as.Services.Script.SaveScript(pth, content, ps.Logger)
if err != nil {
return "", err
}
return controller.FlashAndRedir(true, "Scripting created", "/admin/scripting/"+url.QueryEscape(pth), rc, ps)
})
}

func ScriptingForm(rc *fasthttp.RequestCtx) {
controller.Act("scripting.form", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
key, err := cutil.RCRequiredString(rc, "key", true)
if err != nil {
return "", err
}
key, sc, err := as.Services.Script.LoadScript(key, ps.Logger)
if err != nil {
return "", err
}
ps.Title = "Edit [" + key + "]"
ps.Data = sc
return controller.Render(rc, as, &vscripting.Form{Path: key, Content: sc}, ps, "scripting", key, "Edit")
})
}

func ScriptingSave(rc *fasthttp.RequestCtx) {
controller.Act("scripting.save", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
key, err := cutil.RCRequiredString(rc, "key", true)
if err != nil {
return "", err
}
frm, err := cutil.ParseForm(rc)
if err != nil {
return "", err
}
content := frm.GetStringOpt("content")
if content == "" {
return "", errors.New("content is required")
}
err = as.Services.Script.SaveScript(key, content, ps.Logger)
if err != nil {
return "", err
}
return controller.FlashAndRedir(true, "Scripting saved", "/admin/scripting/"+url.QueryEscape(key), rc, ps)
})
}

func ScriptingDelete(rc *fasthttp.RequestCtx) {
controller.Act("scripting.delete", rc, func(as *app.State, ps *cutil.PageState) (string, error) {
key, err := cutil.RCRequiredString(rc, "key", true)
if err != nil {
return "", err
}
err = as.Services.Script.DeleteScript(key, ps.Logger)
if err != nil {
return "", err
}
return controller.FlashAndRedir(true, "Script deleted", "/admin/scripting", rc, ps)
})
}