File: cobrautil.go

package info (click to toggle)
docker-buildx 0.13.1%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,356 kB
  • sloc: sh: 299; makefile: 87
file content (53 lines) | stat: -rw-r--r-- 1,320 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package cobrautil

import (
	"github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
	"github.com/spf13/pflag"
)

// HideInheritedFlags hides inherited flags
func HideInheritedFlags(cmd *cobra.Command, hidden ...string) {
	for _, h := range hidden {
		// we could use cmd.SetHelpFunc to override the helper
		// but, it's not enough because we also want the generated
		// docs to be updated, so we override the flag instead
		cmd.Flags().String(h, "", "")
		_ = cmd.Flags().MarkHidden(h)
	}
}

const annotationExperimentalCLI = "experimentalCLI"

func MarkFlagExperimental(f *pflag.Flag) {
	if _, ok := f.Annotations[annotationExperimentalCLI]; ok {
		return
	}
	if f.Annotations == nil {
		f.Annotations = make(map[string][]string)
	}
	f.Annotations[annotationExperimentalCLI] = nil
	f.Usage += " (EXPERIMENTAL)"
}

func MarkFlagsExperimental(fs *pflag.FlagSet, names ...string) {
	for _, name := range names {
		f := fs.Lookup(name)
		if f == nil {
			logrus.Warningf("Unknown flag name %q", name)
			continue
		}
		MarkFlagExperimental(f)
	}
}

func MarkCommandExperimental(c *cobra.Command) {
	if _, ok := c.Annotations[annotationExperimentalCLI]; ok {
		return
	}
	if c.Annotations == nil {
		c.Annotations = make(map[string]string)
	}
	c.Annotations[annotationExperimentalCLI] = ""
	c.Short += " (EXPERIMENTAL)"
}