-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
177 lines (143 loc) · 4.07 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
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
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"flag"
"github.com/santhosh-tekuri/jsonschema/v5"
)
var APP_VERSION = "unreleased"
// cannot continue, exit immediately without a stacktrace.
func fatal() {
fmt.Printf("cannot continue, ") // "cannot continue, exit status 1"
os.Exit(1)
}
func configure_validator(schema_file string) *jsonschema.Schema {
label := "release.json"
compiler := jsonschema.NewCompiler()
//compiler.Draft = jsonschema.Draft4 // todo: either drop schema version or raise this one
file_bytes, err := os.ReadFile(schema_file)
if err != nil {
slog.Error("failed to read the json schema", "path", schema_file)
fatal()
}
err = compiler.AddResource(label, bytes.NewReader(file_bytes))
if err != nil {
slog.Error("failed to add schema to compiler", "error", err)
fatal()
}
schema, err := compiler.Compile(label)
if err != nil {
slog.Error("failed to compile schema", "error", err)
fatal()
}
return schema
}
func path_exists(path string) bool {
_, err := os.Stat(path)
return !errors.Is(err, os.ErrNotExist)
}
func read_cli_args(arg_list []string) (string, string) {
input_file_ptr := flag.String("in", "", "path to release.json file")
schema_file_ptr := flag.String("schema", "", "path to release.json schema file")
phelp := flag.Bool("help", false, "print this help and exit")
pversion := flag.Bool("version", false, "print program version and exit")
flag.Parse()
if phelp != nil && *phelp {
flag.Usage()
os.Exit(0)
}
if pversion != nil && *pversion {
fmt.Println(APP_VERSION)
os.Exit(0)
}
// ---
if input_file_ptr == nil || *input_file_ptr == "" {
fmt.Println("--in is required")
fatal()
}
input_file := *input_file_ptr
if !strings.HasSuffix(input_file, ".json") && !strings.HasSuffix(input_file, ".jsonl") {
fmt.Printf("input file has unsupported file extension. supported extensions: .json, .jsonl\n")
fatal()
}
if !path_exists(input_file) {
fmt.Printf("input file does not exist: %s\n", input_file)
fatal()
}
// ---
if schema_file_ptr == nil || *schema_file_ptr == "" {
fmt.Println("--schema is required")
fatal()
}
schema_file := *schema_file_ptr
if !strings.HasSuffix(schema_file, ".json") {
fmt.Printf("schema file has unsupported file extension. supported extensions: .json\n")
fatal()
}
if !path_exists(schema_file) {
fmt.Printf("schema file does not exist: %s\n", schema_file)
fatal()
}
return schema_file, input_file
}
func validate(schema *jsonschema.Schema, release_dot_json_bytes []byte) error {
var raw interface{}
err := json.Unmarshal(release_dot_json_bytes, &raw)
if err != nil {
return fmt.Errorf("failed to unmarshal release.json bytes into a generic struct for validation: %w", err)
}
return schema.Validate(raw)
}
func read_input_file(input_file string) ([][]byte, error) {
empty_response := [][]byte{}
switch filepath.Ext(input_file) {
case ".json":
bl, err := os.ReadFile(input_file)
if err != nil {
return empty_response, err
}
return [][]byte{bl}, nil
case ".jsonl":
bl := [][]byte{}
fh, err := os.Open(input_file)
if err != nil {
return empty_response, fmt.Errorf("failed to open input file for reading: %w", err)
}
scanner := bufio.NewScanner(fh)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
blt := []byte(scanner.Text()) // this seems to work when scanner.Bytes() does not ...
bl = append(bl, blt)
}
if err := scanner.Err(); err != nil {
return empty_response, fmt.Errorf("failed to read contents of input file: %w", err)
}
return bl, nil
default:
return empty_response, nil
}
}
func main() {
schema_file, input_file := read_cli_args(os.Args)
schema := configure_validator(schema_file)
release_dot_json_bytes_list, err := read_input_file(input_file)
if err != nil {
slog.Error("failed to read input file", "error", err)
fatal()
}
for i, release_dot_json_bytes := range release_dot_json_bytes_list {
err = validate(schema, release_dot_json_bytes)
if err != nil {
fmt.Printf("%#v\n", err)
break
}
fmt.Printf("%d valid\n", i+1)
}
}