@@ -204,11 +204,11 @@ func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
204
204
o .ExplicitNamespace = false
205
205
}
206
206
207
- isSorting , err := cmd .Flags ().GetString ("sort-by" )
207
+ sortBy , err := cmd .Flags ().GetString ("sort-by" )
208
208
if err != nil {
209
209
return err
210
210
}
211
- o .Sort = len (isSorting ) > 0
211
+ o .Sort = len (sortBy ) > 0
212
212
213
213
o .NoHeaders = cmdutil .GetFlagBool (cmd , "no-headers" )
214
214
@@ -253,12 +253,20 @@ func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
253
253
return nil , err
254
254
}
255
255
256
- printer = maybeWrapSortingPrinter (printer , isSorting )
256
+ if o .Sort {
257
+ printer = & SortingPrinter {Delegate : printer , SortField : sortBy }
258
+ }
259
+ if o .ServerPrint {
260
+ printer = & TablePrinter {Delegate : printer }
261
+ }
257
262
return printer .PrintObj , nil
258
263
}
259
264
260
265
switch {
261
266
case o .Watch || o .WatchOnly :
267
+ if o .Sort {
268
+ fmt .Fprintf (o .IOStreams .ErrOut , "warning: --watch or --watch-only requested, --sort-by will be ignored\n " )
269
+ }
262
270
default :
263
271
if len (args ) == 0 && cmdutil .IsFilenameSliceEmpty (o .Filenames , o .Kustomize ) {
264
272
fmt .Fprintf (o .ErrOut , "You must specify the type of resource to get. %s\n \n " , cmdutil .SuggestAPIResources (o .CmdParent ))
@@ -271,6 +279,12 @@ func (o *GetOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []stri
271
279
return cmdutil .UsageErrorf (cmd , usageString )
272
280
}
273
281
}
282
+
283
+ // openapi printing is mutually exclusive with server side printing
284
+ if o .PrintWithOpenAPICols && o .ServerPrint {
285
+ fmt .Fprintf (o .IOStreams .ErrOut , "warning: --%s requested, --%s will be ignored\n " , useOpenAPIPrintColumnFlagLabel , useServerPrintColumns )
286
+ }
287
+
274
288
return nil
275
289
}
276
290
@@ -398,6 +412,27 @@ func NewRuntimeSorter(objects []runtime.Object, sortBy string) *RuntimeSorter {
398
412
}
399
413
}
400
414
415
+ func (o * GetOptions ) transformRequests (req * rest.Request ) {
416
+ // We need full objects if printing with openapi columns
417
+ if o .PrintWithOpenAPICols {
418
+ return
419
+ }
420
+ if ! o .ServerPrint || ! o .IsHumanReadablePrinter {
421
+ return
422
+ }
423
+
424
+ group := metav1beta1 .GroupName
425
+ version := metav1beta1 .SchemeGroupVersion .Version
426
+
427
+ tableParam := fmt .Sprintf ("application/json;as=Table;v=%s;g=%s, application/json" , version , group )
428
+ req .SetHeader ("Accept" , tableParam )
429
+
430
+ // if sorting, ensure we receive the full object in order to introspect its fields via jsonpath
431
+ if o .Sort {
432
+ req .Param ("includeObject" , "Object" )
433
+ }
434
+ }
435
+
401
436
// Run performs the get operation.
402
437
// TODO: remove the need to pass these arguments, like other commands.
403
438
func (o * GetOptions ) Run (f cmdutil.Factory , cmd * cobra.Command , args []string ) error {
@@ -408,11 +443,6 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
408
443
return o .watch (f , cmd , args )
409
444
}
410
445
411
- // openapi printing is mutually exclusive with server side printing
412
- if o .PrintWithOpenAPICols && o .ServerPrint {
413
- fmt .Fprintf (o .IOStreams .ErrOut , "warning: --%s requested, --%s will be ignored\n " , useOpenAPIPrintColumnFlagLabel , useServerPrintColumns )
414
- }
415
-
416
446
chunkSize := o .ChunkSize
417
447
if o .Sort {
418
448
// TODO(juanvallejo): in the future, we could have the client use chunking
@@ -432,26 +462,7 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
432
462
ContinueOnError ().
433
463
Latest ().
434
464
Flatten ().
435
- TransformRequests (func (req * rest.Request ) {
436
- // We need full objects if printing with openapi columns
437
- if o .PrintWithOpenAPICols {
438
- return
439
- }
440
- if ! o .ServerPrint || ! o .IsHumanReadablePrinter {
441
- return
442
- }
443
-
444
- group := metav1beta1 .GroupName
445
- version := metav1beta1 .SchemeGroupVersion .Version
446
-
447
- tableParam := fmt .Sprintf ("application/json;as=Table;v=%s;g=%s, application/json" , version , group )
448
- req .SetHeader ("Accept" , tableParam )
449
-
450
- // if sorting, ensure we receive the full object in order to introspect its fields via jsonpath
451
- if o .Sort {
452
- req .Param ("includeObject" , "Object" )
453
- }
454
- }).
465
+ TransformRequests (o .transformRequests ).
455
466
Do ()
456
467
457
468
if o .IgnoreNotFound {
@@ -475,17 +486,13 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
475
486
476
487
objs := make ([]runtime.Object , len (infos ))
477
488
for ix := range infos {
489
+ // TODO: remove this and just pass the table objects to the printer opaquely once `info.Object.(*metav1beta1.Table)` checking is removed below
478
490
if o .ServerPrint {
479
- table , err := o . decodeIntoTable (infos [ix ].Object )
491
+ table , err := decodeIntoTable (infos [ix ].Object )
480
492
if err == nil {
481
493
infos [ix ].Object = table
482
- } else {
483
- // if we are unable to decode server response into a v1beta1.Table,
484
- // fallback to client-side printing with whatever info the server returned.
485
- klog .V (2 ).Infof ("Unable to decode server response into a Table. Falling back to hardcoded types: %v" , err )
486
494
}
487
495
}
488
-
489
496
objs [ix ] = infos [ix ].Object
490
497
}
491
498
@@ -723,35 +730,6 @@ func attemptToConvertToInternal(obj runtime.Object, converter runtime.ObjectConv
723
730
return internalObject
724
731
}
725
732
726
- func (o * GetOptions ) decodeIntoTable (obj runtime.Object ) (runtime.Object , error ) {
727
- if obj .GetObjectKind ().GroupVersionKind ().Kind != "Table" {
728
- return nil , fmt .Errorf ("attempt to decode non-Table object into a v1beta1.Table" )
729
- }
730
-
731
- unstr , ok := obj .(* unstructured.Unstructured )
732
- if ! ok {
733
- return nil , fmt .Errorf ("attempt to decode non-Unstructured object" )
734
- }
735
- table := & metav1beta1.Table {}
736
- if err := runtime .DefaultUnstructuredConverter .FromUnstructured (unstr .Object , table ); err != nil {
737
- return nil , err
738
- }
739
-
740
- for i := range table .Rows {
741
- row := & table .Rows [i ]
742
- if row .Object .Raw == nil || row .Object .Object != nil {
743
- continue
744
- }
745
- converted , err := runtime .Decode (unstructured .UnstructuredJSONScheme , row .Object .Raw )
746
- if err != nil {
747
- return nil , err
748
- }
749
- row .Object .Object = converted
750
- }
751
-
752
- return table , nil
753
- }
754
-
755
733
func (o * GetOptions ) printGeneric (r * resource.Result ) error {
756
734
// we flattened the data from the builder, so we have individual items, but now we'd like to either:
757
735
// 1. if there is more than one item, combine them all into a single list
@@ -863,16 +841,6 @@ func cmdSpecifiesOutputFmt(cmd *cobra.Command) bool {
863
841
return cmdutil .GetFlagString (cmd , "output" ) != ""
864
842
}
865
843
866
- func maybeWrapSortingPrinter (printer printers.ResourcePrinter , sortBy string ) printers.ResourcePrinter {
867
- if len (sortBy ) != 0 {
868
- return & SortingPrinter {
869
- Delegate : printer ,
870
- SortField : fmt .Sprintf ("%s" , sortBy ),
871
- }
872
- }
873
- return printer
874
- }
875
-
876
844
func multipleGVKsRequested (infos []* resource.Info ) bool {
877
845
if len (infos ) < 2 {
878
846
return false
0 commit comments