Add autocompletion for fish shell (#1412)

This commit is contained in:
Top 2021-08-11 01:01:48 -05:00 committed by GitHub
parent ef474809e3
commit abbe054e1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 13 deletions

View File

@ -60,7 +60,7 @@ Download from [GitHub](https://github.com/kubernetes/kompose/releases/download/v
## Shell autocompletion ## Shell autocompletion
We support both Bash and Zsh autocompletion. We support Bash, Zsh and Fish autocompletion.
```sh ```sh
# Bash (add to .bashrc for persistence) # Bash (add to .bashrc for persistence)
@ -68,6 +68,9 @@ source <(kompose completion bash)
# Zsh (add to .zshrc for persistence) # Zsh (add to .zshrc for persistence)
source <(kompose completion zsh) source <(kompose completion zsh)
# Fish autocompletion
kompose completion fish | source
``` ```
## Development and building of Kompose ## Development and building of Kompose

View File

@ -15,10 +15,11 @@ var completion = &cobra.Command{
Short: "Output shell completion code", Short: "Output shell completion code",
Long: `Generates shell completion code. Long: `Generates shell completion code.
Auto completion supports both bash and zsh. Output is to STDOUT. Auto completion supports bash, zsh and fish. Output is to STDOUT.
source <(kompose completion bash) source <(kompose completion bash)
source <(kompose completion zsh) source <(kompose completion zsh)
kompose completion fish | source
Will load the shell completion code. Will load the shell completion code.
`, `,
@ -38,24 +39,22 @@ Will load the shell completion code.
func Generate(cmd *cobra.Command, args []string) error { func Generate(cmd *cobra.Command, args []string) error {
// Check the passed in arguments // Check the passed in arguments
if len(args) == 0 { if len(args) == 0 {
return fmt.Errorf("shell not specified. ex. kompose completion [bash|zsh]") return fmt.Errorf("shell not specified. ex. kompose completion [bash|zsh|fish]")
} }
if len(args) > 1 { if len(args) > 1 {
return fmt.Errorf("too many arguments. Expected only the shell type. ex. kompose completion [bash|zsh]") return fmt.Errorf("too many arguments. Expected only the shell type. ex. kompose completion [bash|zsh|fish]")
} }
shell := args[0]
// Generate bash through cobra if selected // Generate bash through cobra if selected
if shell == "bash" { switch args[0] {
case "bash":
return cmd.Root().GenBashCompletion(os.Stdout) return cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
// Generate zsh with the appropriate conversion as well as bash inclusion
} else if shell == "zsh" {
return runCompletionZsh(os.Stdout, cmd.Root()) return runCompletionZsh(os.Stdout, cmd.Root())
case "fish":
// Else, return an error. return runCompletionFish(os.Stdout, cmd.Root())
} else { default:
return fmt.Errorf("not a compatible shell, bash and zsh are only supported") return fmt.Errorf("not a compatible shell, bash, zsh and fish are only supported")
} }
} }
@ -63,6 +62,22 @@ func init() {
RootCmd.AddCommand(completion) RootCmd.AddCommand(completion)
} }
/*
Fish shell auto-completion support
*/
func runCompletionFish(out io.Writer, kompose *cobra.Command) error {
kompose.GenFishCompletion(out, true)
fishInitialization := `
set -l commands "completion convert help version"
complete -c kompose -f
complete -c kompose -n "not __fish_seen_subcommand_from $commands" -a $commands
complete -c kompose -n "__fish_seen_subcommand_from completion" -a "bash zsh fish"
`
out.Write([]byte(fishInitialization))
return nil
}
/* /*
This is copied from This is copied from
https://github.com/kubernetes/kubernetes/blob/ea18d5c32ee7c320fe96dda6b0c757476908e696/pkg/kubectl/cmd/completion.go https://github.com/kubernetes/kubernetes/blob/ea18d5c32ee7c320fe96dda6b0c757476908e696/pkg/kubectl/cmd/completion.go