-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Subcommands enhancements #4510
Merged
Merged
Subcommands enhancements #4510
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1180a92
Add deprecation messages for deprecated flags
exekias af6cbde
Ensure RootCmd works when imported somewhere outside main.go
exekias f4923ae
Add `completion` command
exekias b57ac77
Add `export config` subcommand
exekias c2a9e13
Add `export template` subcommand
exekias 8e0f242
Be more verbose on cmd errors
exekias 11df391
Do not fail when no template settings are given
exekias 155f07a
Do not deprecate `-setup` flag
exekias 310bb52
Remove version mangling in `export template`
exekias d7d5e0a
Remove `output_to_file` template settings
exekias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like it too much that we get these writes directly to stdout/stderr. Perhaps it would be possible to init logging with stderr on any non-run command?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's your exact concern? I tried to do it just the other way around, strip all log metadata as this is command line
It would be possible though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just don't like it that logp and fmt.Println are interleaved in the code. But def. not a show stopper.