cobrautil

package module
v2.0.0-...-f7fe739 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2024 License: Apache-2.0 Imports: 16 Imported by: 14

README

cobrautil

A collection of utility functions when using Cobra.

Features include:

  • Synchronizing Viper environment variables
  • "Must" functions to fetch flags and panic if they do not exist
  • Middleware chaining of cobra.Command RunFuncs

See some examples in the documentation.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
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

func IsBuiltinCommand(cmd *cobra.Command) bool

IsBuiltinCommand checks against a hard-coded list of the names of commands that cobra provides out-of-the-box.

func MarkFlagsHidden

func MarkFlagsHidden(flags *pflag.FlagSet, names ...string) error

MarkFlagsHidden is a convenient way to mark flags as hidden in bulk.

func MustGetBool

func MustGetBool(cmd *cobra.Command, name string) bool

MustGetBool returns the bool value of a flag with the given name and panics if that flag was never defined.

func MustGetBoolSlice

func MustGetBoolSlice(cmd *cobra.Command, name string) []bool

MustGetBoolSlice returns the []bool value of a flag with the given name and panics if that flag was never defined.

func MustGetBytesBase64

func MustGetBytesBase64(cmd *cobra.Command, name string) []byte

MustGetBytesBase64 returns the []byte value of a flag with the given name and panics if that flag was never defined.

func MustGetBytesHex

func MustGetBytesHex(cmd *cobra.Command, name string) []byte

MustGetBytesHex returns the []byte value of a flag with the given name and panics if that flag was never defined.

func MustGetCount

func MustGetCount(cmd *cobra.Command, name string) int

MustGetCount returns the int value of a flag with the given name and panics if that flag was never defined.

func MustGetDuration

func MustGetDuration(cmd *cobra.Command, name string) time.Duration

MustGetDuration returns the time.Duration of a flag with the given name and panics if that flag was never defined.

func MustGetDurationSlice

func MustGetDurationSlice(cmd *cobra.Command, name string) []time.Duration

MustGetDurationSlice returns the []time.Duration of a flag with the given name and panics if that flag was never defined.

func MustGetFloat32

func MustGetFloat32(cmd *cobra.Command, name string) float32

MustGetFloat32 returns the float32 value of a flag with the given name and panics if that flag was never defined.

func MustGetFloat32Slice

func MustGetFloat32Slice(cmd *cobra.Command, name string) []float32

MustGetFloat32Slice returns the []float32 value of a flag with the given name and panics if that flag was never defined.

func MustGetFloat64

func MustGetFloat64(cmd *cobra.Command, name string) float64

MustGetFloat64 returns the float64 value of a flag with the given name and panics if that flag was never defined.

func MustGetFloat64Slice

func MustGetFloat64Slice(cmd *cobra.Command, name string) []float64

MustGetFloat64Slice returns the []float64 value of a flag with the given name and panics if that flag was never defined.

func MustGetIP

func MustGetIP(cmd *cobra.Command, name string) net.IP

MustGetIP returns the net.IP value of a flag with the given name and panics if that flag was never defined.

func MustGetIPNet

func MustGetIPNet(cmd *cobra.Command, name string) net.IPNet

MustGetIPNet returns the net.IPNet value of a flag with the given name and panics if that flag was never defined.

func MustGetIPSlice

func MustGetIPSlice(cmd *cobra.Command, name string) []net.IP

MustGetIPSlice returns the []net.IP value of a flag with the given name and panics if that flag was never defined.

func MustGetIPv4Mask

func MustGetIPv4Mask(cmd *cobra.Command, name string) net.IPMask

MustGetIPv4Mask returns the net.IPMask value of a flag with the given name and panics if that flag was never defined.

func MustGetInt

func MustGetInt(cmd *cobra.Command, name string) int

MustGetInt returns the int value of a flag with the given name and panics if that flag was never defined.

func MustGetInt16

func MustGetInt16(cmd *cobra.Command, name string) int16

