Skip to content

Commit

Permalink
use stringvar
Browse files Browse the repository at this point in the history
  • Loading branch information
kvch committed Jul 26, 2018
1 parent c3c6aba commit 6ff6069
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions libbeat/scripts/cmd/global_fields/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,30 @@ import (
)

func main() {
esBeatsPath := flag.String("es_beats_path", "..", "Path to elastic/beats")
beatPath := flag.String("beat_path", ".", "Path to your Beat")
output := flag.String("out", "-", "Path to output. Default: stdout")
var (
esBeatsPath string
beatPath string
output string
)
flag.StringVar(&esBeatsPath, "es_beats_path", "..", "Path to elastic/beats")
flag.StringVar(&beatPath, "beat_path", ".", "Path to your Beat")
flag.StringVar(&output, "out", "-", "Path to output. Default: stdout")
flag.Parse()

beatFieldsPaths := flag.Args()
name := filepath.Base(*beatPath)
name := filepath.Base(beatPath)

if *beatPath == "" {
if beatPath == "" {
fmt.Fprintf(os.Stderr, "beat_path cannot be empty")
os.Exit(1)
}

esBeats, err := os.Open(*esBeatsPath)
esBeats, err := os.Open(esBeatsPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening elastic/beats: %+v\n", err)
os.Exit(1)
}
beat, err := os.Open(*beatPath)
beat, err := os.Open(beatPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening target Beat: %+v\n", err)
os.Exit(1)
Expand All @@ -62,15 +67,15 @@ func main() {
}

if len(beatFieldsPaths) == 0 && os.SameFile(esBeatsInfo, beatInfo) {
if *output != "-" {
if output != "-" {
fmt.Println("No field files to collect")
}
return
}

var fieldsFiles []*fields.YmlFile
for _, fieldsFilePath := range beatFieldsPaths {
pathToModules := filepath.Join(*beatPath, fieldsFilePath)
pathToModules := filepath.Join(beatPath, fieldsFilePath)

fieldsFile, err := fields.CollectModuleFiles(pathToModules)
if err != nil {
Expand All @@ -81,13 +86,13 @@ func main() {
fieldsFiles = append(fieldsFiles, fieldsFile...)
}

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

if *output != "-" {
if output != "-" {
fmt.Printf("Generated fields.yml for %s\n", name)
}
}

0 comments on commit 6ff6069

Please sign in to comment.