-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
131 lines (110 loc) · 2.76 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
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
package main
import (
"encoding/json"
"fmt"
"github.com/santhosh-tekuri/jsonschema/v5"
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
"github.com/spf13/viper"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
)
var compiledSchema *jsonschema.Schema
var schemaErrors = make(map[string]error)
var hadError bool
const (
BASEDIR = "GITHUB_WORKSPACE"
SUBDIR = "DIR"
ForceSchemaLocation = "FORCE_SCHEMA_LOCATION"
FailFast = "FAIL_FAST"
RequireSchemas = "REQUIRE_SCHEMAS"
)
func main() {
viper.SetDefault(BASEDIR, "")
viper.SetDefault(SUBDIR, "")
viper.SetDefault(ForceSchemaLocation, "")
viper.SetDefault(FailFast, false)
viper.SetDefault(RequireSchemas, false)
viper.AutomaticEnv()
var err error
compiler := jsonschema.NewCompiler()
compiler.Draft = jsonschema.Draft2020
if viper.GetString(ForceSchemaLocation) != "" {
compiledSchema, err = compiler.Compile(viper.GetString(ForceSchemaLocation))
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to compile provided schema: %#v\n", err)
os.Exit(1)
}
} else {
fmt.Fprintf(os.Stderr, "%s is a required ENV", ForceSchemaLocation)
}
dir := path.Join(viper.GetString(BASEDIR), viper.GetString(SUBDIR))
if dir == "" {
dir, err = os.Getwd()
if err != nil {
fmt.Printf("unable to find current working dir and no dir provided: %s \n", err.Error())
os.Exit(1)
}
}
err = filepath.WalkDir(dir, walkValidate)
if err != nil && viper.GetBool(FailFast) {
fmt.Printf("Validation failed fast, some JSON files were potentially skipped! \n ")
}
for k := range schemaErrors {
schemaError := schemaErrors[k]
if schemaError == nil {
fmt.Printf("%s \U00002705 \n ", k)
} else {
fmt.Printf("%s \U0000274C \n", k)
fmt.Printf("Error detail: %s \n ", schemaError.Error())
}
}
if hadError {
os.Exit(1)
}
}
func walkValidate(entry string, dir fs.DirEntry, err error) error {
if err != nil {
return err
}
if dir != nil {
if dir.IsDir() {
return nil
}
}
if strings.HasSuffix(entry, "json") {
fmt.Printf("Validating %s \n", entry)
err = validate(entry)
if err != nil {
hadError = true
if viper.GetBool(FailFast) {
return err
}
}
schemaErrors[entry] = err
}
return nil
}
func validate(jsonFile string) error {
file, err := os.Open(jsonFile)
if err != nil {
return fmt.Errorf("Error opening file: %v\n", err)
}
defer file.Close()
//var v map[string]interface{}
var v interface{}
dec := json.NewDecoder(file)
dec.UseNumber()
if err := dec.Decode(&v); err != nil {
return fmt.Errorf("Syntax error: %v\n", err)
}
err = compiledSchema.Validate(v)
if ve, ok := err.(*jsonschema.ValidationError); ok {
out := ve.DetailedOutput()
b, _ := json.MarshalIndent(out, "", " ")
return fmt.Errorf(string(b))
}
return nil
}