-
Notifications
You must be signed in to change notification settings - Fork 1
/
check.go
313 lines (279 loc) · 7.63 KB
/
check.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
Copyright AppsCode Inc. and Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/go-openapi/jsonreference"
"github.com/spf13/cobra"
diff "github.com/yudai/gojsondiff"
"github.com/yudai/gojsondiff/formatter"
"sigs.k8s.io/yaml"
)
var (
// wizardDir = filepath.Join(homedir.HomeDir(), "go/src/go.bytebuilders.dev/ui-wizards/charts")
// uiFile = filepath.Join(homedir.HomeDir(), "go/src/go.bytebuilders.dev/ui-wizards/charts/kubedbcom-mongodb-editor/ui/create-ui.yaml")
// schemaFile = filepath.Join(homedir.HomeDir(), "go/src/go.bytebuilders.dev/ui-wizards/charts/kubedbcom-mongodb-editor/values.openapiv3_schema.yaml")
wizardDir = ""
uiFile = ""
schemaFile = ""
fmtOnly bool
skipSchemaRefValidation bool
)
func NewCheckCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "check",
Short: "Check schema of ui-builder json",
RunE: func(cmd *cobra.Command, args []string) error {
if wizardDir == "" {
err := formatSchema(uiFile)
if err != nil || fmtOnly {
return err
}
return checkFile(uiFile, schemaFile)
}
return checkDir(wizardDir)
},
}
flags := cmd.Flags()
flags.StringVar(&wizardDir, "wizard-dir", wizardDir, "Path to wizard directory")
flags.StringVar(&uiFile, "ui-file", uiFile, "Path to ui.json file")
flags.StringVar(&schemaFile, "schema-file", schemaFile, "Path to schema file")
flags.BoolVar(&fmtOnly, "fmt-only", fmtOnly, "Format ui.json file only")
flags.BoolVar(&skipSchemaRefValidation, "skip-schema-ref-validation", skipSchemaRefValidation, "Skip schema ref validation")
return cmd
}
func checkDir(root string) error {
matches, err := filepath.Glob(filepath.Join(root, "**", "ui"))
if err != nil {
return err
}
for _, path := range matches {
dir := filepath.Dir(path)
uifiles, err := os.ReadDir(filepath.Join(dir, "ui"))
if os.IsNotExist(err) {
continue
} else if err != nil {
return err
}
for _, f := range uifiles {
if f.IsDir() || f.Name() == "functions.js" || f.Name() == "language.yaml" {
continue
}
fp := filepath.Join(dir, "ui", f.Name())
fmt.Printf("processing file: %s\n", fp)
err := formatSchema(fp)
if err != nil {
return err
}
schemaFile = filepath.Join(dir, "values.openapiv3_schema.yaml")
if !fmtOnly && fileExists(schemaFile) {
if err = checkFile(fp, schemaFile); err != nil {
return err
}
}
}
}
return nil
}
func fileExists(name string) bool {
if _, err := os.Stat(name); err == nil {
return true
}
return false
}
func checkFile(uiFile, schemaFile string) error {
result, err := checkUIBuilderSchema(uiFile)
if err != nil {
return err
}
if result != "" {
return fmt.Errorf("ui json file does not conform to ui-builder schema. diff\n %v", result)
}
if skipSchemaRefValidation {
return nil
}
return checkJsonReference(uiFile, schemaFile)
}
func checkJsonReference(uiFile, schemaFile string) error {
data, err := os.ReadFile(uiFile)
if err != nil {
return err
}
var uijson map[string]interface{}
err = yaml.Unmarshal(data, &uijson)
if err != nil {
return err
}
data, err = os.ReadFile(schemaFile)
if err != nil {
return err
}
var schema map[string]interface{}
err = yaml.Unmarshal(data, &schema)
if err != nil {
return err
}
errlist := checkRef(uijson, schema, "")
errno := 0
for _, e := range errlist {
if e != nil {
_, _ = fmt.Fprintln(os.Stderr, e)
errno++
}
}
if errno > 0 {
return fmt.Errorf("schema ref check failed")
}
return nil
}
func checkRef(uijson, schema map[string]interface{}, path string) (errlist []error) {
for k, v := range uijson {
switch u := v.(type) {
case map[string]interface{}:
errlist = append(errlist, checkRef(u, schema, path+k+".")...)
case []interface{}:
for i := range u {
entry, ok := u[i].(map[string]interface{})
if !ok {
continue
}
errlist = append(errlist, checkRef(entry, schema, fmt.Sprintf("%s%s[%d].", path, k, i))...)
}
case string:
if k == "$ref" && strings.HasPrefix(u, "schema#/") && !strings.Contains(u, "/$dyn") {
curPath := path + k + "."
errlist = append(errlist, r1(u, schema, curPath))
}
}
}
return
}
var re = regexp.MustCompile(`/properties/\d+(/?)`)
func r1(ref string, schema map[string]interface{}, curPath string) error {
u := re.ReplaceAllString(ref, "/items${1}")
p, err := jsonreference.New(u)
if err != nil {
return fmt.Errorf("failed to parse schema.ref %s at path %s: %v", u, curPath, err)
}
_, _, err = p.GetPointer().Get(schema)
if err == nil {
return nil
}
parts := strings.Split(u, "/")
preserveUnknownFields := func() bool {
obj := schema
for i := 1; i < len(parts); i++ {
v, ok := obj[parts[i]]
if !ok {
return false
}
obj, ok = v.(map[string]any)
if !ok {
return false
}
v, ok = obj["x-kubernetes-preserve-unknown-fields"]
if ok && v.(bool) {
return true
}
}
return false
}
if preserveUnknownFields() {
return nil
}
if len(parts) >= 3 && parts[len(parts)-2] == "properties" {
nu := strings.Join(parts[:len(parts)-2], "/")
p, err := jsonreference.New(nu)
if err != nil {
return fmt.Errorf("failed to parse schema.ref %s at path %s: %v", ref, curPath, err)
}
v, _, err := p.GetPointer().Get(schema)
if err != nil {
return fmt.Errorf("schema.ref %s at path %s is invalid: %v", ref, curPath, err)
}
if m, ok := v.(map[string]interface{}); !ok {
return fmt.Errorf("expected schema.ref %s at path %s to point to an object", nu, curPath)
} else if _, o2 := m["additionalProperties"]; !o2 {
return fmt.Errorf("schema.ref %s at path %s is missing additionalProperties", ref, curPath)
} else {
return nil
}
}
return fmt.Errorf("schema.ref %s at path %s is invalid: %v", u, curPath, err)
}
func formatSchema(filename string) error {
data, err := os.ReadFile(filename)
if err != nil {
return err
}
var original map[string]interface{}
err = yaml.Unmarshal(data, &original)
if err != nil {
return err
}
// fix formatting of the input ui.json file
fmtyml, err := yaml.Marshal(original)
if err != nil {
return err
}
return os.WriteFile(filename, fmtyml, 0o644)
}
func checkUIBuilderSchema(filename string) (string, error) {
data, err := os.ReadFile(filename)
if err != nil {
return "", err
}
var original map[string]interface{}
err = yaml.Unmarshal(data, &original)
if err != nil {
return "", err
}
sorted, err := json.MarshalIndent(&original, "", " ")
if err != nil {
return "", err
}
var spec Document
err = yaml.Unmarshal(data, &spec)
if err != nil {
return "", err
}
parsed, err := json.Marshal(spec)
if err != nil {
return "", err
}
// Then, Check them
differ := diff.New()
d, err := differ.Compare(sorted, parsed)
if err != nil {
fmt.Printf("Failed to unmarshal file: %s\n", err.Error())
os.Exit(3)
}
if d.Modified() {
config := formatter.AsciiFormatterConfig{
ShowArrayIndex: true,
Coloring: true,
}
f := formatter.NewAsciiFormatter(original, config)
result, err := f.Format(d)
if err != nil {
return "", err
}
return result, nil
}
return "", nil
}