MCPcopy
hub / github.com/kubernetes/kubectl / NewKubectlCommand

Function NewKubectlCommand

pkg/cmd/cmd.go:165–383  ·  view source on GitHub ↗

NewKubectlCommand creates the `kubectl` command and its nested children.

(o KubectlOptions)

Source from the content-addressed store, hash-verified

163
164// NewKubectlCommand creates the `kubectl` command and its nested children.
165func 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")

Calls 15

AddFlagsMethod · 0.95
AddFlagsMethod · 0.95
AddMethod · 0.95
ApplyMethod · 0.95
TFunction · 0.92
LongDescFunction · 0.92
SetupPluginCompletionFunction · 0.92
NewPreferencesFunction · 0.92
NewCmdProxyFunction · 0.92
NewCmdGetFunction · 0.92
NewCmdDebugFunction · 0.92
NewCmdCreateFunction · 0.92

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…