github.com/hazelops/ize@v1.1.12-0.20230915191306-97d7c0e48f11/internal/commands/completion.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/spf13/cobra" 8 ) 9 10 func NewCmdCompletion() *cobra.Command { 11 cmd := &cobra.Command{ 12 Use: "completion [bash|zsh|fish|powershell]", 13 Short: "Generate completion script", 14 Long: fmt.Sprintf(`To load completions: 15 16 Bash: 17 18 $ source <(%[1]s completion bash) 19 20 # To load completions for each session, execute once: 21 # Linux: 22 $ %[1]s completion bash > /etc/bash_completion.d/%[1]s 23 # macOS: 24 $ %[1]s completion bash > /usr/local/etc/bash_completion.d/%[1]s 25 26 Zsh: 27 28 # If shell completion is not already enabled in your environment, 29 # you will need to enable it. You can execute the following once: 30 31 $ echo "autoload -U compinit; compinit" >> ~/.zshrc 32 33 # To load completions for each session, execute once: 34 $ %[1]s completion zsh > "${fpath[1]}/_%[1]s" 35 36 # You will need to start a new shell for this setup to take effect. 37 38 fish: 39 40 $ %[1]s completion fish | source 41 42 # To load completions for each session, execute once: 43 $ %[1]s completion fish > ~/.config/fish/completions/%[1]s.fish 44 45 PowerShell: 46 47 PS> %[1]s completion powershell | Out-String | Invoke-Expression 48 49 # To load completions for every new session, run: 50 PS> %[1]s completion powershell > %[1]s.ps1 51 # and source this file from your PowerShell profile. 52 `, " "), 53 DisableFlagsInUseLine: true, 54 ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, 55 Args: cobra.ExactValidArgs(1), 56 Run: func(cmd *cobra.Command, args []string) { 57 switch args[0] { 58 case "bash": 59 _ = cmd.Root().GenBashCompletion(os.Stdout) 60 case "zsh": 61 _ = cmd.Root().GenZshCompletion(os.Stdout) 62 case "fish": 63 _ = cmd.Root().GenFishCompletion(os.Stdout, true) 64 case "powershell": 65 _ = cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout) 66 } 67 }, 68 } 69 70 return cmd 71 }