This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocli.go
127 lines (115 loc) · 2.84 KB
/
ocli.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
package main
//This file loads and executes OCLI script files
import (
"bufio"
c "cli/controllers"
l "cli/logger"
"fmt"
"os"
"path/filepath"
"strings"
)
type fileParseError struct {
filename string
lineErrors []string
}
func (e *fileParseError) Error() string {
msg := "Syntax errors were found in the file: " + e.filename
msg += "\nThe following commands were invalid"
for _, err := range e.lineErrors {
msg += "\n" + err
}
return msg
}
func addLineError(
fileErr *fileParseError,
lineErr error,
filename string,
lineNumber int,
line string,
) *fileParseError {
msg := fmt.Sprintf(" LINE#: %d\tCOMMAND:%s", lineNumber, line)
if fileErr == nil {
return &fileParseError{filename, []string{msg}}
}
fileErr.lineErrors = append(fileErr.lineErrors, msg)
return fileErr
}
type parsedLine struct {
line string
lineNumber int
root node
}
func parseFile(path string) ([]parsedLine, error) {
filename := filepath.Base(path)
file, openErr := os.Open(path)
if openErr != nil {
return nil, openErr
}
result := []parsedLine{}
var fileErr *fileParseError
scanner := bufio.NewScanner(file)
for lineNumber := 1; scanner.Scan(); lineNumber++ {
line := scanner.Text()
if len(line) > 0 {
root, err := Parse(line)
if err != nil {
fileErr = addLineError(fileErr, err, filename, lineNumber, line)
}
if root != nil {
result = append(result, parsedLine{line, lineNumber, root})
}
}
}
if fileErr != nil {
return result, fileErr
}
return result, nil
}
type stackTraceError struct {
err error
history string
}
func newStackTraceError(err error, filename string, line string, lineNumber int) *stackTraceError {
stackErr := &stackTraceError{err: err}
stackErr.extend(filename, line, lineNumber)
return stackErr
}
func (s *stackTraceError) extend(filename string, line string, lineNumber int) {
trace := fmt.Sprintf(" File \"%s\", line %d\n", filename, lineNumber)
trace += " " + line + "\n"
s.history = trace + s.history
}
func (s *stackTraceError) Error() string {
msg := "Stack trace (most recent call last):\n"
return msg + s.history + "Error : " + s.err.Error()
}
func LoadFile(path string) error {
filename := filepath.Base(path)
file, err := parseFile(path)
if err != nil {
return err
}
for i := range file {
fmt.Println(file[i].line)
_, err := file[i].root.execute()
if err != nil {
errMsg := err.Error()
if strings.Contains(errMsg, "Duplicate") || strings.Contains(errMsg, "duplicate") {
l.GetWarningLogger().Println(errMsg)
if c.State.DebugLvl > c.NONE {
fmt.Println(errMsg)
}
continue
}
stackTraceErr, ok := err.(*stackTraceError)
if ok {
stackTraceErr.extend(filename, file[i].line, file[i].lineNumber)
} else {
stackTraceErr = newStackTraceError(err, filename, file[i].line, file[i].lineNumber)
}
return stackTraceErr
}
}
return nil
}