From 73a10ef0e58e384844a7ff24641853b442f03ebd Mon Sep 17 00:00:00 2001 From: AlteredCoder <64792091+AlteredCoder@users.noreply.github.com> Date: Wed, 24 Mar 2021 19:07:58 +0100 Subject: [PATCH] add autocompletion for cscli (#717) --- cmd/crowdsec-cli/completion.go | 60 +++++++++++++++++ cmd/crowdsec-cli/main.go | 6 ++ docs/v1.X/docs/cscli/cscli.md | 2 + docs/v1.X/docs/cscli/cscli_alerts_delete.md | 1 + docs/v1.X/docs/cscli/cscli_alerts_list.md | 1 + docs/v1.X/docs/cscli/cscli_completion.md | 64 +++++++++++++++++++ .../v1.X/docs/cscli/cscli_dashboard_remove.md | 2 +- docs/v1.X/docs/cscli/cscli_dashboard_setup.md | 1 + .../v1.X/docs/cscli/cscli_decisions_delete.md | 1 + docs/v1.X/docs/cscli/cscli_decisions_list.md | 1 + docs/v1.X/docs/cscli/cscli_machines.md | 2 +- docs/v1.X/docs/cscli/cscli_machines_add.md | 4 +- docs/v1.X/docs/cscli/cscli_version.md | 31 +++++++++ docs/v1.X/docs/user_guide/cscli.md | 5 ++ docs/v1.X/mkdocs.yml | 1 + 15 files changed, 178 insertions(+), 4 deletions(-) create mode 100644 cmd/crowdsec-cli/completion.go create mode 100644 docs/v1.X/docs/cscli/cscli_completion.md create mode 100644 docs/v1.X/docs/cscli/cscli_version.md diff --git a/cmd/crowdsec-cli/completion.go b/cmd/crowdsec-cli/completion.go new file mode 100644 index 000000000..a251d4bd8 --- /dev/null +++ b/cmd/crowdsec-cli/completion.go @@ -0,0 +1,60 @@ +package main + +import ( + "os" + + "github.com/spf13/cobra" +) + +func NewCompletionCmd() *cobra.Command { + + var completionCmd = &cobra.Command{ + Use: "completion [bash|zsh]", + Short: "Generate completion script", + Long: `To load completions: + +### Bash: + + $ source <(cscli completion bash) + + # To load completions for each session, execute once: + + + # Linux: + + $ cscli completion bash | sudo tee /etc/bash_completion.d/cscli + + # macOS: + + $ cscli completion bash | sudo tee /usr/local/etc/bash_completion.d/cscli + +### Zsh: + + # If shell completion is not already enabled in your environment, + # you will need to enable it. You can execute the following once: + + $ echo "autoload -U compinit; compinit" >> ~/.zshrc + + # To load completions for each session, execute once: + + $ cscli completion zsh > "${fpath[1]}/_cscli" + + # You will need to start a new shell for this setup to take effect. +`, + DisableFlagsInUseLine: true, + ValidArgs: []string{"bash", "zsh"}, + Args: cobra.ExactValidArgs(1), + Run: func(cmd *cobra.Command, args []string) { + switch args[0] { + case "bash": + cmd.Root().GenBashCompletion(os.Stdout) + case "zsh": + cmd.Root().GenZshCompletion(os.Stdout) + /*case "fish": + cmd.Root().GenFishCompletion(os.Stdout, true) + */ + } + }, + } + return completionCmd +} diff --git a/cmd/crowdsec-cli/main.go b/cmd/crowdsec-cli/main.go index 031af7736..d1c211d8d 100644 --- a/cmd/crowdsec-cli/main.go +++ b/cmd/crowdsec-cli/main.go @@ -77,6 +77,10 @@ func initConfig() { } +var validArgs = []string{ + "scenarios", "parsers", "collections", "capi", "lapi", "postoverflows", "machines", "metrics", "bouncers", "alerts", "decisions", "simulation", "hub", "dashboard", "config", "completion", "version", +} + func main() { var rootCmd = &cobra.Command{ @@ -84,6 +88,7 @@ func main() { Short: "cscli allows you to manage crowdsec", Long: `cscli is the main command to interact with your crowdsec service, scenarios & db. It is meant to allow you to manage bans, parsers/scenarios/etc, api and generally manage you crowdsec setup.`, + ValidArgs: validArgs, /*TBD examples*/ } var cmdDocGen = &cobra.Command{ @@ -142,6 +147,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall rootCmd.AddCommand(NewPostOverflowsCmd()) rootCmd.AddCommand(NewCapiCmd()) rootCmd.AddCommand(NewLapiCmd()) + rootCmd.AddCommand(NewCompletionCmd()) if err := rootCmd.Execute(); err != nil { log.Fatalf("While executing root command : %s", err) } diff --git a/docs/v1.X/docs/cscli/cscli.md b/docs/v1.X/docs/cscli/cscli.md index c8655101e..d29fea58c 100644 --- a/docs/v1.X/docs/cscli/cscli.md +++ b/docs/v1.X/docs/cscli/cscli.md @@ -26,6 +26,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall * [cscli bouncers](cscli_bouncers.md) - Manage bouncers * [cscli capi](cscli_capi.md) - Manage interaction with Central API (CAPI) * [cscli collections](cscli_collections.md) - Manage collections from hub +* [cscli completion](cscli_completion.md) - Generate completion script * [cscli config](cscli_config.md) - Allows to view current config * [cscli dashboard](cscli_dashboard.md) - Manage your metabase dashboard container * [cscli decisions](cscli_decisions.md) - Manage decisions @@ -37,5 +38,6 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall * [cscli postoverflows](cscli_postoverflows.md) - Install/Remove/Upgrade/Inspect postoverflow(s) from hub * [cscli scenarios](cscli_scenarios.md) - Install/Remove/Upgrade/Inspect scenario(s) from hub * [cscli simulation](cscli_simulation.md) - Manage simulation status of scenarios +* [cscli version](cscli_version.md) - Display version and exit. diff --git a/docs/v1.X/docs/cscli/cscli_alerts_delete.md b/docs/v1.X/docs/cscli/cscli_alerts_delete.md index 2d97ce9da..103716391 100644 --- a/docs/v1.X/docs/cscli/cscli_alerts_delete.md +++ b/docs/v1.X/docs/cscli/cscli_alerts_delete.md @@ -24,6 +24,7 @@ cscli alerts delete -s crowdsecurity/ssh-bf" -i, --ip string Source ip (shorthand for --scope ip --value ) -r, --range string Range source ip (shorthand for --scope range --value ) -a, --all delete all alerts + --contained query decisions contained by range -h, --help help for delete ``` diff --git a/docs/v1.X/docs/cscli/cscli_alerts_list.md b/docs/v1.X/docs/cscli/cscli_alerts_list.md index d4b75c742..5d5ef7c45 100644 --- a/docs/v1.X/docs/cscli/cscli_alerts_list.md +++ b/docs/v1.X/docs/cscli/cscli_alerts_list.md @@ -27,6 +27,7 @@ cscli alerts list --type ban --type string restrict to alerts with given decision type (ie. ban, captcha) --scope string restrict to alerts of this scope (ie. ip,range) -v, --value string the value to match for in the specified scope + --contained query decisions contained by range -m, --machine print machines that sended alerts -l, --limit int limit size of alerts list table (0 to view all alerts) (default 50) -h, --help help for list diff --git a/docs/v1.X/docs/cscli/cscli_completion.md b/docs/v1.X/docs/cscli/cscli_completion.md new file mode 100644 index 000000000..48c38841e --- /dev/null +++ b/docs/v1.X/docs/cscli/cscli_completion.md @@ -0,0 +1,64 @@ +## cscli completion + +Generate completion script + +### Synopsis + +To load completions: + +### Bash: + + $ source <(cscli completion bash) + + # To load completions for each session, execute once: + + + # Linux: + + $ cscli completion bash | sudo tee /etc/bash_completion.d/cscli + + # macOS: + + $ cscli completion bash | sudo tee /usr/local/etc/bash_completion.d/cscli + +### Zsh: + + # If shell completion is not already enabled in your environment, + # you will need to enable it. You can execute the following once: + + $ echo "autoload -U compinit; compinit" >> ~/.zshrc + + # To load completions for each session, execute once: + + $ cscli completion zsh > "${fpath[1]}/_cscli" + + # You will need to start a new shell for this setup to take effect. + + +``` +cscli completion [bash|zsh] +``` + +### Options + +``` + -h, --help help for completion +``` + +### Options inherited from parent commands + +``` + -c, --config string path to crowdsec config file (default "/etc/crowdsec/config.yaml") + --debug Set logging to debug. + --error Set logging to error. + --info Set logging to info. + -o, --output string Output format : human, json, raw. + --trace Set logging to trace. + --warning Set logging to warning. +``` + +### SEE ALSO + +* [cscli](cscli.md) - cscli allows you to manage crowdsec + + diff --git a/docs/v1.X/docs/cscli/cscli_dashboard_remove.md b/docs/v1.X/docs/cscli/cscli_dashboard_remove.md index bc4e5f7a3..0afcc6801 100644 --- a/docs/v1.X/docs/cscli/cscli_dashboard_remove.md +++ b/docs/v1.X/docs/cscli/cscli_dashboard_remove.md @@ -22,7 +22,7 @@ cscli dashboard remove --force ### Options ``` - -f, --force Force remove : stop the container if running and remove. + -f, --force Remove also the metabase image -h, --help help for remove -y, --yes force yes ``` diff --git a/docs/v1.X/docs/cscli/cscli_dashboard_setup.md b/docs/v1.X/docs/cscli/cscli_dashboard_setup.md index ede8201d6..fd282d683 100644 --- a/docs/v1.X/docs/cscli/cscli_dashboard_setup.md +++ b/docs/v1.X/docs/cscli/cscli_dashboard_setup.md @@ -29,6 +29,7 @@ cscli dashboard setup -l 0.0.0.0 -p 443 --password -l, --listen string Listen address of container (default "127.0.0.1") --password string metabase password -p, --port string Listen port of container (default "3000") + -y, --yes force yes ``` ### Options inherited from parent commands diff --git a/docs/v1.X/docs/cscli/cscli_decisions_delete.md b/docs/v1.X/docs/cscli/cscli_decisions_delete.md index 0a291e68c..e7747ecfe 100644 --- a/docs/v1.X/docs/cscli/cscli_decisions_delete.md +++ b/docs/v1.X/docs/cscli/cscli_decisions_delete.md @@ -26,6 +26,7 @@ cscli decisions delete --type captcha -t, --type string the decision type (ie. ban,captcha) -v, --value string the value to match for in the specified scope --all delete all decisions + --contained query decisions contained by range -h, --help help for delete ``` diff --git a/docs/v1.X/docs/cscli/cscli_decisions_list.md b/docs/v1.X/docs/cscli/cscli_decisions_list.md index ae9634dd0..1c1b16981 100644 --- a/docs/v1.X/docs/cscli/cscli_decisions_list.md +++ b/docs/v1.X/docs/cscli/cscli_decisions_list.md @@ -29,6 +29,7 @@ cscli decisions list -t ban -i, --ip string restrict to alerts from this source ip (shorthand for --scope ip --value ) -r, --range string restrict to alerts from this source range (shorthand for --scope range --value ) --no-simu exclude decisions in simulation mode + --contained query decisions contained by range -h, --help help for list ``` diff --git a/docs/v1.X/docs/cscli/cscli_machines.md b/docs/v1.X/docs/cscli/cscli_machines.md index 56002e4f5..964bc0ab7 100644 --- a/docs/v1.X/docs/cscli/cscli_machines.md +++ b/docs/v1.X/docs/cscli/cscli_machines.md @@ -7,7 +7,7 @@ Manage local API machines Machines Management. -To list/add/delete/register/validate machines +To list/add/delete/validate machines ### Examples diff --git a/docs/v1.X/docs/cscli/cscli_machines_add.md b/docs/v1.X/docs/cscli/cscli_machines_add.md index 8eb8e297c..df9eef9e0 100644 --- a/docs/v1.X/docs/cscli/cscli_machines_add.md +++ b/docs/v1.X/docs/cscli/cscli_machines_add.md @@ -23,8 +23,8 @@ cscli machines add MyTestMachine --password MyPassword ### Options ``` - -a, --auto add the machine automatically (will generate also the username if not provided) - -f, --file string output file destination + -a, --auto automatically generate password (and username if not provided) + -f, --file string output file destination (defaults to /etc/crowdsec/local_api_credentials.yaml) --force will force add the machine if it already exist -h, --help help for add -i, --interactive interfactive mode to enter the password diff --git a/docs/v1.X/docs/cscli/cscli_version.md b/docs/v1.X/docs/cscli/cscli_version.md new file mode 100644 index 000000000..38aacae7f --- /dev/null +++ b/docs/v1.X/docs/cscli/cscli_version.md @@ -0,0 +1,31 @@ +## cscli version + +Display version and exit. + +``` +cscli version [flags] +``` + +### Options + +``` + -h, --help help for version +``` + +### Options inherited from parent commands + +``` + -c, --config string path to crowdsec config file (default "/etc/crowdsec/config.yaml") + --debug Set logging to debug. + --error Set logging to error. + --info Set logging to info. + -o, --output string Output format : human, json, raw. + --trace Set logging to trace. + --warning Set logging to warning. +``` + +### SEE ALSO + +* [cscli](cscli.md) - cscli allows you to manage crowdsec + + diff --git a/docs/v1.X/docs/user_guide/cscli.md b/docs/v1.X/docs/user_guide/cscli.md index bb5d19f2c..0d5294474 100644 --- a/docs/v1.X/docs/user_guide/cscli.md +++ b/docs/v1.X/docs/user_guide/cscli.md @@ -12,6 +12,11 @@ Take a look at the [dedicated documentation](/Crowdsec/v1/cscli/cscli) +!!! tips + You can enable `cscli` auto completion in `bash` or `zsh`. + + You can find `cscli completion` documentation [here](/Crowdsec/v1/cscli/cscli_completion/). + # Configuration `{{v1X.cli.name}}` shares the configuration file of {{v1X.crowdsec.name}}, usually in `/etc/crowdsec/config.yaml` diff --git a/docs/v1.X/mkdocs.yml b/docs/v1.X/mkdocs.yml index 0b0ef58b3..ec3c069e6 100644 --- a/docs/v1.X/mkdocs.yml +++ b/docs/v1.X/mkdocs.yml @@ -26,6 +26,7 @@ nav: - Alerts: cscli/cscli_alerts.md - Bouncers: cscli/cscli_bouncers.md - Collections: cscli/cscli_collections.md + - Completion: cscli/cscli_completion.md - Config: cscli/cscli_config.md - Dashboard: cscli/cscli_dashboard.md - Decisions: cscli/cscli_decisions.md