forked from google/go-jsonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
58 lines (51 loc) · 1005 Bytes
/
cmd.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
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/sh0rez/go-jsonnet/linter"
jsonnet "github.com/sh0rez/go-jsonnet"
)
// ExitProblemsFound is used if we find problems with the code
func ExitProblemsFound() {
os.Exit(2)
}
// ExitError is used if the file doesn't exist etc.
func ExitError() {
os.Exit(1)
}
func die(err error) {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
ExitError()
}
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s <path>\n", os.Args[0])
os.Exit(2)
}
p := os.Args[1]
inputFile, err := os.Open(p)
if err != nil {
die(err)
}
data, err := ioutil.ReadAll(inputFile)
if err != nil {
die(err)
}
err = inputFile.Close()
if err != nil {
die(err)
}
node, err := jsonnet.SnippetToAST(p, string(data))
if err != nil {
die(err)
}
errWriter := &linter.ErrorWriter{
Writer: os.Stderr,
}
linter.Lint(node, errWriter)
if errWriter.ErrorsFound {
fmt.Fprintf(os.Stderr, "Problems found!\n")
ExitProblemsFound()
}
}