Files
velero/pkg/cmd/cli/completion/completion.go
JenTing Hsiao 870291141f Support fish shell completion (#3231)
* Support fish shell completion

Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com>

* Use spf13/cobra library to generate zsh completion

reference to https://github.com/spf13/cobra/blob/v1.1.1/shell_completions.md

Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com>

* Update velero completion help message

Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com>

* Add changelog

Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com>

* Update cobra version in go.mod instead of replace

Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com>

* Replace yourprogram to velero

Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com>
2021-01-25 12:45:53 -05:00

81 lines
2.0 KiB
Go

/*
Copyright 2021 the Velero contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package completion
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
func NewCommand() *cobra.Command {
c := &cobra.Command{
Use: "completion [bash|zsh|fish]",
Short: "Generate completion script",
Long: `To load completions:
Bash:
$ source <(velero completion bash)
# To load completions for each session, execute once:
Linux:
$ velero completion bash > /etc/bash_completion.d/velero
MacOS:
$ velero completion bash > /usr/local/etc/bash_completion.d/velero
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:
$ velero completion zsh > "${fpath[1]}/_velero"
# You will need to start a new shell for this setup to take effect.
Fish:
$ velero completion fish | source
# To load completions for each session, execute once:
$ velero completion fish > ~/.config/fish/completions/velero.fish
`,
Args: cobra.ExactArgs(1),
ValidArgs: []string{"bash", "zsh", "fish"},
Run: func(cmd *cobra.Command, args []string) {
shell := args[0]
switch shell {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
cmd.Root().GenFishCompletion(os.Stdout, true)
default:
fmt.Println("Invalid shell specified, specify bash, zsh, or fish")
os.Exit(1)
}
},
}
return c
}