...

Source file src/k8s.io/cli-runtime/pkg/genericclioptions/json_yaml_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  	"strings"
    21  
    22  	"github.com/spf13/cobra"
    23  
    24  	"k8s.io/cli-runtime/pkg/printers"
    25  )
    26  
    27  // AllowedFormats returns slice of string of allowed JSONYaml printing format
    28  func (f *JSONYamlPrintFlags) AllowedFormats() []string {
    29  	if f == nil {
    30  		return []string{}
    31  	}
    32  	return []string{"json", "yaml"}
    33  }
    34  
    35  // JSONYamlPrintFlags provides default flags necessary for json/yaml printing.
    36  // Given the following flag values, a printer can be requested that knows
    37  // how to handle printing based on these values.
    38  type JSONYamlPrintFlags struct {
    39  	ShowManagedFields bool
    40  }
    41  
    42  // ToPrinter receives an outputFormat and returns a printer capable of
    43  // handling --output=(yaml|json) printing.
    44  // Returns false if the specified outputFormat does not match a supported format.
    45  // Supported Format types can be found in pkg/printers/printers.go
    46  func (f *JSONYamlPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error) {
    47  	var printer printers.ResourcePrinter
    48  
    49  	outputFormat = strings.ToLower(outputFormat)
    50  	switch outputFormat {
    51  	case "json":
    52  		printer = &printers.JSONPrinter{}
    53  	case "yaml":
    54  		printer = &printers.YAMLPrinter{}
    55  	default:
    56  		return nil, NoCompatiblePrinterError{OutputFormat: &outputFormat, AllowedFormats: f.AllowedFormats()}
    57  	}
    58  
    59  	if !f.ShowManagedFields {
    60  		printer = &printers.OmitManagedFieldsPrinter{Delegate: printer}
    61  	}
    62  	return printer, nil
    63  }
    64  
    65  // AddFlags receives a *cobra.Command reference and binds
    66  // flags related to JSON or Yaml printing to it
    67  func (f *JSONYamlPrintFlags) AddFlags(c *cobra.Command) {
    68  	if f == nil {
    69  		return
    70  	}
    71  
    72  	c.Flags().BoolVar(&f.ShowManagedFields, "show-managed-fields", f.ShowManagedFields, "If true, keep the managedFields when printing objects in JSON or YAML format.")
    73  }
    74  
    75  // NewJSONYamlPrintFlags returns flags associated with
    76  // yaml or json printing, with default values set.
    77  func NewJSONYamlPrintFlags() *JSONYamlPrintFlags {
    78  	return &JSONYamlPrintFlags{}
    79  }
    80  

View as plain text