-
Notifications
You must be signed in to change notification settings - Fork 12
/
script.go
84 lines (77 loc) · 1.73 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
// 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 (
"os"
"github.com/gentee/gentee"
)
type Script struct {
Header Header // script header
Exec *gentee.Exec // Bytecode
}
type Settings struct {
ChStdin chan []byte
ChStdout chan []byte
ChSystem chan int
ProgressHandle gentee.ProgressFunc
}
var scriptTask *Script
func (script *Script) Run(options Settings) (interface{}, error) {
var (
settings gentee.Settings
rIn, wIn *os.File
rOut, wOut *os.File
conOut *os.File
)
settings.SysChan = options.ChSystem
rOut, wOut, _ = os.Pipe()
settings.Stdout = wOut
settings.Stderr = wOut
defer func() {
wOut.Close()
}()
if script.Header.Console {
conOut = os.Stdout
} else {
rIn, wIn, _ = os.Pipe()
settings.Stdin = rIn
defer func() {
wIn.Close()
}()
go func() {
var buf []byte
for {
buf = <-options.ChStdin
_, err := wIn.Write(buf)
if err != nil {
break
}
}
}()
}
go func(con bool) {
for {
buf := make([]byte, 1024)
n, err := rOut.Read(buf)
buf = buf[:n]
if err != nil {
break
}
if con {
conOut.Write(buf)
}
options.ChStdout <- buf
}
}(script.Header.Console)
if script.Header.IsPlayground {
settings.IsPlayground = true
settings.Playground.Path = script.Header.Playground.Dir
settings.Playground.AllSizeLimit = script.Header.Playground.Summary
settings.Playground.FilesLimit = int(script.Header.Playground.Files)
settings.Playground.SizeLimit = script.Header.Playground.Size
}
settings.ProgressHandle = options.ProgressHandle
scriptTask = script
return script.Exec.Run(settings)
}