|
| 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 | +/* TODO: support fish code completion |
| 65 | + At the time this comment is written, the imported spf13.cobra version does not support fish completion. |
| 66 | + However, fish completion has been added to new spf13.cobra versions. When a new spf13.cobra version that |
| 67 | + supports it is used, uncomment this command and add it to the base completion command. |
| 68 | +func (c *cli) newFishCmd() *cobra.Command { |
| 69 | + return &cobra.Command{ |
| 70 | + Use: "fish", |
| 71 | + Short: "Load fish completions", |
| 72 | + Example: fmt.Sprintf(`# To load completion for this session, execute: |
| 73 | +$ %[1]s completion fish | source |
| 74 | +
|
| 75 | +# To load completions for each session, execute once: |
| 76 | +$ %[1]s completion fish > ~/.config/fish/completions/%[1]s.fish`, c.commandName), |
| 77 | + RunE: func(cmd *cobra.Command, cmdArgs []string) error { |
| 78 | + return cmd.Root().GenFishCompletion(os.Stdout) |
| 79 | + }, |
| 80 | + } |
| 81 | +} |
| 82 | +*/ |
| 83 | + |
| 84 | +func (c *cli) newPowerShellCmd() *cobra.Command { |
| 85 | + return &cobra.Command{ |
| 86 | + Use: "powershell", |
| 87 | + Short: "Load powershell completions", |
| 88 | + RunE: func(cmd *cobra.Command, cmdArgs []string) error { |
| 89 | + return cmd.Root().GenPowerShellCompletion(os.Stdout) |
| 90 | + }, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func (c *cli) newCompletionCmd() *cobra.Command { |
| 95 | + cmd := &cobra.Command{ |
| 96 | + Use: "completion", |
| 97 | + Short: "Load completions for the specified shell", |
| 98 | + Long: fmt.Sprintf(`Output shell completion code for the specified shell. |
| 99 | +The shell code must be evaluated to provide interactive completion of %[1]s commands. |
| 100 | +Detailed instructions on how to do this for each shell are provided in their own commands. |
| 101 | +`, c.commandName), |
| 102 | + } |
| 103 | + cmd.AddCommand(c.newBashCmd()) |
| 104 | + cmd.AddCommand(c.newZshCmd()) |
| 105 | + // cmd.AddCommand(c.newFishCmd()) // TODO: uncomment when adding fish completion |
| 106 | + cmd.AddCommand(c.newPowerShellCmd()) |
| 107 | + return cmd |
| 108 | +} |
0 commit comments