Browse Source

add autocompletion for cscli (#717)

AlteredCoder 4 years ago
parent
commit
73a10ef0e5

+ 60 - 0
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
+}

+ 6 - 0
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)
 	}

+ 2 - 0
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.
 
 

+ 1 - 0
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 <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
 ```
 

+ 1 - 0
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

+ 64 - 0
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
+
+

+ 1 - 1
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
 ```

+ 1 - 0
docs/v1.X/docs/cscli/cscli_dashboard_setup.md

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

+ 1 - 0
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
 ```
 

+ 1 - 0
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 <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
 ```
 

+ 1 - 1
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

+ 2 - 2
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

+ 31 - 0
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
+
+

+ 5 - 0
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`

+ 1 - 0
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