github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/cmd/tools/helm/flags.go (about)

     1  /*
     2  Copyright The Helm 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  /*
    18  NOTICE: This file's 'package' and some functionality has been modified / removed to fit within Jackal's package structure.
    19  */
    20  
    21  // Package helm is a copy of the main package from helm to include a subset of the helm CLI in Jackal
    22  package helm
    23  
    24  import (
    25  	"flag"
    26  	"fmt"
    27  	"log"
    28  	"sort"
    29  	"strings"
    30  
    31  	"github.com/spf13/cobra"
    32  	"github.com/spf13/pflag"
    33  	"helm.sh/helm/v3/pkg/cli/output"
    34  	"k8s.io/klog/v2"
    35  )
    36  
    37  const (
    38  	outputFlag = "output"
    39  )
    40  
    41  // bindOutputFlag will add the output flag to the given command and bind the
    42  // value to the given format pointer
    43  func bindOutputFlag(cmd *cobra.Command, varRef *output.Format) {
    44  	cmd.Flags().VarP(newOutputValue(output.Table, varRef), outputFlag, "o",
    45  		fmt.Sprintf("prints the output in the specified format. Allowed values: %s", strings.Join(output.Formats(), ", ")))
    46  
    47  	err := cmd.RegisterFlagCompletionFunc(outputFlag, func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
    48  		var formatNames []string
    49  		for format, desc := range output.FormatsWithDesc() {
    50  			formatNames = append(formatNames, fmt.Sprintf("%s\t%s", format, desc))
    51  		}
    52  
    53  		// Sort the results to get a deterministic order for the tests
    54  		sort.Strings(formatNames)
    55  		return formatNames, cobra.ShellCompDirectiveNoFileComp
    56  	})
    57  
    58  	if err != nil {
    59  		log.Fatal(err)
    60  	}
    61  }
    62  
    63  type outputValue output.Format
    64  
    65  func newOutputValue(defaultValue output.Format, p *output.Format) *outputValue {
    66  	*p = defaultValue
    67  	return (*outputValue)(p)
    68  }
    69  
    70  func (o *outputValue) String() string {
    71  	// It is much cleaner looking (and technically less allocations) to just
    72  	// convert to a string rather than type asserting to the underlying
    73  	// output.Format
    74  	return string(*o)
    75  }
    76  
    77  func (o *outputValue) Type() string {
    78  	return "format"
    79  }
    80  
    81  func (o *outputValue) Set(s string) error {
    82  	outfmt, err := output.ParseFormat(s)
    83  	if err != nil {
    84  		return err
    85  	}
    86  	*o = outputValue(outfmt)
    87  	return nil
    88  }
    89  
    90  // addKlogFlags adds flags from k8s.io/klog
    91  // marks the flags as hidden to avoid polluting the help text
    92  func addKlogFlags(fs *pflag.FlagSet) {
    93  	local := flag.NewFlagSet("klog", flag.ExitOnError)
    94  	klog.InitFlags(local)
    95  	local.VisitAll(func(fl *flag.Flag) {
    96  		fl.Name = normalize(fl.Name)
    97  		if fs.Lookup(fl.Name) != nil {
    98  			return
    99  		}
   100  		newflag := pflag.PFlagFromGoFlag(fl)
   101  		newflag.Hidden = true
   102  		fs.AddFlag(newflag)
   103  	})
   104  }
   105  
   106  // normalize replaces underscores with hyphens
   107  func normalize(s string) string {
   108  	return strings.ReplaceAll(s, "_", "-")
   109  }