...

Source file src/k8s.io/kubectl/pkg/cmd/get/customcolumn_flags.go

Documentation: k8s.io/kubectl/pkg/cmd/get

     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 get
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"sort"
    23  	"strings"
    24  
    25  	"github.com/spf13/cobra"
    26  
    27  	"k8s.io/cli-runtime/pkg/genericclioptions"
    28  	"k8s.io/cli-runtime/pkg/printers"
    29  	"k8s.io/kubectl/pkg/scheme"
    30  )
    31  
    32  var columnsFormats = map[string]bool{
    33  	"custom-columns-file": true,
    34  	"custom-columns":      true,
    35  }
    36  
    37  // CustomColumnsPrintFlags provides default flags necessary for printing
    38  // custom resource columns from an inline-template or file.
    39  type CustomColumnsPrintFlags struct {
    40  	NoHeaders        bool
    41  	TemplateArgument string
    42  }
    43  
    44  func (f *CustomColumnsPrintFlags) AllowedFormats() []string {
    45  	formats := make([]string, 0, len(columnsFormats))
    46  	for format := range columnsFormats {
    47  		formats = append(formats, format)
    48  	}
    49  	sort.Strings(formats)
    50  	return formats
    51  }
    52  
    53  // ToPrinter receives an templateFormat and returns a printer capable of
    54  // handling custom-column printing.
    55  // Returns false if the specified templateFormat does not match a supported format.
    56  // Supported format types can be found in pkg/printers/printers.go
    57  func (f *CustomColumnsPrintFlags) ToPrinter(templateFormat string) (printers.ResourcePrinter, error) {
    58  	if len(templateFormat) == 0 {
    59  		return nil, genericclioptions.NoCompatiblePrinterError{}
    60  	}
    61  
    62  	templateValue := ""
    63  
    64  	if len(f.TemplateArgument) == 0 {
    65  		for format := range columnsFormats {
    66  			format = format + "="
    67  			if strings.HasPrefix(templateFormat, format) {
    68  				templateValue = templateFormat[len(format):]
    69  				templateFormat = format[:len(format)-1]
    70  				break
    71  			}
    72  		}
    73  	} else {
    74  		templateValue = f.TemplateArgument
    75  	}
    76  
    77  	if _, supportedFormat := columnsFormats[templateFormat]; !supportedFormat {
    78  		return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &templateFormat, AllowedFormats: f.AllowedFormats()}
    79  	}
    80  
    81  	if len(templateValue) == 0 {
    82  		return nil, fmt.Errorf("custom-columns format specified but no custom columns given")
    83  	}
    84  
    85  	// UniversalDecoder call must specify parameter versions; otherwise it will decode to internal versions.
    86  	decoder := scheme.Codecs.UniversalDecoder(scheme.Scheme.PrioritizedVersionsAllGroups()...)
    87  
    88  	if templateFormat == "custom-columns-file" {
    89  		file, err := os.Open(templateValue)
    90  		if err != nil {
    91  			return nil, fmt.Errorf("error reading template %s, %v\n", templateValue, err)
    92  		}
    93  		defer file.Close()
    94  		p, err := NewCustomColumnsPrinterFromTemplate(file, decoder)
    95  		return p, err
    96  	}
    97  
    98  	return NewCustomColumnsPrinterFromSpec(templateValue, decoder, f.NoHeaders)
    99  }
   100  
   101  // AddFlags receives a *cobra.Command reference and binds
   102  // flags related to custom-columns printing
   103  func (f *CustomColumnsPrintFlags) AddFlags(c *cobra.Command) {}
   104  
   105  // NewCustomColumnsPrintFlags returns flags associated with
   106  // custom-column printing, with default values set.
   107  // NoHeaders and TemplateArgument should be set by callers.
   108  func NewCustomColumnsPrintFlags() *CustomColumnsPrintFlags {
   109  	return &CustomColumnsPrintFlags{
   110  		NoHeaders:        false,
   111  		TemplateArgument: "",
   112  	}
   113  }
   114  

View as plain text