1 /* 2 Copyright 2019 The Kubernetes Authors. 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 get 18 19 import ( 20 "io" 21 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "k8s.io/apimachinery/pkg/runtime" 24 "k8s.io/cli-runtime/pkg/printers" 25 ) 26 27 // skipPrinter allows conditionally suppressing object output via the output field. 28 // table objects are suppressed by setting their Rows to nil (allowing column definitions to propagate to the delegate). 29 // non-table objects are suppressed by not calling the delegate at all. 30 type skipPrinter struct { 31 delegate printers.ResourcePrinter 32 output *bool 33 } 34 35 func (p *skipPrinter) PrintObj(obj runtime.Object, writer io.Writer) error { 36 if *p.output { 37 return p.delegate.PrintObj(obj, writer) 38 } 39 40 table, isTable := obj.(*metav1.Table) 41 if !isTable { 42 return nil 43 } 44 45 table = table.DeepCopy() 46 table.Rows = nil 47 return p.delegate.PrintObj(table, writer) 48 } 49