-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add deprecation messages for deprecated flags * Ensure RootCmd works when imported somewhere outside main.go * Add `completion` command * Add `export config` subcommand * Add `export template` subcommand * Be more verbose on cmd errors * Do not fail when no template settings are given * Do not deprecate `-setup` flag * Remove version mangling in `export template` * Remove `output_to_file` template settings Deprecated in favor of `beat export template`
- Loading branch information
Showing
25 changed files
with
261 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
package beat | ||
|
||
type TemplateConfig struct { | ||
Enabled bool `config:"enabled"` | ||
Name string `config:"name"` | ||
Fields string `config:"fields"` | ||
Overwrite bool `config:"overwrite"` | ||
OutputToFile string `config:"output_to_file"` | ||
Settings map[string]string `config:"settings"` | ||
Enabled bool `config:"enabled"` | ||
Name string `config:"name"` | ||
Fields string `config:"fields"` | ||
Overwrite bool `config:"overwrite"` | ||
Settings map[string]string `config:"settings"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func genCompletionCmd(name, version string, rootCmd *BeatsRootCmd) *cobra.Command { | ||
completionCmd := cobra.Command{ | ||
Use: "completion SHELL", | ||
Short: "Output shell completion code for the specified shell (bash only by the moment)", | ||
// We don't want to expose this one in help: | ||
Hidden: true, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) != 1 { | ||
fmt.Println("Expected one argument with the desired shell") | ||
os.Exit(1) | ||
} | ||
|
||
switch args[0] { | ||
case "bash": | ||
rootCmd.GenBashCompletion(os.Stdout) | ||
default: | ||
fmt.Printf("Unknown shell %s, only bash is available\n", args[0]) | ||
} | ||
}, | ||
} | ||
|
||
return &completionCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/elastic/beats/libbeat/beat" | ||
"github.com/elastic/beats/libbeat/cmd/export" | ||
) | ||
|
||
func genExportCmd(name, beatVersion string, beatCreator beat.Creator) *cobra.Command { | ||
exportCmd := &cobra.Command{ | ||
Use: "export", | ||
Short: "Export current config or index template", | ||
} | ||
|
||
exportCmd.AddCommand(export.GenExportConfigCmd(name, beatVersion, beatCreator)) | ||
exportCmd.AddCommand(export.GenTemplateConfigCmd(name, beatVersion, beatCreator)) | ||
|
||
return exportCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package export | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v2" | ||
|
||
"github.com/elastic/beats/libbeat/beat" | ||
) | ||
|
||
func GenExportConfigCmd(name, beatVersion string, beatCreator beat.Creator) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "config", | ||
Short: "Export current config to stdout", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
b, err := beat.New(name, beatVersion) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error initializing beat: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
err = b.Init() | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error initializing beat: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
var config map[string]interface{} | ||
err = b.RawConfig.Unpack(&config) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error unpacking config") | ||
os.Exit(1) | ||
} | ||
res, err := yaml.Marshal(config) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error converting config to YAML format") | ||
os.Exit(1) | ||
} | ||
|
||
os.Stdout.Write(res) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package export | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/elastic/beats/libbeat/beat" | ||
"github.com/elastic/beats/libbeat/template" | ||
) | ||
|
||
func GenTemplateConfigCmd(name, beatVersion string, beatCreator beat.Creator) *cobra.Command { | ||
genTemplateConfigCmd := &cobra.Command{ | ||
Use: "template", | ||
Short: "Export index template to stdout", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
version, _ := cmd.Flags().GetString("es.version") | ||
index, _ := cmd.Flags().GetString("index") | ||
|
||
b, err := beat.New(name, beatVersion) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error initializing beat: %s\n", err) | ||
os.Exit(1) | ||
} | ||
err = b.Init() | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error initializing beat: %s\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
cfg := template.DefaultConfig | ||
if b.Config.Template.Enabled() { | ||
err = b.Config.Template.Unpack(&cfg) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error getting template settings: %+v", err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
tmpl, err := template.New(b.Info.Version, version, index, cfg.Settings) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error generating template: %+v", err) | ||
os.Exit(1) | ||
} | ||
|
||
templateString, err := tmpl.Load(cfg.Fields) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error generating template: %+v", err) | ||
os.Exit(1) | ||
} | ||
|
||
_, err = os.Stdout.WriteString(templateString.StringToPrint() + "\n") | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error writing template: %+v", err) | ||
os.Exit(1) | ||
} | ||
}, | ||
} | ||
|
||
genTemplateConfigCmd.Flags().String("es.version", beatVersion, "Elasticsearch version") | ||
genTemplateConfigCmd.Flags().String("index", name, "Base index name") | ||
|
||
return genTemplateConfigCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.