Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 14:15 UTC
cmd: add completion command (#4994)
* cmd: add completion command * error check
Mohammed Al Sahaf committed
Aug 31, 2022 at 02:24 UTC
d605ebe75a2043199ba53022adaa20be680cf051
1 file changed
+63
cmd/commands.go
+63
@@ -22,6 +22,7 @@ import (
22
"strings"
23
24
"github.com/caddyserver/caddy/v2"
25
+ "github.com/spf13/cobra"
26
"github.com/spf13/cobra/doc"
27
)
28
@@ -379,6 +380,68 @@ The manual pages are sorted into the section specified by the argument of --sect
380
return fs
381
}(),
382
})
383
+
384
+ // source: https://github.com/spf13/cobra/blob/main/shell_completions.md
385
+ rootCmd.AddCommand(&cobra.Command{
386
+ Use: "completion [bash|zsh|fish|powershell]",
387
+ Short: "Generate completion script",
388
+ Long: fmt.Sprintf(`To load completions:
389
+
390
+ Bash:
391
+
392
+ $ source <(%[1]s completion bash)
393
+
394
+ # To load completions for each session, execute once:
395
+ # Linux:
396
+ $ %[1]s completion bash > /etc/bash_completion.d/%[1]s
397
+ # macOS:
398
+ $ %[1]s completion bash > $(brew --prefix)/etc/bash_completion.d/%[1]s
399
+
400
+ Zsh:
401
+
402
+ # If shell completion is not already enabled in your environment,
403
+ # you will need to enable it. You can execute the following once:
404
+
405
+ $ echo "autoload -U compinit; compinit" >> ~/.zshrc
406
+
407
+ # To load completions for each session, execute once:
408
+ $ %[1]s completion zsh > "${fpath[1]}/_%[1]s"
409
+
410
+ # You will need to start a new shell for this setup to take effect.
411
+
412
+ fish:
413
+
414
+ $ %[1]s completion fish | source
415
+
416
+ # To load completions for each session, execute once:
417
+ $ %[1]s completion fish > ~/.config/fish/completions/%[1]s.fish
418
+
419
+ PowerShell:
420
+
421
+ PS> %[1]s completion powershell | Out-String | Invoke-Expression
422
+
423
+ # To load completions for every new session, run:
424
+ PS> %[1]s completion powershell > %[1]s.ps1
425
+ # and source this file from your PowerShell profile.
426
+ `, rootCmd.Root().Name()),
427
+ DisableFlagsInUseLine: true,
428
+ ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
429
+ Args: cobra.ExactValidArgs(1),
430
+ RunE: func(cmd *cobra.Command, args []string) error {
431
+ switch args[0] {
432
+ case "bash":
433
+ return cmd.Root().GenBashCompletion(os.Stdout)
434
+ case "zsh":
435
+ return cmd.Root().GenZshCompletion(os.Stdout)
436
+ case "fish":
437
+ return cmd.Root().GenFishCompletion(os.Stdout, true)
438
+ case "powershell":
439
+ return cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
440
+ default:
441
+ return fmt.Errorf("unrecognized shell: %s", args[0])
442
+ }
443
+ },
444
+ })
445
}
446
447
// RegisterCommand registers the command cmd.