...

Source file src/k8s.io/cli-runtime/pkg/genericclioptions/name_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  	"fmt"
    21  	"strings"
    22  
    23  	"github.com/spf13/cobra"
    24  
    25  	"k8s.io/cli-runtime/pkg/printers"
    26  )
    27  
    28  // NamePrintFlags provides default flags necessary for printing
    29  // a resource's fully-qualified Kind.group/name, or a successful
    30  // message about that resource if an Operation is provided.
    31  type NamePrintFlags struct {
    32  	// Operation describes the name of the action that
    33  	// took place on an object, to be included in the
    34  	// finalized "successful" message.
    35  	Operation string
    36  }
    37  
    38  // Complete sets NamePrintFlags operation flag from successTemplate
    39  func (f *NamePrintFlags) Complete(successTemplate string) error {
    40  	f.Operation = fmt.Sprintf(successTemplate, f.Operation)
    41  	return nil
    42  }
    43  
    44  // AllowedFormats returns slice of string of allowed Name printing format
    45  func (f *NamePrintFlags) AllowedFormats() []string {
    46  	if f == nil {
    47  		return []string{}
    48  	}
    49  	return []string{"name"}
    50  }
    51  
    52  // ToPrinter receives an outputFormat and returns a printer capable of
    53  // handling --output=name printing.
    54  // Returns false if the specified outputFormat does not match a supported format.
    55  // Supported format types can be found in pkg/printers/printers.go
    56  func (f *NamePrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error) {
    57  	namePrinter := &printers.NamePrinter{
    58  		Operation: f.Operation,
    59  	}
    60  
    61  	outputFormat = strings.ToLower(outputFormat)
    62  	switch outputFormat {
    63  	case "name":
    64  		namePrinter.ShortOutput = true
    65  		fallthrough
    66  	case "":
    67  		return namePrinter, nil
    68  	default:
    69  		return nil, NoCompatiblePrinterError{OutputFormat: &outputFormat, AllowedFormats: f.AllowedFormats()}
    70  	}
    71  }
    72  
    73  // AddFlags receives a *cobra.Command reference and binds
    74  // flags related to name printing to it
    75  func (f *NamePrintFlags) AddFlags(c *cobra.Command) {}
    76  
    77  // NewNamePrintFlags returns flags associated with
    78  // --name printing, with default values set.
    79  func NewNamePrintFlags(operation string) *NamePrintFlags {
    80  	return &NamePrintFlags{
    81  		Operation: operation,
    82  	}
    83  }
    84  

View as plain text