| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "io/ioutil" |
| 5 | "log" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | |
| 9 | "github.com/crowdsecurity/crowdsec/pkg/types" |
| 10 | "gopkg.in/yaml.v2" |
| 11 | ) |
| 12 | |
| 13 | type ParserResults struct { |
| 14 | ProvisionalResults []map[string]map[string]types.Event |
| 15 | FinalResults []types.Event |
| 16 | } |
| 17 | |
| 18 | func main() { |
| 19 | var ( |
| 20 | buf []byte |
| 21 | err error |
| 22 | results []types.Event = []types.Event{} |
| 23 | final types.Event = types.Event{ |
| 24 | Enriched: map[string]string{}, |
| 25 | Parsed: map[string]string{}, |
| 26 | Meta: map[string]string{}, |
| 27 | } |
| 28 | ) |
| 29 | _ = filepath.Walk(".", func(path string, info os.FileInfo, err error) error { |
| 30 | if err != nil { |
| 31 | log.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err) |
| 32 | return err |
| 33 | } |
| 34 | if !info.IsDir() && info.Name() == "parser_results.yaml" { |
| 35 | if buf, err = ioutil.ReadFile(path); err != nil { |
| 36 | log.Printf("Unable to read %s: %s", path, err) |
| 37 | return err |
| 38 | } |
| 39 | tmp := ParserResults{} |
| 40 | if err = yaml.Unmarshal(buf, &tmp); err != nil { |
| 41 | log.Printf("Unable to unmarshal path %s: %s", path, err) |
| 42 | } |
| 43 | results = append(results, tmp.FinalResults...) |
| 44 | } |
| 45 | return nil |
| 46 | |
| 47 | }) |
| 48 | |
| 49 | for _, result := range results { |
| 50 | for key, value := range result.Enriched { |
| 51 | final.Enriched[key] = value |
| 52 | } |
| 53 | for key, value := range result.Parsed { |
| 54 | final.Parsed[key] = value |
| 55 | } |
| 56 | for key, value := range result.Meta { |
| 57 | final.Meta[key] = value |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if buf, err = yaml.Marshal(final); err != nil { |
| 62 | log.Printf("Unable to marshal result: %s", err) |
| 63 | } |
| 64 | |
| 65 | if err = ioutil.WriteFile("exportedField.yaml", buf, 0644); err != nil { |
| 66 | log.Printf("Unable to write file: %s", err) |
| 67 | } |
| 68 | } |