-
Notifications
You must be signed in to change notification settings - Fork 163
/
main.go
75 lines (63 loc) · 1.43 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/url"
"os"
"path"
"go.uber.org/multierr"
"github.com/glasskube/glasskube/api/v1alpha1"
"github.com/glasskube/glasskube/internal/repo"
"github.com/invopop/jsonschema"
)
var (
types = map[string]interface{}{
"package-manifest": &v1alpha1.PackageManifest{},
"index": &repo.PackageRepoIndex{},
"versions": &repo.PackageIndex{},
}
outBase = "website/static"
idBase = "https://glasskube.dev/"
schemaPath = "schemas/v1"
outDir = path.Join(outBase, schemaPath)
reflector = jsonschema.Reflector{
ExpandedStruct: true,
}
)
func run() (retErr error) {
if err := os.MkdirAll(outDir, 0755); err != nil {
return err
}
for k, v := range types {
fileName := fmt.Sprintf("%v.json", k)
schema := reflector.Reflect(v)
if id, err := url.JoinPath(idBase, schemaPath, fileName); err != nil {
return err
} else {
schema.ID = jsonschema.ID(id)
}
file, err := os.Create(path.Join(outDir, fileName))
if err != nil {
return err
}
defer func(file *os.File) {
closeErr := file.Close()
if closeErr != nil {
retErr = multierr.Append(err, closeErr)
}
}(file)
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
fmt.Fprintf(os.Stderr, "Writing to %v\n", file.Name())
if err = encoder.Encode(schema); err != nil {
return err
}
}
return nil
}
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}