add autocompletion for cscli (#717)

This commit is contained in:
AlteredCoder 2021-03-24 19:07:58 +01:00 committed by GitHub
parent 1e899c2211
commit 73a10ef0e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 178 additions and 4 deletions

View file

@ -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
}

View file

@ -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)
}

View file

@ -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.

View file

@ -24,6 +24,7 @@ cscli alerts delete -s crowdsecurity/ssh-bf"
-i, --ip string Source ip (shorthand for --scope ip --value <IP>)
-r, --range string Range source ip (shorthand for --scope range --value <RANGE>)
-a, --all delete all alerts
--contained query decisions contained by range
-h, --help help for delete
```

View file

@ -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

View file

@ -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

View file

@ -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
```

View file

@ -29,6 +29,7 @@ cscli dashboard setup -l 0.0.0.0 -p 443 --password <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

View file

@ -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
```

View file

@ -29,6 +29,7 @@ cscli decisions list -t ban
-i, --ip string restrict to alerts from this source ip (shorthand for --scope ip --value <IP>)
-r, --range string restrict to alerts from this source range (shorthand for --scope range --value <RANGE>)
--no-simu exclude decisions in simulation mode
--contained query decisions contained by range
-h, --help help for list
```

View file

@ -7,7 +7,7 @@ Manage local API machines
Machines Management.
To list/add/delete/register/validate machines
To list/add/delete/validate machines
### Examples

View file

@ -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

View file

@ -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

View file

@ -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`

View file

@ -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