diff --git a/README.md b/README.md index a98012b9..e03550c3 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ Download from [GitHub](https://github.com/kubernetes/kompose/releases/download/v ## Shell autocompletion -We support both Bash and Zsh autocompletion. +We support Bash, Zsh and Fish autocompletion. ```sh # Bash (add to .bashrc for persistence) @@ -68,6 +68,9 @@ source <(kompose completion bash) # Zsh (add to .zshrc for persistence) source <(kompose completion zsh) + +# Fish autocompletion +kompose completion fish | source ``` ## Development and building of Kompose diff --git a/cmd/completion.go b/cmd/completion.go index eae5b0ab..873d32be 100644 --- a/cmd/completion.go +++ b/cmd/completion.go @@ -15,10 +15,11 @@ var completion = &cobra.Command{ Short: "Output 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 zsh) +kompose completion fish | source Will load the shell completion code. `, @@ -38,24 +39,22 @@ Will load the shell completion code. func Generate(cmd *cobra.Command, args []string) error { // Check the passed in arguments 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 { - 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 - if shell == "bash" { + switch args[0] { + case "bash": return cmd.Root().GenBashCompletion(os.Stdout) - - // Generate zsh with the appropriate conversion as well as bash inclusion - } else if shell == "zsh" { + case "zsh": return runCompletionZsh(os.Stdout, cmd.Root()) - - // Else, return an error. - } else { - return fmt.Errorf("not a compatible shell, bash and zsh are only supported") + case "fish": + return runCompletionFish(os.Stdout, cmd.Root()) + default: + return fmt.Errorf("not a compatible shell, bash, zsh and fish are only supported") } } @@ -63,6 +62,22 @@ func init() { 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 https://github.com/kubernetes/kubernetes/blob/ea18d5c32ee7c320fe96dda6b0c757476908e696/pkg/kubectl/cmd/completion.go