-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
70 lines (58 loc) · 1.34 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
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
au "github.com/logrusorgru/aurora"
)
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr,
"%s %s [-schema=PATH] FILE [FILE]\n",
au.Cyan(au.Bold("USAGE")),
au.Bold(filepath.Base(os.Args[0])))
flag.PrintDefaults()
os.Exit(0)
}
var schema *string
schema = flag.String("schema", "", "path to JSON schema definition")
flag.Parse()
os.Exit(realMain(*schema, flag.Args()))
}
func realMain(schema string, args []string) int {
errors := 0
// handle possible STDIN input first
// distinguish from "no input"
hasNamedPipe := false
info, _ := os.Stdin.Stat()
if info.Mode()&os.ModeNamedPipe != 0 {
hasNamedPipe = true
}
if !hasNamedPipe && len(args) == 0 {
log.Println("No input")
flag.Usage()
return 1
}
if hasNamedPipe {
err := validateSTDIN(os.Stdin, schema)
if err != nil {
log.Println(fmt.Sprintf("%s %s", au.Red(au.Bold("ERROR")), err.Error()))
errors++
}
}
// iterate over files; return number of invalid files
for _, arg := range args {
err := validateFile(arg, schema)
if err != nil {
log.Println(fmt.Sprintf("%s %s", au.Red(au.Bold("ERROR")), err.Error()))
errors++
}
}
// report success only if all files/streams have passed
if errors == 0 {
log.Println(fmt.Sprintf("%s", au.Green(au.Bold("OK"))))
}
return errors
}