...

Source file src/k8s.io/kubectl/pkg/cmd/get/get_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  	"strings"
    22  
    23  	"github.com/spf13/cobra"
    24  
    25  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  	"k8s.io/cli-runtime/pkg/genericclioptions"
    27  	"k8s.io/cli-runtime/pkg/printers"
    28  	"k8s.io/kubectl/pkg/cmd/util"
    29  )
    30  
    31  // PrintFlags composes common printer flag structs
    32  // used in the Get command.
    33  type PrintFlags struct {
    34  	JSONYamlPrintFlags *genericclioptions.JSONYamlPrintFlags
    35  	NamePrintFlags     *genericclioptions.NamePrintFlags
    36  	CustomColumnsFlags *CustomColumnsPrintFlags
    37  	HumanReadableFlags *HumanPrintFlags
    38  	TemplateFlags      *genericclioptions.KubeTemplatePrintFlags
    39  
    40  	NoHeaders    *bool
    41  	OutputFormat *string
    42  }
    43  
    44  // SetKind sets the Kind option of humanreadable flags
    45  func (f *PrintFlags) SetKind(kind schema.GroupKind) {
    46  	f.HumanReadableFlags.SetKind(kind)
    47  }
    48  
    49  // EnsureWithNamespace ensures that humanreadable flags return
    50  // a printer capable of printing with a "namespace" column.
    51  func (f *PrintFlags) EnsureWithNamespace() error {
    52  	return f.HumanReadableFlags.EnsureWithNamespace()
    53  }
    54  
    55  // EnsureWithKind ensures that humanreadable flags return
    56  // a printer capable of including resource kinds.
    57  func (f *PrintFlags) EnsureWithKind() error {
    58  	return f.HumanReadableFlags.EnsureWithKind()
    59  }
    60  
    61  // Copy returns a copy of PrintFlags for mutation
    62  func (f *PrintFlags) Copy() PrintFlags {
    63  	printFlags := *f
    64  	return printFlags
    65  }
    66  
    67  // AllowedFormats is the list of formats in which data can be displayed
    68  func (f *PrintFlags) AllowedFormats() []string {
    69  	formats := f.JSONYamlPrintFlags.AllowedFormats()
    70  	formats = append(formats, f.NamePrintFlags.AllowedFormats()...)
    71  	formats = append(formats, f.TemplateFlags.AllowedFormats()...)
    72  	formats = append(formats, f.CustomColumnsFlags.AllowedFormats()...)
    73  	formats = append(formats, f.HumanReadableFlags.AllowedFormats()...)
    74  	return formats
    75  }
    76  
    77  // ToPrinter attempts to find a composed set of PrintFlags suitable for
    78  // returning a printer based on current flag values.
    79  func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) {
    80  	outputFormat := ""
    81  	if f.OutputFormat != nil {
    82  		outputFormat = *f.OutputFormat
    83  	}
    84  
    85  	noHeaders := false
    86  	if f.NoHeaders != nil {
    87  		noHeaders = *f.NoHeaders
    88  	}
    89  	f.HumanReadableFlags.NoHeaders = noHeaders
    90  	f.CustomColumnsFlags.NoHeaders = noHeaders
    91  
    92  	// for "get.go" we want to support a --template argument given, even when no --output format is provided
    93  	if f.TemplateFlags.TemplateArgument != nil && len(*f.TemplateFlags.TemplateArgument) > 0 && len(outputFormat) == 0 {
    94  		outputFormat = "go-template"
    95  	}
    96  
    97  	if p, err := f.TemplateFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
    98  		return p, err
    99  	}
   100  
   101  	if f.TemplateFlags.TemplateArgument != nil {
   102  		f.CustomColumnsFlags.TemplateArgument = *f.TemplateFlags.TemplateArgument
   103  	}
   104  
   105  	if p, err := f.JSONYamlPrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
   106  		return p, err
   107  	}
   108  
   109  	if p, err := f.HumanReadableFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
   110  		return p, err
   111  	}
   112  
   113  	if p, err := f.CustomColumnsFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
   114  		return p, err
   115  	}
   116  
   117  	if p, err := f.NamePrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) {
   118  		return p, err
   119  	}
   120  
   121  	return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &outputFormat, AllowedFormats: f.AllowedFormats()}
   122  }
   123  
   124  // AddFlags receives a *cobra.Command reference and binds
   125  // flags related to humanreadable and template printing.
   126  func (f *PrintFlags) AddFlags(cmd *cobra.Command) {
   127  	f.JSONYamlPrintFlags.AddFlags(cmd)
   128  	f.NamePrintFlags.AddFlags(cmd)
   129  	f.TemplateFlags.AddFlags(cmd)
   130  	f.HumanReadableFlags.AddFlags(cmd)
   131  	f.CustomColumnsFlags.AddFlags(cmd)
   132  
   133  	if f.OutputFormat != nil {
   134  		cmd.Flags().StringVarP(f.OutputFormat, "output", "o", *f.OutputFormat, fmt.Sprintf(`Output format. One of: (%s). See custom columns [https://kubernetes.io/docs/reference/kubectl/#custom-columns], golang template [http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template [https://kubernetes.io/docs/reference/kubectl/jsonpath/].`, strings.Join(f.AllowedFormats(), ", ")))
   135  		util.CheckErr(cmd.RegisterFlagCompletionFunc(
   136  			"output",
   137  			func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
   138  				var comps []string
   139  				for _, format := range f.AllowedFormats() {
   140  					if strings.HasPrefix(format, toComplete) {
   141  						comps = append(comps, format)
   142  					}
   143  				}
   144  				return comps, cobra.ShellCompDirectiveNoFileComp
   145  			},
   146  		))
   147  	}
   148  	if f.NoHeaders != nil {
   149  		cmd.Flags().BoolVar(f.NoHeaders, "no-headers", *f.NoHeaders, "When using the default or custom-column output format, don't print headers (default print headers).")
   150  	}
   151  }
   152  
   153  // NewGetPrintFlags returns flags associated with humanreadable,
   154  // template, and "name" printing, with default values set.
   155  func NewGetPrintFlags() *PrintFlags {
   156  	outputFormat := ""
   157  	noHeaders := false
   158  
   159  	return &PrintFlags{
   160  		OutputFormat: &outputFormat,
   161  		NoHeaders:    &noHeaders,
   162  
   163  		JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(),
   164  		NamePrintFlags:     genericclioptions.NewNamePrintFlags(""),
   165  		TemplateFlags:      genericclioptions.NewKubeTemplatePrintFlags(),
   166  
   167  		HumanReadableFlags: NewHumanPrintFlags(),
   168  		CustomColumnsFlags: NewCustomColumnsPrintFlags(),
   169  	}
   170  }
   171  

View as plain text