Commit 68e590a604fbe8813dfcc79c42a4f3e10432158e
1 parent
d7e8e0a94e
Exists in
master
load metadata from file
Showing
1 changed file
with
70 additions
and
0 deletions
Show diff stats
payload.go
... | ... | @@ -7,6 +7,8 @@ import ( |
7 | 7 | "fmt" |
8 | 8 | "io" |
9 | 9 | "net/http" |
10 | + "os" | |
11 | + "strings" | |
10 | 12 | "sync" |
11 | 13 | "time" |
12 | 14 | |
... | ... | @@ -267,6 +269,74 @@ func initMetadata(project string) error { |
267 | 269 | return nil |
268 | 270 | } |
269 | 271 | |
272 | +func LoadMetadataFromFile(path string) error { | |
273 | + lines, err := getFileLines(path) | |
274 | + if err != nil { | |
275 | + return err | |
276 | + } | |
277 | + | |
278 | + if len(metadata) > 0 { | |
279 | + metadata = nil | |
280 | + } | |
281 | + metadata = make(map[string]Payload) | |
282 | + | |
283 | + var name string | |
284 | + for _, l := range lines { | |
285 | + if strings.HasPrefix(l, "[") && strings.HasSuffix(l, "]") { | |
286 | + name = strings.Trim(l, "[]") | |
287 | + metadata[name] = Payload{ | |
288 | + Lang: []Translation{ | |
289 | + Translation{ | |
290 | + Language: "sr", | |
291 | + FieldsLabels: make(map[string]string), | |
292 | + }, | |
293 | + }, | |
294 | + } | |
295 | + continue | |
296 | + } | |
297 | + | |
298 | + parts := strings.Split(l, ":") | |
299 | + if len(parts) >= 2 { | |
300 | + k := strings.TrimSpace(parts[0]) | |
301 | + v := strings.TrimSpace(parts[1]) | |
302 | + if v != "-" { | |
303 | + metadata[name].Lang[0].FieldsLabels[k] = v | |
304 | + } | |
305 | + } | |
306 | + } | |
307 | + | |
308 | + return nil | |
309 | +} | |
310 | + | |
311 | +func getFileLines(path string) ([]string, error) { | |
312 | + f, err := os.Open(path) | |
313 | + if err != nil { | |
314 | + return nil, err | |
315 | + } | |
316 | + defer f.Close() | |
317 | + | |
318 | + var s strings.Builder | |
319 | + for { | |
320 | + buf := make([]byte, 1024) | |
321 | + _, err := f.Read(buf) | |
322 | + if err != nil { | |
323 | + if err == io.EOF { | |
324 | + s.Write(buf) | |
325 | + break | |
326 | + } else { | |
327 | + return nil, err | |
328 | + } | |
329 | + } | |
330 | + | |
331 | + s.Write(buf) | |
332 | + } | |
333 | + | |
334 | + str := s.String() | |
335 | + lines := strings.Split(str, "\n") | |
336 | + | |
337 | + return lines, nil | |
338 | +} | |
339 | + | |
270 | 340 | func hotload(n int) { |
271 | 341 | entityScan := make(map[string]int64) |
272 | 342 | firstCheck := true | ... | ... |