-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
101 lines (84 loc) · 1.91 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/mgutz/ansi"
)
// Version from ldflags
var version string
func readResources(config string) (*[]Resource, error) {
file, err := ioutil.ReadFile(config)
if err != nil {
return nil, err
}
var resources []Resource
err = json.Unmarshal(file, &resources)
if err != nil {
return nil, err
}
return &resources, nil
}
func printValidResult(result *ResourceResult) {
color := "green"
fmt.Println(ansi.Color("Status: valid", color+"+i"))
fmt.Println(ansi.Color("Document: "+result.URL, color))
fmt.Println(ansi.Color("Schema: "+result.Schema, color))
}
func printInvalidResult(result *ResourceResult) {
color := "red"
fmt.Println(ansi.Color("Status: invalid", color+"+i"))
fmt.Println(ansi.Color("Document: "+result.URL, color))
fmt.Println(ansi.Color("Schema: "+result.Schema, color))
fmt.Println(ansi.Color("Errors:", color))
for _, desc := range result.Errors() {
fmt.Println(ansi.Color(" * "+desc.String(), color))
}
}
func main() {
showVersion := flag.Bool("version", false, "prints current version")
flag.Usage = func() {
fmt.Println("Usage: resource-validator [config]")
}
flag.Parse()
if *showVersion {
fmt.Println(version)
os.Exit(0)
}
files := flag.Args()
if len(files) != 1 {
flag.Usage()
os.Exit(1)
}
resources, err := readResources(files[0])
if err != nil {
log.Fatal(err)
}
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
chans := make([]chan *ResourceResult, len(*resources))
for i, resource := range *resources {
chans[i] = make(chan *ResourceResult)
go resource.Check(wd, chans[i])
}
var hasErrors bool
for _, ch := range chans {
for result := range ch {
if result.Valid() {
printValidResult(result)
} else {
hasErrors = true
printInvalidResult(result)
}
fmt.Println("--------------------------------")
}
}
if hasErrors {
os.Exit(1)
}
}