MustGetInt16 returns the int16 value of a flag with the given name and panics if that flag was never defined.

func MustGetInt32

func MustGetInt32(cmd *cobra.Command, name string) int32

MustGetInt32 returns the int32 value of a flag with the given name and panics if that flag was never defined.

func MustGetInt32Slice

func MustGetInt32Slice(cmd *cobra.Command, name string) []int32

MustGetInt32Slice returns the []int32 value of a flag with the given name and panics if that flag was never defined.

func MustGetInt64

func MustGetInt64(cmd *cobra.Command, name string) int64

MustGetInt64 returns the int64 value of a flag with the given name and panics if that flag was never defined.

func MustGetInt64Slice

func MustGetInt64Slice(cmd *cobra.Command, name string) []int64

MustGetInt64Slice returns the []int64 value of a flag with the given name and panics if that flag was never defined.

func MustGetInt8

func MustGetInt8(cmd *cobra.Command, name string) int8

MustGetInt8 returns the int8 value of a flag with the given name and panics if that flag was never defined.

func MustGetIntSlice

func MustGetIntSlice(cmd *cobra.Command, name string) []int

MustGetIntSlice returns the []int value of a flag with the given name and panics if that flag was never defined.

func MustGetString

func MustGetString(cmd *cobra.Command, name string) string

MustGetString returns the string value of a flag with the given name and panics if that flag was never defined.

func MustGetStringExpanded

func MustGetStringExpanded(cmd *cobra.Command, name string) string

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

func MustGetStringSlice(cmd *cobra.Command, name string) []string

MustGetStringSlice returns the []string value of a flag with the given name and panics if that flag was never defined.

func MustGetStringSliceExpanded

func MustGetStringSliceExpanded(cmd *cobra.Command, name string) []string

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

func MustGetStringToInt(cmd *cobra.Command, name string) map[string]int

MustGetStringToInt returns the map[string]int value of a flag with the given name and panics if that flag was never defined.

func MustGetStringToInt64

func MustGetStringToInt64(cmd *cobra.Command, name string) map[string]int64

MustGetStringToInt64 returns the map[string]int64 value of a flag with the given name and panics if that flag was never defined.

func MustGetStringToString

func MustGetStringToString(cmd *cobra.Command, name string) map[string]string

MustGetStringToString returns the map[string]string value of a flag with the given name and panics if that flag was never defined.

func MustGetUint

func MustGetUint(cmd *cobra.Command, name string) uint

MustGetUint returns the uint value of a flag with the given name and panics if that flag was never defined.

func MustGetUint16

func MustGetUint16(cmd *cobra.Command, name string) uint16

MustGetUint16 returns the uint16 value of a flag with the given name and panics if that flag was never defined.

func MustGetUint32

func MustGetUint32(cmd *cobra.Command, name string) uint32

MustGetUint32 returns the uint32 value of a flag with the given name and panics if that flag was never defined.

func MustGetUint64

func MustGetUint64(cmd *cobra.Command, name string) uint64

MustGetUint64 returns the uint64 value of a flag with the given name and panics if that flag was never defined.

func MustGetUint8

func MustGetUint8(cmd *cobra.Command, name string) uint8

MustGetUint8 returns the uint8 value of a flag with the given name and panics if that flag was never defined.

func MustGetUintSlice

func MustGetUintSlice(cmd *cobra.Command, name string) []uint

MustGetUintSlice returns the []uint value of a flag with the given name and panics if that flag was never defined.

func PrefixJoiner

func PrefixJoiner(prefix string) func(...string) string

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

func RegisterVersionFlags(flags *pflag.FlagSet)

RegisterVersionFlags registers the flags used for the VersionRunFunc.

func UsageVersion

func UsageVersion(programName string, includeDeps bool) string

UsageVersion introspects the process debug data for Go modules to return a version string.

func VersionWithFallbacks

func VersionWithFallbacks(bi *debug.BuildInfo) string

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

type CobraRunFunc func(cmd *cobra.Command, args []string) error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL