-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.go
More file actions
181 lines (163 loc) · 4.53 KB
/
main.go
File metadata and controls
181 lines (163 loc) · 4.53 KB
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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
"github.com/qri-io/jsonschema"
"gopkg.in/yaml.v3"
)
var schemaData = []byte(`{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"opentofu": { "$ref": "#/$defs/tool" },
"terraform": { "$ref": "#/$defs/tool" }
},
"required": ["opentofu", "terraform"],
"$defs": {
"tool": {
"type": "object",
"properties": {
"version": {
"type": "string",
"maxLength": 4
},
"license": {
"type": "string"
},
"licenseURL": {
"type": "string",
"format": "uri"
},
"registry": {
"type": "string",
"format": "uri"
},
"features": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"url": {
"type": "string",
"format": "uri"
},
"version": {
"type": "string",
"maxLength": 4
},
"featureRequestURL": {
"type": "string",
"format": "uri"
}
},
"required": ["name"]
}
}
},
"required": ["version", "versionURL", "license", "licenseURL", "registry", "features"]
}
}
}`)
type Feature struct {
Name string `json:"name" yaml:"name"`
Version string `json:"version,omitempty" yaml:"version,omitempty"`
URL string `json:"url,omitempty" yaml:"url,omitempty"`
FeatureRequestURL string `json:"featureRequestURL,omitempty" yaml:"featureRequestURL,omitempty"`
}
type Tool struct {
Version string `json:"version" yaml:"version"`
VersionURL string `json:"versionURL" yaml:"versionURL"`
License string `json:"license" yaml:"license"`
LicenseURL string `json:"licenseURL" yaml:"licenseURL"`
Registry string `json:"registry" yaml:"registry"`
Features []Feature `json:"features" yaml:"features"`
}
type ToolMeta struct {
Version string `yaml:"version"`
VersionURL string `yaml:"versionURL"`
License string `yaml:"license"`
LicenseURL string `yaml:"licenseURL"`
Registry string `yaml:"registry"`
}
type FeatureToolData struct {
Version string `yaml:"version,omitempty"`
URL string `yaml:"url,omitempty"`
FeatureRequestURL string `yaml:"featureRequestURL,omitempty"`
}
type FeatureEntry struct {
Name string `yaml:"name"`
Tools map[string]*FeatureToolData `yaml:"tools"`
}
type ToolsFile struct {
Tools map[string]ToolMeta `yaml:"tools"`
Features []FeatureEntry `yaml:"features"`
}
func main() {
data, err := os.ReadFile("tools/tools.yaml")
if err != nil {
panic("read tools/tools.yaml: " + err.Error())
}
var tf ToolsFile
if err := yaml.Unmarshal(data, &tf); err != nil {
panic("parse tools/tools.yaml: " + err.Error())
}
tools := make(map[string]Tool, len(tf.Tools))
for name, meta := range tf.Tools {
tools[name] = Tool{
Version: meta.Version,
VersionURL: meta.VersionURL,
License: meta.License,
LicenseURL: meta.LicenseURL,
Registry: meta.Registry,
}
}
for _, f := range tf.Features {
for toolName, fd := range f.Tools {
t := tools[toolName]
t.Features = append(t.Features, Feature{Name: f.Name, Version: fd.Version, URL: fd.URL, FeatureRequestURL: fd.FeatureRequestURL})
tools[toolName] = t
}
}
toolsJSON, err := json.MarshalIndent(tools, "", " ")
if err != nil {
panic("marshal tools: " + err.Error())
}
ctx := context.Background()
rs := &jsonschema.Schema{}
if err := json.Unmarshal(schemaData, rs); err != nil {
panic("unmarshal schema: " + err.Error())
}
errs, err := rs.ValidateBytes(ctx, toolsJSON)
if err != nil {
panic(err)
}
if len(errs) > 0 {
for _, e := range errs {
fmt.Println(e.Error())
}
os.Exit(1)
}
output := make(map[string]interface{}, len(tools)+1)
for k, v := range tools {
output[k] = v
}
output["lastUpdated"] = time.Now().UTC().Format("2006-01-02")
outputJSON, err := json.MarshalIndent(output, "", " ")
if err != nil {
panic("marshal output: " + err.Error())
}
// File for Hugo to template the table
if err := os.WriteFile("data/tools.json", outputJSON, 0644); err != nil {
panic(err)
}
// allow access to https://cani.tf/tools.json
if err := os.WriteFile("static/tools.json", outputJSON, 0644); err != nil {
panic(err)
}
}