forked from aergoio/aergo
/
batch.go
197 lines (159 loc) · 4.08 KB
/
batch.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
package exec
import (
"bufio"
"fmt"
"os"
"path/filepath"
"github.com/aergoio/aergo/cmd/brick/context"
"github.com/fsnotify/fsnotify"
"github.com/mattn/go-colorable"
"github.com/rs/zerolog"
)
var (
verboseBatch = false
letBatchKnowErr error
batchErrorCount = 0
enableWatch = false
watchFileList []string
watcher *fsnotify.Watcher
)
func EnableVerbose() {
verboseBatch = true
}
func GetBatchErrorCount() int {
return batchErrorCount
}
func EnableWatch() {
enableWatch = true
}
func init() {
registerExec(&batch{})
}
type batch struct {
level int
}
func (c *batch) Command() string {
return "batch"
}
func (c *batch) Syntax() string {
return fmt.Sprintf("%s", context.PathSymbol)
}
func (c *batch) Usage() string {
return fmt.Sprintf("batch `<batch_file_path>`")
}
func (c *batch) Describe() string {
return "batch run"
}
func (c *batch) Validate(args string) error {
_, err := c.parse(args)
return err
}
func (c *batch) parse(args string) (string, error) {
splitArgs := context.SplitSpaceAndAccent(args, false)
if len(splitArgs) != 1 {
return "", fmt.Errorf("invalid format. usage: %s", c.Usage())
}
batchFilePath := splitArgs[0]
if _, err := os.Stat(batchFilePath.Text); os.IsNotExist(err) {
return "", fmt.Errorf("fail to read a brick batch file %s: %s", batchFilePath.Text, err.Error())
}
return batchFilePath.Text, nil
}
func (c *batch) Run(args string) (string, error) {
batchFilePath, _ := c.parse(args)
stdOut := colorable.NewColorableStdout()
var err error
for {
if c.level == 0 && enableWatch {
watcher, err = fsnotify.NewWatcher()
if err != nil {
return "", err
}
// clear screnn
fmt.Fprintf(stdOut, "\033[H\033[2J")
}
prefix := ""
batchFile, err := os.Open(batchFilePath)
if err != nil {
return "", err
}
var cmdLines []string
scanner := bufio.NewScanner(batchFile)
for scanner.Scan() {
cmdLines = append(cmdLines, scanner.Text())
}
batchFile.Close()
c.level++
// set highest log level to turn off verbose
if false == verboseBatch {
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
fmt.Fprintf(stdOut, "> %s\n", batchFile.Name())
} else if verboseBatch && c.level != 1 {
prefix = fmt.Sprintf("%d-", c.level-1)
fmt.Fprintf(stdOut, "\n<<<<<<< %s\n", batchFile.Name())
}
for i, line := range cmdLines {
lineNum := i + 1
cmd, args := context.ParseFirstWord(line)
if len(cmd) == 0 {
if verboseBatch {
fmt.Fprintf(stdOut, "\x1B[0;37m%s%d\x1B[0m\n", prefix, lineNum)
}
continue
} else if context.Comment == cmd {
if verboseBatch {
fmt.Fprintf(stdOut, "\x1B[0;37m%s%d \x1B[32m%s\x1B[0m\n", prefix, lineNum, line)
}
continue
}
if verboseBatch {
fmt.Fprintf(stdOut, "\x1B[0;37m%s%d \x1B[34;1m%s \x1B[0m%s\n", prefix, lineNum, cmd, args)
}
Broker(line)
if letBatchKnowErr != nil {
// if there is error during execution, then print line for error trace
fmt.Fprintf(stdOut, "\x1B[0;37m%s:%d \x1B[34;1m%s \x1B[0m%s\n\n", batchFile.Name(), lineNum, cmd, args)
letBatchKnowErr = nil
}
}
if c.level != 1 && verboseBatch {
fmt.Fprintf(stdOut, ">>>>>>> %s\n", batchFile.Name())
}
c.level--
// print final result and reset params
if c.level == 0 {
if batchErrorCount == 0 {
fmt.Fprintf(stdOut, "\x1B[32;1mBatch is successfully finished\x1B[0m\n")
} else {
fmt.Fprintf(stdOut, "\x1B[31;1mBatch is failed: Error %d\x1B[0m\n", batchErrorCount)
}
// reset params
batchErrorCount = 0
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
// add file to watch list
if enableWatch {
absPath, _ := filepath.Abs(batchFile.Name())
watcher.Add(absPath)
}
if c.level == 0 && enableWatch {
defer watcher.Close()
// wait and check file changes
fileWatching:
for {
select {
case <-watcher.Events:
break fileWatching
case err, _ := <-watcher.Errors:
if err != nil {
fmt.Fprintf(stdOut, "\x1B[0;37mWatching File %s Error: %s\x1B[0m\n", batchFile.Name(), err.Error())
}
break fileWatching
}
}
continue
}
break
}
return "batch exec is finished", nil
}