NewKubectlCommand creates the `kubectl` command and its nested children.
(o KubectlOptions)
| 163 | |
| 164 | // NewKubectlCommand creates the `kubectl` command and its nested children. |
| 165 | func NewKubectlCommand(o KubectlOptions) *cobra.Command { |
| 166 | warningHandler := rest.NewWarningWriter(o.IOStreams.ErrOut, rest.WarningWriterOptions{Deduplicate: true, Color: printers.AllowsColorOutput(o.IOStreams.ErrOut)}) |
| 167 | warningsAsErrors := false |
| 168 | var finishProfiling func() error |
| 169 | // Parent command to which all subcommands are added. |
| 170 | cmds := &cobra.Command{ |
| 171 | Use: "kubectl", |
| 172 | Short: i18n.T("kubectl controls the Kubernetes cluster manager"), |
| 173 | Long: templates.LongDesc(` |
| 174 | kubectl controls the Kubernetes cluster manager. |
| 175 | |
| 176 | Find more information at: |
| 177 | https://kubernetes.io/docs/reference/kubectl/`), |
| 178 | Run: runHelp, |
| 179 | // Hook before and after Run initialize and write profiles to disk, |
| 180 | // respectively. |
| 181 | PersistentPreRunE: func(cmd *cobra.Command, args []string) error { |
| 182 | rest.SetDefaultWarningHandler(warningHandler) |
| 183 | |
| 184 | if cmd.Name() == cobra.ShellCompRequestCmd { |
| 185 | // This is the __complete or __completeNoDesc command which |
| 186 | // indicates shell completion has been requested. |
| 187 | plugin.SetupPluginCompletion(cmd, args) |
| 188 | } |
| 189 | |
| 190 | var err error |
| 191 | finishProfiling, err = initProfiling() |
| 192 | return err |
| 193 | }, |
| 194 | PersistentPostRunE: func(*cobra.Command, []string) error { |
| 195 | if finishProfiling != nil { |
| 196 | if err := finishProfiling(); err != nil { |
| 197 | return err |
| 198 | } |
| 199 | } |
| 200 | if warningsAsErrors { |
| 201 | count := warningHandler.WarningCount() |
| 202 | switch count { |
| 203 | case 0: |
| 204 | // no warnings |
| 205 | case 1: |
| 206 | return fmt.Errorf("%d warning received", count) |
| 207 | default: |
| 208 | return fmt.Errorf("%d warnings received", count) |
| 209 | } |
| 210 | } |
| 211 | return nil |
| 212 | }, |
| 213 | } |
| 214 | // From this point and forward we get warnings on flags that contain "_" separators |
| 215 | // when adding them with hyphen instead of the original name. |
| 216 | cmds.SetGlobalNormalizationFunc(cliflag.WarnWordSepNormalizeFunc) |
| 217 | |
| 218 | flags := cmds.PersistentFlags() |
| 219 | |
| 220 | addProfilingFlags(flags) |
| 221 | |
| 222 | flags.BoolVar(&warningsAsErrors, "warnings-as-errors", warningsAsErrors, "Treat warnings received from the server as errors and exit with a non-zero exit code") |
searching dependent graphs…