Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close files earlier when generating fields files #16681

Merged
merged 1 commit into from
Feb 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions libbeat/generator/fields/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,24 +123,32 @@ func writeGeneratedFieldsYml(fieldFiles []*YmlFile, output io.Writer) error {
func GenerateFieldsYml(fieldFiles []*YmlFile) ([]byte, error) {
buf := bytes.NewBufferString("")
for _, p := range fieldFiles {
file, err := os.Open(p.Path)
err := generateFieldsYmlForFile(buf, p)
if err != nil {
return nil, err
}
defer file.Close()
}
return buf.Bytes(), nil
}

fs := bufio.NewScanner(file)
for fs.Scan() {
err = writeIndentedLine(buf, fs.Text()+"\n", p.Indent)
if err != nil {
return nil, err
}
}
if err := fs.Err(); err != nil {
return nil, err
func generateFieldsYmlForFile(buf *bytes.Buffer, p *YmlFile) error {
file, err := os.Open(p.Path)
if err != nil {
return err
}
defer file.Close()

fs := bufio.NewScanner(file)
for fs.Scan() {
err = writeIndentedLine(buf, fs.Text()+"\n", p.Indent)
if err != nil {
return err
}
}
return buf.Bytes(), nil
if err := fs.Err(); err != nil {
return err
}
return nil
}

func writeIndentedLine(buf *bytes.Buffer, line string, indent int) error {
Expand Down