|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes Authors All rights reserved. |
| 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 options |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + |
| 22 | + "github.com/spf13/cobra" |
| 23 | + "k8s.io/klog/v2" |
| 24 | +) |
| 25 | + |
| 26 | +const autoloadZsh = `Generate the autocompletion script for the zsh shell. |
| 27 | +
|
| 28 | +If shell completion is not already enabled in your environment you will need |
| 29 | +to enable it. You can execute the following once: |
| 30 | +
|
| 31 | + echo "autoload -U compinit; compinit" >> ~/.zshrc |
| 32 | +
|
| 33 | +To load completions in your current shell session: |
| 34 | +
|
| 35 | + source <(kube-state-metrics completion zsh); compdef _kube-state-metrics kube-state-metrics |
| 36 | +
|
| 37 | +To load completions for every new session, execute once: |
| 38 | +
|
| 39 | +#### Linux: |
| 40 | +
|
| 41 | + kube-state-metrics completion zsh > "${fpath[1]}/_kube-state-metrics" |
| 42 | +
|
| 43 | +#### macOS: |
| 44 | +
|
| 45 | + kube-state-metrics completion zsh > $(brew --prefix)/share/zsh/site-functions/_kube-state-metrics |
| 46 | +
|
| 47 | +You will need to start a new shell for this setup to take effect. |
| 48 | +
|
| 49 | +Usage: |
| 50 | + kube-state-metrics completion zsh [flags] |
| 51 | +
|
| 52 | +Flags: |
| 53 | + --no-descriptions disable completion descriptions |
| 54 | +` |
| 55 | +const autoloadBash = `Generate the autocompletion script for the bash shell. |
| 56 | +
|
| 57 | +This script depends on the 'bash-completion' package. |
| 58 | +If it is not installed already, you can install it via your OS's package manager. |
| 59 | +
|
| 60 | +To load completions in your current shell session: |
| 61 | +
|
| 62 | + source <(kube-state-metrics completion bash) |
| 63 | +
|
| 64 | +To load completions for every new session, execute once: |
| 65 | +
|
| 66 | +#### Linux: |
| 67 | +
|
| 68 | + kube-state-metrics completion bash > /etc/bash_completion.d/kube-state-metrics |
| 69 | +
|
| 70 | +#### macOS: |
| 71 | +
|
| 72 | + kube-state-metrics completion bash > $(brew --prefix)/etc/bash_completion.d/kube-state-metrics |
| 73 | +
|
| 74 | +You will need to start a new shell for this setup to take effect. |
| 75 | +
|
| 76 | +Usage: |
| 77 | + kube-state-metrics completion bash |
| 78 | +
|
| 79 | +Flags: |
| 80 | + --no-descriptions disable completion descriptions |
| 81 | +` |
| 82 | + |
| 83 | +const autoloadFish = `Generate the autocompletion script for the fish shell. |
| 84 | +
|
| 85 | +To load completions in your current shell session: |
| 86 | +
|
| 87 | + kube-state-metrics completion fish | source |
| 88 | +
|
| 89 | +To load completions for every new session, execute once: |
| 90 | +
|
| 91 | + kube-state-metrics completion fish > ~/.config/fish/completions/kube-state-metrics.fish |
| 92 | +
|
| 93 | +You will need to start a new shell for this setup to take effect. |
| 94 | +
|
| 95 | +Usage: |
| 96 | + kube-state-metrics completion fish [flags] |
| 97 | +
|
| 98 | +Flags: |
| 99 | + --no-descriptions disable completion descriptions |
| 100 | +` |
| 101 | + |
| 102 | +// FetchLoadInstructions returns instructions for enabling autocompletion for a particular shell. |
| 103 | +func FetchLoadInstructions(shell string) string { |
| 104 | + switch shell { |
| 105 | + case "zsh": |
| 106 | + return autoloadZsh |
| 107 | + case "bash": |
| 108 | + return autoloadBash |
| 109 | + case "fish": |
| 110 | + return autoloadFish |
| 111 | + default: |
| 112 | + return "" |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +var completionCommand = &cobra.Command{ |
| 117 | + Use: "completion [bash|zsh|fish]", |
| 118 | + Short: "Generate completion script for kube-state-metrics.", |
| 119 | + DisableFlagsInUseLine: true, |
| 120 | + Aliases: []string{"comp", "c"}, |
| 121 | + ValidArgs: []string{"bash", "zsh", "fish"}, |
| 122 | + Args: cobra.ExactArgs(1), |
| 123 | + Run: func(cmd *cobra.Command, args []string) { |
| 124 | + switch args[0] { |
| 125 | + case "bash": |
| 126 | + _ = cmd.Root().GenBashCompletion(os.Stdout) |
| 127 | + case "zsh": |
| 128 | + _ = cmd.Root().GenZshCompletion(os.Stdout) |
| 129 | + case "fish": |
| 130 | + _ = cmd.Root().GenFishCompletion(os.Stdout, true) |
| 131 | + } |
| 132 | + klog.FlushAndExit(klog.ExitFlushTimeout, 0) |
| 133 | + }, |
| 134 | + Example: "kube-state-metrics completion bash > /tmp/kube-state-metrics.bash && source /tmp/kube-state-metrics.bash # for shells compatible with bash", |
| 135 | +} |
| 136 | + |
| 137 | +// InitCommand defines the root command that others will latch onto. |
| 138 | +var InitCommand = &cobra.Command{ |
| 139 | + Use: "kube-state-metrics", |
| 140 | + Short: "Add-on agent to generate and expose cluster-level metrics.", |
| 141 | + Long: "kube-state-metrics is a simple service that listens to the Kubernetes API server and generates metrics about the state of the objects.", |
| 142 | + Args: cobra.NoArgs, |
| 143 | +} |
0 commit comments