-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
230 lines (188 loc) · 5.18 KB
/
Copy pathmain.go
File metadata and controls
230 lines (188 loc) · 5.18 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
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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
)
type LocalizedText struct {
En string `json:"en"`
De string `json:"de"`
}
type Group struct {
En string `json:"en"`
De string `json:"de"`
Icon string `json:"icon"`
}
type Service struct {
ID int `json:"id"`
Name string `json:"name"`
Group string `json:"group"`
Description LocalizedText `json:"description"`
Domains []string `json:"domains,omitempty"`
Patterns []string `json:"patterns,omitempty"`
}
type CompiledServicesOutput struct {
Groups map[string]Group `json:"groups"`
Services map[string]Service `json:"services"`
}
type StableIDs struct {
Services map[string]int `json:"services"`
}
func main() {
const stableIDsPath = "ids.json"
const legacyServiceIDsPath = "service_ids.json"
groups, err := loadGroups("groups.json")
if err != nil {
fmt.Fprintf(os.Stderr, "error loading groups: %v\n", err)
os.Exit(1)
}
stableIDs, err := loadStableIDs(stableIDsPath, legacyServiceIDsPath)
if err != nil {
fmt.Fprintf(os.Stderr, "error loading stable ids: %v\n", err)
os.Exit(1)
}
nextServiceID, err := nextAvailableID(stableIDs.Services)
if err != nil {
fmt.Fprintf(os.Stderr, "error preparing service ids: %v\n", err)
os.Exit(1)
}
compiledServices := CompiledServicesOutput{
Groups: groups,
Services: make(map[string]Service),
}
serviceCount, err := compileServices("services", groups, stableIDs.Services, nextServiceID, compiledServices.Services)
if err != nil {
fmt.Fprintf(os.Stderr, "error compiling services: %v\n", err)
os.Exit(1)
}
if err := writeJSONFile("services.json", compiledServices); err != nil {
fmt.Fprintf(os.Stderr, "error writing services.json: %v\n", err)
os.Exit(1)
}
if err := writeJSONFile(stableIDsPath, stableIDs); err != nil {
fmt.Fprintf(os.Stderr, "error writing stable ids: %v\n", err)
os.Exit(1)
}
fmt.Printf("successfully compiled %d services in %d groups to services.json using %d stable ids\n", serviceCount, len(compiledServices.Groups), len(stableIDs.Services))
}
func compileServices(dir string, groups map[string]Group, serviceIDs map[string]int, nextServiceID int, dst map[string]Service) (int, error) {
entries, err := os.ReadDir(dir)
if err != nil {
return 0, err
}
var count int
for _, e := range entries {
if e.IsDir() || !strings.HasSuffix(e.Name(), ".json") {
continue
}
count++
d, err := os.ReadFile(filepath.Join(dir, e.Name()))
if err != nil {
return 0, fmt.Errorf("reading %s: %w", e.Name(), err)
}
var svc Service
if err := json.Unmarshal(d, &svc); err != nil {
return 0, fmt.Errorf("parsing %s: %w", e.Name(), err)
}
serviceID := strings.TrimSuffix(e.Name(), ".json")
numericID, ok := serviceIDs[serviceID]
if !ok {
numericID = nextServiceID
serviceIDs[serviceID] = numericID
nextServiceID++
fmt.Printf("assigned new service id %d -> %s\n", numericID, serviceID)
}
if _, ok := groups[svc.Group]; !ok {
return 0, fmt.Errorf("unknown group %q in %s", svc.Group, e.Name())
}
svc.ID = numericID
dst[serviceID] = svc
fmt.Printf("compiled service: %s -> %s (id=%d)\n", serviceID, svc.Group, svc.ID)
}
fmt.Printf("found %d service files\n", count)
return count, nil
}
func loadGroups(path string) (map[string]Group, error) {
d, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var g map[string]Group
if err := json.Unmarshal(d, &g); err != nil {
return nil, err
}
return g, nil
}
func loadStableIDs(path, legacyServicesPath string) (StableIDs, error) {
d, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return loadLegacyStableIDs(legacyServicesPath)
}
return StableIDs{}, err
}
var ids StableIDs
if err := json.Unmarshal(d, &ids); err != nil {
return StableIDs{}, err
}
ensureStableIDMaps(&ids)
return ids, nil
}
func loadLegacyStableIDs(servicesPath string) (StableIDs, error) {
serviceIDs, err := loadLegacyStableIDMap(servicesPath)
if err != nil {
return StableIDs{}, err
}
return StableIDs{
Services: serviceIDs,
}, nil
}
func loadLegacyStableIDMap(path string) (map[string]int, error) {
d, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return make(map[string]int), nil
}
return nil, err
}
var ids map[string]int
if err := json.Unmarshal(d, &ids); err != nil {
return nil, err
}
if ids == nil {
ids = make(map[string]int)
}
return ids, nil
}
func ensureStableIDMaps(ids *StableIDs) {
if ids.Services == nil {
ids.Services = make(map[string]int)
}
}
func nextAvailableID(ids map[string]int) (int, error) {
maxID := 0
seen := make(map[int]string, len(ids))
for serviceKey, id := range ids {
if id <= 0 {
return 0, fmt.Errorf("service %q has invalid id %d", serviceKey, id)
}
if existingService, ok := seen[id]; ok {
return 0, fmt.Errorf("duplicate id %d for services %q and %q", id, existingService, serviceKey)
}
seen[id] = serviceKey
if id > maxID {
maxID = id
}
}
return maxID + 1, nil
}
func writeJSONFile(path string, value any) error {
out, err := json.MarshalIndent(value, "", " ")
if err != nil {
return err
}
out = append(out, '\n')
return os.WriteFile(path, out, 0644)
}