diff --git a/payload.go b/payload.go index 541b236..81b1366 100644 --- a/payload.go +++ b/payload.go @@ -7,6 +7,8 @@ import ( "fmt" "io" "net/http" + "os" + "strings" "sync" "time" @@ -267,6 +269,74 @@ func initMetadata(project string) error { return nil } +func LoadMetadataFromFile(path string) error { + lines, err := getFileLines(path) + if err != nil { + return err + } + + if len(metadata) > 0 { + metadata = nil + } + metadata = make(map[string]Payload) + + var name string + for _, l := range lines { + if strings.HasPrefix(l, "[") && strings.HasSuffix(l, "]") { + name = strings.Trim(l, "[]") + metadata[name] = Payload{ + Lang: []Translation{ + Translation{ + Language: "sr", + FieldsLabels: make(map[string]string), + }, + }, + } + continue + } + + parts := strings.Split(l, ":") + if len(parts) >= 2 { + k := strings.TrimSpace(parts[0]) + v := strings.TrimSpace(parts[1]) + if v != "-" { + metadata[name].Lang[0].FieldsLabels[k] = v + } + } + } + + return nil +} + +func getFileLines(path string) ([]string, error) { + f, err := os.Open(path) + if err != nil { + return nil, err + } + defer f.Close() + + var s strings.Builder + for { + buf := make([]byte, 1024) + _, err := f.Read(buf) + if err != nil { + if err == io.EOF { + s.Write(buf) + break + } else { + return nil, err + } + } + + s.Write(buf) + } + + str := s.String() + lines := strings.Split(str, "\n") + + return lines, nil +} + func hotload(n int) { entityScan := make(map[string]int64) firstCheck := true