sigs.k8s.io/kubebuilder/v3@v3.14.0/pkg/cli/completion.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package cli 18 19 import ( 20 "fmt" 21 "os" 22 23 "github.com/spf13/cobra" 24 ) 25 26 func (c CLI) newBashCmd() *cobra.Command { 27 return &cobra.Command{ 28 Use: "bash", 29 Short: "Load bash completions", 30 Example: fmt.Sprintf(`# To load completion for this session, execute: 31 $ source <(%[1]s completion bash) 32 33 # To load completions for each session, execute once: 34 Linux: 35 $ %[1]s completion bash > /etc/bash_completion.d/%[1]s 36 MacOS: 37 $ %[1]s completion bash > /usr/local/etc/bash_completion.d/%[1]s 38 `, c.commandName), 39 RunE: func(cmd *cobra.Command, cmdArgs []string) error { 40 return cmd.Root().GenBashCompletion(os.Stdout) 41 }, 42 } 43 } 44 45 func (c CLI) newZshCmd() *cobra.Command { 46 return &cobra.Command{ 47 Use: "zsh", 48 Short: "Load zsh completions", 49 Example: fmt.Sprintf(`# If shell completion is not already enabled in your environment you will need 50 # to enable it. You can execute the following once: 51 $ echo "autoload -U compinit; compinit" >> ~/.zshrc 52 53 # To load completions for each session, execute once: 54 $ %[1]s completion zsh > "${fpath[1]}/_%[1]s" 55 56 # You will need to start a new shell for this setup to take effect. 57 `, c.commandName), 58 RunE: func(cmd *cobra.Command, cmdArgs []string) error { 59 return cmd.Root().GenZshCompletion(os.Stdout) 60 }, 61 } 62 } 63 64 func (c CLI) newFishCmd() *cobra.Command { 65 return &cobra.Command{ 66 Use: "fish", 67 Short: "Load fish completions", 68 Example: fmt.Sprintf(`# To load completion for this session, execute: 69 $ %[1]s completion fish | source 70 71 # To load completions for each session, execute once: 72 $ %[1]s completion fish > ~/.config/fish/completions/%[1]s.fish 73 `, c.commandName), 74 RunE: func(cmd *cobra.Command, cmdArgs []string) error { 75 return cmd.Root().GenFishCompletion(os.Stdout, true) 76 }, 77 } 78 } 79 80 func (CLI) newPowerShellCmd() *cobra.Command { 81 return &cobra.Command{ 82 Use: "powershell", 83 Short: "Load powershell completions", 84 RunE: func(cmd *cobra.Command, cmdArgs []string) error { 85 return cmd.Root().GenPowerShellCompletion(os.Stdout) 86 }, 87 } 88 } 89 90 func (c CLI) newCompletionCmd() *cobra.Command { 91 cmd := &cobra.Command{ 92 Use: "completion", 93 Short: "Load completions for the specified shell", 94 Long: fmt.Sprintf(`Output shell completion code for the specified shell. 95 The shell code must be evaluated to provide interactive completion of %[1]s commands. 96 Detailed instructions on how to do this for each shell are provided in their own commands. 97 `, c.commandName), 98 } 99 cmd.AddCommand(c.newBashCmd()) 100 cmd.AddCommand(c.newZshCmd()) 101 cmd.AddCommand(c.newFishCmd()) 102 cmd.AddCommand(c.newPowerShellCmd()) 103 return cmd 104 }