...

Source file src/k8s.io/kubectl/pkg/explain/fields_printer.go

Documentation: k8s.io/kubectl/pkg/explain

     1  /*
     2  Copyright 2017 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 explain
    18  
    19  import "k8s.io/kube-openapi/pkg/util/proto"
    20  
    21  // indentDesc is the level of indentation for descriptions.
    22  const indentDesc = 2
    23  
    24  // regularFieldsPrinter prints fields with their type and description.
    25  type regularFieldsPrinter struct {
    26  	Writer *Formatter
    27  	Error  error
    28  }
    29  
    30  var _ proto.SchemaVisitor = &regularFieldsPrinter{}
    31  var _ fieldsPrinter = &regularFieldsPrinter{}
    32  
    33  // VisitArray prints a Array type. It is just a passthrough.
    34  func (f *regularFieldsPrinter) VisitArray(a *proto.Array) {
    35  	a.SubType.Accept(f)
    36  }
    37  
    38  // VisitKind prints a Kind type. It prints each key in the kind, with
    39  // the type, the required flag, and the description.
    40  func (f *regularFieldsPrinter) VisitKind(k *proto.Kind) {
    41  	for _, key := range k.Keys() {
    42  		v := k.Fields[key]
    43  		required := ""
    44  		if k.IsRequired(key) {
    45  			required = " -required-"
    46  		}
    47  
    48  		if err := f.Writer.Write("%s\t<%s>%s", key, GetTypeName(v), required); err != nil {
    49  			f.Error = err
    50  			return
    51  		}
    52  		if err := f.Writer.Indent(indentDesc).WriteWrapped("%s", v.GetDescription()); err != nil {
    53  			f.Error = err
    54  			return
    55  		}
    56  		if err := f.Writer.Write(""); err != nil {
    57  			f.Error = err
    58  			return
    59  		}
    60  	}
    61  }
    62  
    63  // VisitMap prints a Map type. It is just a passthrough.
    64  func (f *regularFieldsPrinter) VisitMap(m *proto.Map) {
    65  	m.SubType.Accept(f)
    66  }
    67  
    68  // VisitPrimitive prints a Primitive type. It stops the recursion.
    69  func (f *regularFieldsPrinter) VisitPrimitive(p *proto.Primitive) {
    70  	// Nothing to do. Shouldn't really happen.
    71  }
    72  
    73  // VisitReference prints a Reference type. It is just a passthrough.
    74  func (f *regularFieldsPrinter) VisitReference(r proto.Reference) {
    75  	r.SubSchema().Accept(f)
    76  }
    77  
    78  // PrintFields will write the types from schema.
    79  func (f *regularFieldsPrinter) PrintFields(schema proto.Schema) error {
    80  	schema.Accept(f)
    81  	return f.Error
    82  }
    83  

View as plain text