...

Source file src/k8s.io/kubectl/pkg/explain/v2/generator.go

Documentation: k8s.io/kubectl/pkg/explain/v2

     1  /*
     2  Copyright 2022 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 v2
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"text/template"
    23  
    24  	"k8s.io/apimachinery/pkg/runtime/schema"
    25  )
    26  
    27  type Generator interface {
    28  	AddTemplate(name string, contents string) error
    29  
    30  	Render(
    31  		// Template to use for rendering
    32  		templateName string,
    33  		// Self-Contained OpenAPI Document Containing all schemas used by $ref
    34  		// Only OpenAPI V3 documents are supported
    35  		document map[string]interface{},
    36  		// Resource within OpenAPI document for which to render explain schema
    37  		gvr schema.GroupVersionResource,
    38  		// Field path of child of resource to focus output onto
    39  		fieldSelector []string,
    40  		// Boolean indicating whether the fields should be rendered recursively/deeply
    41  		recursive bool,
    42  		// Output writer
    43  		writer io.Writer,
    44  	) error
    45  }
    46  
    47  type TemplateContext struct {
    48  	GVR       schema.GroupVersionResource
    49  	Document  map[string]interface{}
    50  	Recursive bool
    51  	FieldPath []string
    52  }
    53  
    54  type generator struct {
    55  	templates map[string]*template.Template
    56  }
    57  
    58  func NewGenerator() Generator {
    59  	return &generator{
    60  		templates: make(map[string]*template.Template),
    61  	}
    62  }
    63  
    64  func (g *generator) AddTemplate(name string, contents string) error {
    65  	compiled, err := WithBuiltinTemplateFuncs(template.New(name)).Parse(contents)
    66  
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	g.templates[name] = compiled
    72  	return nil
    73  }
    74  
    75  func (g *generator) Render(
    76  	// Template to use for rendering
    77  	templateName string,
    78  	// Self-Contained OpenAPI Document Containing all schemas used by $ref
    79  	// Only OpenAPI V3 documents are supported
    80  	document map[string]interface{},
    81  	// Resource within OpenAPI document for which to render explain schema
    82  	gvr schema.GroupVersionResource,
    83  	// Field path of child of resource to focus output onto
    84  	fieldSelector []string,
    85  	// Boolean indicating whether the fields should be rendered recursively/deeply
    86  	recursive bool,
    87  	// Output writer
    88  	writer io.Writer,
    89  ) error {
    90  	compiledTemplate, ok := g.templates[templateName]
    91  	if !ok {
    92  		return fmt.Errorf("unrecognized format: %s", templateName)
    93  	}
    94  
    95  	err := compiledTemplate.Execute(writer, TemplateContext{
    96  		Document:  document,
    97  		Recursive: recursive,
    98  		FieldPath: fieldSelector,
    99  		GVR:       gvr,
   100  	})
   101  	return err
   102  }
   103  

View as plain text