...

Source file src/k8s.io/cli-runtime/pkg/genericclioptions/filename_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  	"github.com/spf13/pflag"
    24  
    25  	"k8s.io/cli-runtime/pkg/resource"
    26  )
    27  
    28  // FileNameFlags are flags for processing files.
    29  // Usage of this struct by itself is discouraged.
    30  // These flags are composed by ResourceBuilderFlags
    31  // which should be used instead.
    32  type FileNameFlags struct {
    33  	Usage string
    34  
    35  	Filenames *[]string
    36  	Kustomize *string
    37  	Recursive *bool
    38  }
    39  
    40  // ToOptions creates a new FileNameOptions struct and sets FilenameOptions based on FileNameflags
    41  func (o *FileNameFlags) ToOptions() resource.FilenameOptions {
    42  	options := resource.FilenameOptions{}
    43  
    44  	if o == nil {
    45  		return options
    46  	}
    47  
    48  	if o.Recursive != nil {
    49  		options.Recursive = *o.Recursive
    50  	}
    51  	if o.Filenames != nil {
    52  		options.Filenames = *o.Filenames
    53  	}
    54  	if o.Kustomize != nil {
    55  		options.Kustomize = *o.Kustomize
    56  	}
    57  
    58  	return options
    59  }
    60  
    61  // AddFlags binds file name flags to a given flagset
    62  func (o *FileNameFlags) AddFlags(flags *pflag.FlagSet) {
    63  	if o == nil {
    64  		return
    65  	}
    66  
    67  	if o.Recursive != nil {
    68  		flags.BoolVarP(o.Recursive, "recursive", "R", *o.Recursive, "Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory.")
    69  	}
    70  	if o.Filenames != nil {
    71  		flags.StringSliceVarP(o.Filenames, "filename", "f", *o.Filenames, o.Usage)
    72  		annotations := make([]string, 0, len(resource.FileExtensions))
    73  		for _, ext := range resource.FileExtensions {
    74  			annotations = append(annotations, strings.TrimLeft(ext, "."))
    75  		}
    76  		flags.SetAnnotation("filename", cobra.BashCompFilenameExt, annotations)
    77  	}
    78  	if o.Kustomize != nil {
    79  		flags.StringVarP(o.Kustomize, "kustomize", "k", *o.Kustomize,
    80  			"Process a kustomization directory. This flag can't be used together with -f or -R.")
    81  	}
    82  }
    83  

View as plain text