-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch.go
More file actions
193 lines (169 loc) · 4.04 KB
/
Copy pathwatch.go
File metadata and controls
193 lines (169 loc) · 4.04 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
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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"os/exec"
"path"
"reflect"
"strconv"
"strings"
"unicode"
"github.com/fatih/color"
)
type OutputType struct {
Type string `json:"type"`
}
type OutputError struct {
Title string `json:"title"`
Path string `json:"path"`
Message []interface{} `json:"message"`
}
type OutputErrors struct {
Errors []struct {
Path string `json:"path"`
Problems []struct {
Title string `json:"title"`
Region struct {
Start struct {
Line int `json:"line"`
Column int `json:"column"`
} `json:"start"`
} `json:"region"`
Message []interface{} `json:"message"`
} `json:"problems"`
} `json:"errors"`
}
var (
currentDirectory string
pathToElmCompiler *string
pathToMainFile *string
)
func init() {
log.SetFlags(0)
color.NoColor = false
var err error
currentDirectory, err = os.Getwd()
if err != nil {
log.Fatal(err)
}
pathToElmCompiler = flag.String("elm", "/usr/local/bin/elm", "path to elm compiler")
pathToMainFile = flag.String("main", "./src/elm/Main.elm", "path to main file")
flag.Parse()
}
func main() {
runElmMake()
}
func runElmMake() {
cmd := exec.Command(*pathToElmCompiler, "make", *pathToMainFile, "--output=/dev/null", "--report=json")
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err == nil {
fmt.Println("It Works!")
return
}
var outputType OutputType
if err := json.Unmarshal(stderr.Bytes(), &outputType); err != nil {
log.Fatalln(err, stderr.String())
}
switch outputType.Type {
case "error":
var elmErr OutputError
err := json.Unmarshal(stderr.Bytes(), &elmErr)
if err != nil {
log.Fatalln(err, stderr.String())
}
printError(elmErr)
case "compile-errors":
var elmErr OutputErrors
err := json.Unmarshal(stderr.Bytes(), &elmErr)
if err != nil {
log.Fatalln(err, stderr.String())
}
printErrors(elmErr)
default:
fmt.Println(stderr.String())
}
os.Exit(1)
}
func printError(output OutputError) {
printHeader(output.Title, output.Path, 1, 0)
printMessage(getMessage(output.Message), 0)
}
func printErrors(output OutputErrors) {
for _, e := range output.Errors {
for _, p := range e.Problems {
fmt.Println()
printHeader(p.Title, e.Path, p.Region.Start.Line, p.Region.Start.Column)
printMessage(getMessage(p.Message), p.Region.Start.Line)
}
}
}
func printHeader(title, relativePath string, line, column int) {
c := color.New(color.FgCyan)
_, err := c.Print(title, " -- ", path.Join(currentDirectory, relativePath), ":"+strconv.Itoa(line)+":"+strconv.Itoa(column), "\n")
if err != nil {
log.Fatal(err)
}
}
func getMessage(output []interface{}) string {
var message string
for _, m := range output {
switch v := m.(type) {
case string:
message += v
case map[string]interface{}:
message += getStyledMessagePart(v)
default:
fmt.Println("Problem with go parser, unrecognized type", reflect.TypeOf(m))
}
}
return message
}
func getStyledMessagePart(v map[string]interface{}) string {
// get formatting values
s := v["string"].(string)
isBold := v["bold"].(bool)
isUnderline := v["underline"].(bool)
// handle nil color
var msgColor string
switch col := v["color"].(type) {
case string:
msgColor = col
}
c := color.New()
switch strings.ToLower(msgColor) {
case "red":
c.Add(color.FgRed)
case "yellow":
c.Add(color.FgYellow)
case "green":
c.Add(color.FgGreen)
}
if isBold {
c.Add(color.Bold)
}
if isUnderline {
c.Add(color.Underline)
}
return c.Sprint(s)
}
func printMessage(message string, errorLineNumber int) {
split := strings.Split(message, "\n")
var resultMessage []string
for _, s := range split {
testable := strings.Trim(s, " \t")
if len(testable) > 0 && unicode.IsNumber(rune(testable[0])) {
if strings.HasPrefix(testable, strconv.Itoa(errorLineNumber)) || strings.HasPrefix(testable, strconv.Itoa(errorLineNumber-1)) {
resultMessage = append(resultMessage, s)
}
} else if testable != "" {
resultMessage = append(resultMessage, s)
}
}
fmt.Println(strings.Join(resultMessage, "\n"))
}