sq/cli/cmd_completion.go

77 lines
1.9 KiB
Go
Raw Normal View History

2020-08-06 20:58:47 +03:00
package cli
import (
"github.com/neilotoole/sq/cli/run"
2020-08-06 20:58:47 +03:00
"github.com/spf13/cobra"
"github.com/neilotoole/sq/libsq/core/errz"
2020-08-06 20:58:47 +03:00
)
func newCompletionCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate shell completion script",
RunE: execCompletion,
Long: `To load completions:
2020-08-06 20:58:47 +03:00
Bash:
2020-08-06 20:58:47 +03:00
$ source <(sq completion bash)
2020-08-06 20:58:47 +03:00
# To load completions for each session, execute once:
Linux:
$ sq completion bash > /etc/bash_completion.d/sq
MacOS:
$ sq completion bash > /usr/local/etc/bash_completion.d/sq
2020-08-06 20:58:47 +03:00
Zsh:
2020-08-06 20:58:47 +03:00
# If shell completion is not already enabled in your environment you will need
# to enable it. You can execute the following once:
2020-08-06 20:58:47 +03:00
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
2020-08-06 20:58:47 +03:00
# To load completions for each session, execute once:
$ sq completion zsh > "${fpath[1]}/_sq"
2020-08-06 20:58:47 +03:00
# You will need to start a new shell for this setup to take effect.
2020-08-06 20:58:47 +03:00
Fish:
2020-08-06 20:58:47 +03:00
$ sq completion fish | source
2020-08-06 20:58:47 +03:00
# To load completions for each session, execute once:
$ sq completion fish > ~/.config/fish/completions/sq.fish
2020-08-06 20:58:47 +03:00
Powershell:
PS> sq completion powershell | Out-String | Invoke-Expression
# To load completions for every new session, run:
PS> sq completion powershell > sq.ps1
# and source this file from your powershell profile.
`,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
2022-12-18 02:11:33 +03:00
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
2020-08-06 20:58:47 +03:00
}
return cmd
}
func execCompletion(cmd *cobra.Command, args []string) error {
ru := run.FromContext(cmd.Context())
switch args[0] {
case "bash":
return cmd.Root().GenBashCompletion(ru.Out)
case "zsh":
return cmd.Root().GenZshCompletion(ru.Out)
case "fish":
return cmd.Root().GenFishCompletion(ru.Out, true)
case "powershell":
return cmd.Root().GenPowerShellCompletion(ru.Out)
default:
return errz.Errorf("invalid arg: %s", args[0])
2020-08-06 20:58:47 +03:00
}
}