github.com/pdmccormick/importable-docker-buildx@v0.0.0-20240426161518-e47091289030/util/cobrautil/cobrautil.go (about)

     1  package cobrautil
     2  
     3  import (
     4  	"github.com/sirupsen/logrus"
     5  	"github.com/spf13/cobra"
     6  	"github.com/spf13/pflag"
     7  )
     8  
     9  // HideInheritedFlags hides inherited flags
    10  func HideInheritedFlags(cmd *cobra.Command, hidden ...string) {
    11  	for _, h := range hidden {
    12  		// we could use cmd.SetHelpFunc to override the helper
    13  		// but, it's not enough because we also want the generated
    14  		// docs to be updated, so we override the flag instead
    15  		cmd.Flags().String(h, "", "")
    16  		_ = cmd.Flags().MarkHidden(h)
    17  	}
    18  }
    19  
    20  const annotationExperimentalCLI = "experimentalCLI"
    21  
    22  func MarkFlagExperimental(f *pflag.Flag) {
    23  	if _, ok := f.Annotations[annotationExperimentalCLI]; ok {
    24  		return
    25  	}
    26  	if f.Annotations == nil {
    27  		f.Annotations = make(map[string][]string)
    28  	}
    29  	f.Annotations[annotationExperimentalCLI] = nil
    30  	f.Usage += " (EXPERIMENTAL)"
    31  }
    32  
    33  func MarkFlagsExperimental(fs *pflag.FlagSet, names ...string) {
    34  	for _, name := range names {
    35  		f := fs.Lookup(name)
    36  		if f == nil {
    37  			logrus.Warningf("Unknown flag name %q", name)
    38  			continue
    39  		}
    40  		MarkFlagExperimental(f)
    41  	}
    42  }
    43  
    44  func MarkCommandExperimental(c *cobra.Command) {
    45  	if _, ok := c.Annotations[annotationExperimentalCLI]; ok {
    46  		return
    47  	}
    48  	if c.Annotations == nil {
    49  		c.Annotations = make(map[string]string)
    50  	}
    51  	c.Annotations[annotationExperimentalCLI] = ""
    52  	c.Short += " (EXPERIMENTAL)"
    53  }