Documentation ¶
Index ¶
- Variables
- func IsBuiltinCommand(cmd *cobra.Command) bool
- func MarkFlagsHidden(flags *pflag.FlagSet, names ...string) error
- func MustGetBool(cmd *cobra.Command, name string) bool
- func MustGetBoolSlice(cmd *cobra.Command, name string) []bool
- func MustGetBytesBase64(cmd *cobra.Command, name string) []byte
- func MustGetBytesHex(cmd *cobra.Command, name string) []byte
- func MustGetCount(cmd *cobra.Command, name string) int
- func MustGetDuration(cmd *cobra.Command, name string) time.Duration
- func MustGetDurationSlice(cmd *cobra.Command, name string) []time.Duration
- func MustGetFloat32(cmd *cobra.Command, name string) float32
- func MustGetFloat32Slice(cmd *cobra.Command, name string) []float32
- func MustGetFloat64(cmd *cobra.Command, name string) float64
- func MustGetFloat64Slice(cmd *cobra.Command, name string) []float64
- func MustGetIP(cmd *cobra.Command, name string) net.IP
- func MustGetIPNet(cmd *cobra.Command, name string) net.IPNet
- func MustGetIPSlice(cmd *cobra.Command, name string) []net.IP
- func MustGetIPv4Mask(cmd *cobra.Command, name string) net.IPMask
- func MustGetInt(cmd *cobra.Command, name string) int
- func MustGetInt16(cmd *cobra.Command, name string) int16
- func MustGetInt32(cmd *cobra.Command, name string) int32
- func MustGetInt32Slice(cmd *cobra.Command, name string) []int32
- func MustGetInt64(cmd *cobra.Command, name string) int64
- func MustGetInt64Slice(cmd *cobra.Command, name string) []int64
- func MustGetInt8(cmd *cobra.Command, name string) int8
- func MustGetIntSlice(cmd *cobra.Command, name string) []int
- func MustGetString(cmd *cobra.Command, name string) string
- func MustGetStringExpanded(cmd *cobra.Command, name string) string
- func MustGetStringSlice(cmd *cobra.Command, name string) []string
- func MustGetStringSliceExpanded(cmd *cobra.Command, name string) []string
- func MustGetStringToInt(cmd *cobra.Command, name string) map[string]int
- func MustGetStringToInt64(cmd *cobra.Command, name string) map[string]int64
- func MustGetStringToString(cmd *cobra.Command, name string) map[string]string
- func MustGetUint(cmd *cobra.Command, name string) uint
- func MustGetUint16(cmd *cobra.Command, name string) uint16
- func MustGetUint32(cmd *cobra.Command, name string) uint32
- func MustGetUint64(cmd *cobra.Command, name string) uint64
- func MustGetUint8(cmd *cobra.Command, name string) uint8
- func MustGetUintSlice(cmd *cobra.Command, name string) []uint
- func PrefixJoiner(prefix string) func(...string) string
- func RegisterVersionFlags(flags *pflag.FlagSet)
- func UsageVersion(programName string, includeDeps bool) string
- func VersionWithFallbacks(bi *debug.BuildInfo) string
- type CobraRunFunc
- type NamedFlagSets
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Version string
Version is variable that holds program's version string. This should be set with the follow flags to the `go build` command: -ldflags '-X github.com/jzelinskie/cobrautil.Version=$YOUR_VERSION_HERE'
Functions ¶
func IsBuiltinCommand ¶
IsBuiltinCommand checks against a hard-coded list of the names of commands that cobra provides out-of-the-box.
func MarkFlagsHidden ¶
MarkFlagsHidden is a convenient way to mark flags as hidden in bulk.
func MustGetBool ¶
MustGetBool returns the bool value of a flag with the given name and panics if that flag was never defined.
func MustGetBoolSlice ¶
MustGetBoolSlice returns the []bool value of a flag with the given name and panics if that flag was never defined.
func MustGetBytesBase64 ¶
MustGetBytesBase64 returns the []byte value of a flag with the given name and panics if that flag was never defined.
func MustGetBytesHex ¶
MustGetBytesHex returns the []byte value of a flag with the given name and panics if that flag was never defined.
func MustGetCount ¶
MustGetCount returns the int value of a flag with the given name and panics if that flag was never defined.
func MustGetDuration ¶
MustGetDuration returns the time.Duration of a flag with the given name and panics if that flag was never defined.
func MustGetDurationSlice ¶
MustGetDurationSlice returns the []time.Duration of a flag with the given name and panics if that flag was never defined.
func MustGetFloat32 ¶
MustGetFloat32 returns the float32 value of a flag with the given name and panics if that flag was never defined.
func MustGetFloat32Slice ¶
MustGetFloat32Slice returns the []float32 value of a flag with the given name and panics if that flag was never defined.
func MustGetFloat64 ¶
MustGetFloat64 returns the float64 value of a flag with the given name and panics if that flag was never defined.
func MustGetFloat64Slice ¶
MustGetFloat64Slice returns the []float64 value of a flag with the given name and panics if that flag was never defined.
func MustGetIP ¶
MustGetIP returns the net.IP value of a flag with the given name and panics if that flag was never defined.
func MustGetIPNet ¶
MustGetIPNet returns the net.IPNet value of a flag with the given name and panics if that flag was never defined.
func MustGetIPSlice ¶
MustGetIPSlice returns the []net.IP value of a flag with the given name and panics if that flag was never defined.
func MustGetIPv4Mask ¶
MustGetIPv4Mask returns the net.IPMask value of a flag with the given name and panics if that flag was never defined.
func MustGetInt ¶
MustGetInt returns the int value of a flag with the given name and panics if that flag was never defined.
func MustGetInt16 ¶
MustGetInt16 returns the int16 value of a flag with the given name and panics if that flag was never defined.
func MustGetInt32 ¶
MustGetInt32 returns the int32 value of a flag with the given name and panics if that flag was never defined.
func MustGetInt32Slice ¶
MustGetInt32Slice returns the []int32 value of a flag with the given name and panics if that flag was never defined.
func MustGetInt64 ¶
MustGetInt64 returns the int64 value of a flag with the given name and panics if that flag was never defined.
func MustGetInt64Slice ¶
MustGetInt64Slice returns the []int64 value of a flag with the given name and panics if that flag was never defined.
func MustGetInt8 ¶
MustGetInt8 returns the int8 value of a flag with the given name and panics if that flag was never defined.
func MustGetIntSlice ¶
MustGetIntSlice returns the []int value of a flag with the given name and panics if that flag was never defined.
func MustGetString ¶
MustGetString returns the string value of a flag with the given name and panics if that flag was never defined.
func MustGetStringExpanded ¶
MustGetStringExpanded returns the string value of a flag with the given name, calls os.Expand on it, and panics if that flag was never defined.
func MustGetStringSlice ¶
MustGetStringSlice returns the []string value of a flag with the given name and panics if that flag was never defined.
func MustGetStringSliceExpanded ¶
MustGetStringSlice returns the []string value of a flag with the given name, calls os.ExpandEnv on values, and panics if that flag was never defined.
func MustGetStringToInt ¶
MustGetStringToInt returns the map[string]int value of a flag with the given name and panics if that flag was never defined.
func MustGetStringToInt64 ¶
MustGetStringToInt64 returns the map[string]int64 value of a flag with the given name and panics if that flag was never defined.
func MustGetStringToString ¶
MustGetStringToString returns the map[string]string value of a flag with the given name and panics if that flag was never defined.
func MustGetUint ¶
MustGetUint returns the uint value of a flag with the given name and panics if that flag was never defined.
func MustGetUint16 ¶
MustGetUint16 returns the uint16 value of a flag with the given name and panics if that flag was never defined.
func MustGetUint32 ¶
MustGetUint32 returns the uint32 value of a flag with the given name and panics if that flag was never defined.
func MustGetUint64 ¶
MustGetUint64 returns the uint64 value of a flag with the given name and panics if that flag was never defined.
func MustGetUint8 ¶
MustGetUint8 returns the uint8 value of a flag with the given name and panics if that flag was never defined.
func MustGetUintSlice ¶
MustGetUintSlice returns the []uint value of a flag with the given name and panics if that flag was never defined.
func PrefixJoiner ¶
PrefixJoiner joins a list of strings with the "-" separator, including the provided prefix string
example: PrefixJoiner("hi")("how", "are", "you") = "hi-how-are-you"
func RegisterVersionFlags ¶
RegisterVersionFlags registers the flags used for the VersionRunFunc.
func UsageVersion ¶
UsageVersion introspects the process debug data for Go modules to return a version string.
func VersionWithFallbacks ¶
VersionWithFallbacks returns a string of the program version. If the version wasn't set by ldflags, falls back to the VCS revision, and finally Go module version.
Types ¶
type CobraRunFunc ¶
CobraRunFunc is the signature of cobra.Command RunFuncs.
func CommandStack ¶
func CommandStack(cmdfns ...CobraRunFunc) CobraRunFunc
CommandStack chains together a collection of CobraCommandFuncs into one.
Example ¶
_ = &cobra.Command{ Use: "mycmd", RunE: cobrautil.CommandStack( cobrautil.SyncViperPreRunE("myprogram"), func(cmd *cobra.Command, args []string) error { return nil }, ), }
Output:
func SyncViperDotEnvPreRunE ¶
func SyncViperDotEnvPreRunE(prefix, envfilePath string, l logr.Logger) CobraRunFunc
SyncViperDotEnvPreRunE returns a CobraRunFunc that loads a .dotenv file before synchronizing Viper environment flags with the provided prefix.
If empty, envfilePath defaults to ".env". The .dotenv file is loaded first before any additional Viper behavior.
func SyncViperPreRunE ¶
func SyncViperPreRunE(prefix string) CobraRunFunc
SyncViperPreRunE returns a CobraRunFunc that synchronizes Viper environment flags with the provided prefix.
Thanks to Carolyn Van Slyck: https://github.com/carolynvs/stingoftheviper
func VersionRunFunc ¶
func VersionRunFunc(programName string) CobraRunFunc
VersionRunFunc provides a generic implementation of a version command that reads its values from ldflags and the internal Go module data stored in a binary.
type NamedFlagSets ¶
type NamedFlagSets struct { // Order is an ordered list of flag set names. Order []string // FlagSets stores the flag sets by name. FlagSets map[string]*pflag.FlagSet // NormalizeNameFunc is the normalize function which used to initialize // FlagSets created by NamedFlagSets. NormalizeNameFunc func(f *pflag.FlagSet, name string) pflag.NormalizedName // contains filtered or unexported fields }
NamedFlagSets stores named flag sets in the order of calling FlagSet.
This type is largely adapted from k8s.io/component-base/cli/flag, but modified to be less brittle by integrating with cobra's templates rather than entirely overriding UsageFuncs and HelpFuncs.
func NewNamedFlagSets ¶
func NewNamedFlagSets(cmd *cobra.Command) *NamedFlagSets
NewNamedFlagSets creates a new NamedFlagSets and registers it with the provided command.
func (*NamedFlagSets) AddFlagSets ¶
func (nfs *NamedFlagSets) AddFlagSets(cmd *cobra.Command)
func (*NamedFlagSets) FlagSet ¶
func (nfs *NamedFlagSets) FlagSet(name string) *pflag.FlagSet
FlagSet returns the flag set with the given name and adds it to the ordered name list if it is not in there yet.
func (*NamedFlagSets) SetUsageTemplate ¶
func (nfs *NamedFlagSets) SetUsageTemplate(cmd *cobra.Command)
SetUsageTemplate overrides the cobra usage template to include the named flags sections.
Directories ¶
Path | Synopsis |
---|---|
Package cobragrpc implements a builder for registering flags and producing a Cobra RunFunc that configures a gRPC server.
|
Package cobragrpc implements a builder for registering flags and producing a Cobra RunFunc that configures a gRPC server. |
Package cobrahttp implements a builder for registering flags and producing a Cobra RunFunc that configures an HTTP server.
|
Package cobrahttp implements a builder for registering flags and producing a Cobra RunFunc that configures an HTTP server. |
Package cobraotel implements a builder for registering flags and producing a Cobra RunFunc that configures OpenTelemetry.
|
Package cobraotel implements a builder for registering flags and producing a Cobra RunFunc that configures OpenTelemetry. |
Package cobrahttp implements a builder for registering flags and producing a Cobra RunFunc that configures Zerolog.
|
Package cobrahttp implements a builder for registering flags and producing a Cobra RunFunc that configures Zerolog. |