-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathencoding.go
108 lines (98 loc) · 2.14 KB
/
encoding.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
// 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 script
import (
"bytes"
"encoding/gob"
"os"
"os/exec"
"eonza/lib"
"eonza/users"
"github.com/gentee/gentee"
)
type Header struct {
Name string
Title string
AssetsDir string
LogDir string
PackagesDir string
Theme string
CDN string
Data string
Console bool
IsPlayground bool
IsAutoFill bool
SourceCode []byte
Constants map[string]string
SecureConsts map[string]string
Lang string
User users.User
Role users.Role
ClaimKey string
PkgPath string
IsPro bool
IP string
TaskID uint32
FormAlign uint32
ServerPort int
URLPort int
HTTP *lib.HTTPConfig
Playground *lib.PlaygroundConfig
}
func Encode(header Header, source string) (*bytes.Buffer, error) {
var (
data bytes.Buffer
)
workspace := gentee.New()
bcode, _, err := workspace.Compile(source, header.Name)
if err != nil {
return nil, err
}
enc := gob.NewEncoder(&data)
if err = enc.Encode(header); err != nil {
return nil, err
}
if err = enc.Encode(bcode); err != nil {
return nil, err
}
if header.Console {
return &data, nil
}
command := exec.Command(lib.AppPath())
command.Stdin = &data
command.Stdout = os.Stdout
command.Stderr = os.Stderr
err = command.Start()
go func() {
if err == nil {
_ = command.Wait()
}
}()
return nil, err
}
func Decode(scriptData []byte) (script *Script, err error) {
script = &Script{}
data := bytes.NewBuffer(scriptData)
if scriptData == nil {
if _, err = data.ReadFrom(os.Stdin); err != nil {
return
}
}
dec := gob.NewDecoder(data)
if err = dec.Decode(&script.Header); err == nil {
err = dec.Decode(&script.Exec)
}
return
}
func ReplaceVars(input string, values map[string]string, glob *map[string]string) string {
if len(values) == 0 && len(*glob) == 0 {
return input
}
stack := make([]string, 0)
ret, err := replace(values, []rune(input), &stack, glob)
if err != nil {
return input
}
return string(ret)
}