This repository has been archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add completion for bash, zsh, fish, powershell
- Loading branch information
1 parent
033226c
commit f0f40ca
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/mainawycliffe/kamanda/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// completionCmd represents the completion command | ||
var completionCmd = &cobra.Command{ | ||
Use: "completion [bash|zsh|fish|powershell]", | ||
Short: "Generate completion script", | ||
Long: `To load completions: | ||
Bash: | ||
$ source <(yourprogram completion bash) | ||
# To load completions for each session, execute once: | ||
Linux: | ||
$ yourprogram completion bash > /etc/bash_completion.d/yourprogram | ||
MacOS: | ||
$ yourprogram completion bash > /usr/local/etc/bash_completion.d/yourprogram | ||
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: | ||
$ yourprogram completion zsh > "${fpath[1]}/_yourprogram" | ||
# You will need to start a new shell for this setup to take effect. | ||
Fish: | ||
$ yourprogram completion fish | source | ||
# To load completions for each session, execute once: | ||
$ yourprogram completion fish > ~/.config/fish/completions/yourprogram.fish | ||
`, | ||
DisableFlagsInUseLine: true, | ||
ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, | ||
Args: cobra.ExactValidArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
var err error | ||
switch args[0] { | ||
case "bash": | ||
err = cmd.Root().GenBashCompletion(os.Stdout) | ||
case "zsh": | ||
err = cmd.Root().GenZshCompletion(os.Stdout) | ||
case "fish": | ||
err = cmd.Root().GenFishCompletion(os.Stdout, true) | ||
case "powershell": | ||
err = cmd.Root().GenPowerShellCompletion(os.Stdout) | ||
} | ||
if err != nil { | ||
utils.StdOutError(os.Stderr, "Error while generating %s autocompletion: %w\n", args[0], err) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(completionCmd) | ||
} |