Skip to content

Commit

Permalink
Fix make assets error in metricbeat
Browse files Browse the repository at this point in the history
On elastic#13188 a check is added on global fields generator to check that the
generated fields file is valid. This check was done by reading the
result from disk, but this file is not available when output is stdout.

Modify the check so it is done over the buffered result before writing
to file or stdout.
  • Loading branch information
jsoriano committed Aug 16, 2019
1 parent 3f5e799 commit f365687
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
22 changes: 4 additions & 18 deletions libbeat/generator/fields/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package fields
import (
"bufio"
"bytes"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -104,28 +105,13 @@ func isLibbeat(beatPath string) bool {
return filepath.Base(beatPath) == "libbeat"
}

func writeGeneratedFieldsYml(fieldFiles []*YmlFile, output string) error {
func writeGeneratedFieldsYml(fieldFiles []*YmlFile, output io.Writer) error {
data, err := GenerateFieldsYml(fieldFiles)
if err != nil {
return err
}

if output == "-" {
fw := bufio.NewWriter(os.Stdout)
_, err = fw.Write(data)
if err != nil {
return err
}
return fw.Flush()
}

f, err := os.Create(output)
if err != nil {
return err
}
defer f.Close()

fw := bufio.NewWriter(f)
fw := bufio.NewWriter(output)
_, err = fw.Write(data)
if err != nil {
return err
Expand Down Expand Up @@ -164,7 +150,7 @@ func writeIndentedLine(buf *bytes.Buffer, line string, indent int) error {
}

// Generate collects fields.yml files and concatenates them into one global file.
func Generate(esBeatsPath, beatPath string, files []*YmlFile, output string) error {
func Generate(esBeatsPath, beatPath string, files []*YmlFile, output io.Writer) error {
files, err := collectCommonFiles(esBeatsPath, beatPath, files)
if err != nil {
return err
Expand Down
18 changes: 14 additions & 4 deletions libbeat/scripts/cmd/global_fields/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
package main

import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -90,21 +92,29 @@ func main() {
fieldsFiles = append(fieldsFiles, fieldsFile...)
}

err = fields.Generate(esBeatsPath, beatPath, fieldsFiles, output)
var buffer bytes.Buffer
err = fields.Generate(esBeatsPath, beatPath, fieldsFiles, &buffer)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot generate global fields.yml file for %s: %+v\n", name, err)
os.Exit(3)
}

_, err = mapping.LoadFieldsYaml(output)
_, err = mapping.LoadFields(buffer.Bytes())
if err != nil {
fmt.Fprintf(os.Stderr, "Generated global fields.yml file for %s is invalid: %+v\n", name, err)
os.Exit(3)
}

outputPath, _ := filepath.Abs(output)
if output == "-" {
fmt.Print(buffer.String())
return
}

err = ioutil.WriteFile(output, buffer.Bytes(), 0644)
if err != nil {
outputPath = output
fmt.Fprintf(os.Stderr, "Cannot write global fields.yml file for %s: %v", name, err)
}

outputPath, _ := filepath.Abs(output)
fmt.Fprintf(os.Stderr, "Generated fields.yml for %s to %s\n", name, outputPath)
}

0 comments on commit f365687

Please sign in to comment.