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

Method Run

pkg/cmd/util/editor/editoptions.go:234–467  ·  view source on GitHub ↗

Run performs the execution

()

Source from the content-addressed store, hash-verified

232
233// Run performs the execution
234func (o *EditOptions) Run() error {
235 edit := NewDefaultEditor(editorEnvs())
236 // editFn is invoked for each edit session (once with a list for normal edit, once for each individual resource in a edit-on-create invocation)
237 editFn := func(infos []*resource.Info) error {
238 var (
239 results = editResults{}
240 original = []byte{}
241 edited = []byte{}
242 file string
243 err error
244 )
245
246 containsError := false
247 // loop until we succeed or cancel editing
248 for {
249 // get the object we're going to serialize as input to the editor
250 var originalObj runtime.Object
251 switch len(infos) {
252 case 1:
253 originalObj = infos[0].Object
254 default:
255 l := &unstructured.UnstructuredList{
256 Object: map[string]interface{}{
257 "kind": "List",
258 "apiVersion": "v1",
259 "metadata": map[string]interface{}{},
260 },
261 }
262 for _, info := range infos {
263 l.Items = append(l.Items, *info.Object.(*unstructured.Unstructured))
264 }
265 originalObj = l
266 }
267
268 // generate the file to edit
269 buf := &bytes.Buffer{}
270 var w io.Writer = buf
271 if o.WindowsLineEndings {
272 w = crlf.NewCRLFWriter(w)
273 }
274
275 if o.editPrinterOptions.addHeader {
276 results.header.writeTo(w, o.EditMode)
277 }
278
279 if !containsError {
280 if err := o.extractManagedFields(originalObj); err != nil {
281 return preservedFile(err, results.file, o.ErrOut)
282 }
283
284 if err := o.editPrinterOptions.PrintObj(originalObj, w); err != nil {
285 return preservedFile(err, results.file, o.ErrOut)
286 }
287 original = buf.Bytes()
288 } else {
289 // In case of an error, preserve the edited file.
290 // Remove the comments (header) from it since we already
291 // have included the latest header in the buffer above.

Callers 7

RunEditOnCreateFunction · 0.95
NewCmdEditFunction · 0.95
TestCachingVerifierFunction · 0.45
Test_processEnvFileLineFunction · 0.45

Calls 15

extractManagedFieldsMethod · 0.95
restoreManagedFieldsMethod · 0.95
visitAnnotationMethod · 0.95
visitToPatchMethod · 0.95
visitToApplyEditPatchMethod · 0.95
visitToCreateMethod · 0.95
NewCRLFWriterFunction · 0.92
GetOriginalConfigurationFunction · 0.92
NewDefaultEditorFunction · 0.85
editorEnvsFunction · 0.85
preservedFileFunction · 0.85
hasLinesFunction · 0.85

Tested by 4

TestCachingVerifierFunction · 0.36
Test_processEnvFileLineFunction · 0.36