...

Source file src/k8s.io/cli-runtime/pkg/printers/typesetter.go

Documentation: k8s.io/cli-runtime/pkg/printers

     1  /*
     2  Copyright 2018 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 printers
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  	"k8s.io/apimachinery/pkg/runtime/schema"
    25  )
    26  
    27  // TypeSetterPrinter is an implementation of ResourcePrinter wraps another printer with types set on the objects
    28  type TypeSetterPrinter struct {
    29  	Delegate ResourcePrinter
    30  
    31  	Typer runtime.ObjectTyper
    32  }
    33  
    34  // NewTypeSetter constructs a wrapping printer with required params
    35  func NewTypeSetter(typer runtime.ObjectTyper) *TypeSetterPrinter {
    36  	return &TypeSetterPrinter{Typer: typer}
    37  }
    38  
    39  // PrintObj is an implementation of ResourcePrinter.PrintObj which sets type information on the obj for the duration
    40  // of printing.  It is NOT threadsafe.
    41  func (p *TypeSetterPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
    42  	if obj == nil {
    43  		return p.Delegate.PrintObj(obj, w)
    44  	}
    45  	if !obj.GetObjectKind().GroupVersionKind().Empty() {
    46  		return p.Delegate.PrintObj(obj, w)
    47  	}
    48  
    49  	// we were empty coming in, make sure we're empty going out.  This makes the call thread-unsafe
    50  	defer func() {
    51  		obj.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{})
    52  	}()
    53  
    54  	gvks, _, err := p.Typer.ObjectKinds(obj)
    55  	if err != nil {
    56  		// printers wrapped by us expect to find the type information present
    57  		return fmt.Errorf("missing apiVersion or kind and cannot assign it; %v", err)
    58  	}
    59  
    60  	for _, gvk := range gvks {
    61  		if len(gvk.Kind) == 0 {
    62  			continue
    63  		}
    64  		if len(gvk.Version) == 0 || gvk.Version == runtime.APIVersionInternal {
    65  			continue
    66  		}
    67  		obj.GetObjectKind().SetGroupVersionKind(gvk)
    68  		break
    69  	}
    70  
    71  	return p.Delegate.PrintObj(obj, w)
    72  }
    73  
    74  // ToPrinter returns a printer (not threadsafe!) that has been wrapped
    75  func (p *TypeSetterPrinter) ToPrinter(delegate ResourcePrinter) ResourcePrinter {
    76  	if p == nil {
    77  		return delegate
    78  	}
    79  
    80  	p.Delegate = delegate
    81  	return p
    82  }
    83  
    84  // WrapToPrinter wraps the common ToPrinter method
    85  func (p *TypeSetterPrinter) WrapToPrinter(delegate ResourcePrinter, err error) (ResourcePrinter, error) {
    86  	if err != nil {
    87  		return delegate, err
    88  	}
    89  	if p == nil {
    90  		return delegate, nil
    91  	}
    92  
    93  	p.Delegate = delegate
    94  	return p, nil
    95  }
    96  

View as plain text