|
| 1 | +package fields |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "path" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + generatedFieldsYml = filepath.Join("_meta", "fields.generated.yml") |
| 15 | +) |
| 16 | + |
| 17 | +// YmlFile holds the info on files and how to write them into the global fields.yml |
| 18 | +type YmlFile struct { |
| 19 | + Path string |
| 20 | + Indent int |
| 21 | +} |
| 22 | + |
| 23 | +func collectBeatFiles(beatPath string, fieldFiles []*YmlFile) ([]*YmlFile, error) { |
| 24 | + commonFields := filepath.Join(beatPath, "_meta", "fields.common.yml") |
| 25 | + _, err := os.Stat(commonFields) |
| 26 | + if os.IsNotExist(err) { |
| 27 | + return fieldFiles, nil |
| 28 | + } else if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + |
| 32 | + files := []*YmlFile{ |
| 33 | + &YmlFile{ |
| 34 | + Path: commonFields, |
| 35 | + Indent: 0, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + return append(files, fieldFiles...), nil |
| 40 | +} |
| 41 | + |
| 42 | +func writeGeneratedFieldsYml(beatsPath string, fieldFiles []*YmlFile) error { |
| 43 | + outPath := path.Join(beatsPath, generatedFieldsYml) |
| 44 | + f, err := os.Create(outPath) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + defer f.Close() |
| 49 | + |
| 50 | + fw := bufio.NewWriter(f) |
| 51 | + for _, p := range fieldFiles { |
| 52 | + ff, err := os.Open(p.Path) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + defer ff.Close() |
| 57 | + |
| 58 | + fs := bufio.NewScanner(ff) |
| 59 | + for fs.Scan() { |
| 60 | + err = writeIndentedLine(fw, fs.Text()+"\n", p.Indent) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | + if err := fs.Err(); err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func writeIndentedLine(fw *bufio.Writer, l string, indent int) error { |
| 74 | + ll := strings.Repeat(" ", indent) + l |
| 75 | + fmt.Fprint(fw, ll) |
| 76 | + return fw.Flush() |
| 77 | +} |
| 78 | + |
| 79 | +// Generate collects fields.yml files and concatenates them into one global file. |
| 80 | +func Generate(esBeatsPath, beatPath string, files []*YmlFile) error { |
| 81 | + files, err := collectBeatFiles(beatPath, files) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + err = writeGeneratedFieldsYml(beatPath, files) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + return AppendFromLibbeat(esBeatsPath, beatPath) |
| 92 | +} |
| 93 | + |
| 94 | +// AppendFromLibbeat appends fields.yml of libbeat to the fields.yml |
| 95 | +func AppendFromLibbeat(esBeatsPath, beatPath string) error { |
| 96 | + fieldsMetaPath := path.Join(beatPath, "_meta", "fields.yml") |
| 97 | + generatedPath := path.Join(beatPath, generatedFieldsYml) |
| 98 | + |
| 99 | + err := createIfNotExists(fieldsMetaPath, generatedPath) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + if isLibbeat(beatPath) { |
| 105 | + out := filepath.Join(esBeatsPath, "libbeat", "fields.yml") |
| 106 | + return copyFileWithFlag(generatedPath, out, os.O_RDWR|os.O_CREATE|os.O_TRUNC) |
| 107 | + } |
| 108 | + |
| 109 | + libbeatPath := filepath.Join(esBeatsPath, "libbeat", generatedFieldsYml) |
| 110 | + out := filepath.Join(beatPath, "fields.yml") |
| 111 | + err = copyFileWithFlag(libbeatPath, out, os.O_RDWR|os.O_CREATE|os.O_TRUNC) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + return copyFileWithFlag(generatedPath, out, os.O_WRONLY|os.O_APPEND) |
| 116 | +} |
| 117 | + |
| 118 | +func isLibbeat(beatPath string) bool { |
| 119 | + return filepath.Base(beatPath) == "libbeat" |
| 120 | +} |
| 121 | + |
| 122 | +func createIfNotExists(inPath, outPath string) error { |
| 123 | + _, err := os.Stat(outPath) |
| 124 | + if os.IsNotExist(err) { |
| 125 | + err := copyFileWithFlag(inPath, outPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC) |
| 126 | + if err != nil { |
| 127 | + fmt.Println("Cannot find _meta/fields.yml") |
| 128 | + } |
| 129 | + return nil |
| 130 | + } |
| 131 | + return err |
| 132 | +} |
| 133 | + |
| 134 | +func copyFileWithFlag(in, out string, flag int) error { |
| 135 | + input, err := ioutil.ReadFile(in) |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + output, err := os.OpenFile(out, flag, 0644) |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + defer output.Close() |
| 145 | + |
| 146 | + _, err = output.Write(input) |
| 147 | + return err |
| 148 | + |
| 149 | +} |
0 commit comments