...

Source file src/k8s.io/cli-runtime/pkg/genericclioptions/kube_template_flags.go

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

     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 genericclioptions
    18  
    19  import (
    20  	"github.com/spf13/cobra"
    21  
    22  	"k8s.io/cli-runtime/pkg/printers"
    23  )
    24  
    25  // KubeTemplatePrintFlags composes print flags that provide both a JSONPath and a go-template printer.
    26  // This is necessary if dealing with cases that require support both both printers, since both sets of flags
    27  // require overlapping flags.
    28  type KubeTemplatePrintFlags struct {
    29  	GoTemplatePrintFlags *GoTemplatePrintFlags
    30  	JSONPathPrintFlags   *JSONPathPrintFlags
    31  
    32  	AllowMissingKeys *bool
    33  	TemplateArgument *string
    34  }
    35  
    36  // AllowedFormats returns slice of string of allowed GoTemplete and JSONPathPrint printing formats
    37  func (f *KubeTemplatePrintFlags) AllowedFormats() []string {
    38  	if f == nil {
    39  		return []string{}
    40  	}
    41  	return append(f.GoTemplatePrintFlags.AllowedFormats(), f.JSONPathPrintFlags.AllowedFormats()...)
    42  }
    43  
    44  // ToPrinter receives an outputFormat and returns a printer capable of
    45  // handling --template printing.
    46  // Returns false if the specified outputFormat does not match a supported format.
    47  // Supported Format types can be found in pkg/printers/printers.go
    48  func (f *KubeTemplatePrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error) {
    49  	if f == nil {
    50  		return nil, NoCompatiblePrinterError{}
    51  	}
    52  
    53  	if p, err := f.JSONPathPrintFlags.ToPrinter(outputFormat); !IsNoCompatiblePrinterError(err) {
    54  		return p, err
    55  	}
    56  	return f.GoTemplatePrintFlags.ToPrinter(outputFormat)
    57  }
    58  
    59  // AddFlags receives a *cobra.Command reference and binds
    60  // flags related to template printing to it
    61  func (f *KubeTemplatePrintFlags) AddFlags(c *cobra.Command) {
    62  	if f == nil {
    63  		return
    64  	}
    65  
    66  	if f.TemplateArgument != nil {
    67  		c.Flags().StringVar(f.TemplateArgument, "template", *f.TemplateArgument, "Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].")
    68  		c.MarkFlagFilename("template")
    69  	}
    70  	if f.AllowMissingKeys != nil {
    71  		c.Flags().BoolVar(f.AllowMissingKeys, "allow-missing-template-keys", *f.AllowMissingKeys, "If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats.")
    72  	}
    73  }
    74  
    75  // NewKubeTemplatePrintFlags returns flags associated with
    76  // --template printing, with default values set.
    77  func NewKubeTemplatePrintFlags() *KubeTemplatePrintFlags {
    78  	allowMissingKeysPtr := true
    79  	templateArgPtr := ""
    80  
    81  	return &KubeTemplatePrintFlags{
    82  		GoTemplatePrintFlags: &GoTemplatePrintFlags{
    83  			TemplateArgument: &templateArgPtr,
    84  			AllowMissingKeys: &allowMissingKeysPtr,
    85  		},
    86  		JSONPathPrintFlags: &JSONPathPrintFlags{
    87  			TemplateArgument: &templateArgPtr,
    88  			AllowMissingKeys: &allowMissingKeysPtr,
    89  		},
    90  
    91  		TemplateArgument: &templateArgPtr,
    92  		AllowMissingKeys: &allowMissingKeysPtr,
    93  	}
    94  }
    95  

View as plain text