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

Fix make assets error in metricbeat (#13266) #2

Merged
merged 1 commit into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
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
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)
}