mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-09-04 07:07:11 +00:00
Add bash and zsh completion support
This commit adds support for auto completion for bash and zsh shells. A new root level command called "completion" has been introduced, and the user can get the auto completion code by running `ark completion bash/zsh`. For bash completion, the built-in GenBashCompletion() from cobra has been used, but for zsh, the built-in GenZshCompletion() is known to cause issues. The workaround has been copied from zsh completion code of kubectl. Signed-off-by: Shubham <shubham@linux.com>
This commit is contained in:
committed by
Shubham
parent
a48cc6ed23
commit
7f3e88151b
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
Copyright 2018 the Heptio Ark 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"
|
||||
|
||||
kubectlcmd "github.com/heptio/ark/third_party/kubernetes/pkg/kubectl/cmd"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func NewCommand() *cobra.Command {
|
||||
c := &cobra.Command{
|
||||
Use: "completion SHELL",
|
||||
Short: "Output shell completion code for the specified shell (bash or zsh)",
|
||||
Long: `Generate shell completion code.
|
||||
|
||||
Auto completion supports both bash and zsh. Output is to STDOUT.
|
||||
|
||||
Load the ark completion code for bash into the current shell -
|
||||
source <(ark completion bash)
|
||||
|
||||
Load the ark completion code for zsh into the current shell -
|
||||
source <(ark completion zsh)
|
||||
`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
ValidArgs: []string{"bash", "zsh"},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
shell := args[0]
|
||||
switch shell {
|
||||
case "bash":
|
||||
cmd.Root().GenBashCompletion(os.Stdout)
|
||||
case "zsh":
|
||||
kubectlcmd.GenZshCompletion(os.Stdout, cmd.Root())
|
||||
default:
|
||||
fmt.Printf("Invalid shell specified, specify bash or zsh\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
Reference in New Issue
Block a user