Documentation ¶
Index ¶
- Variables
- func GrpcListenFromFlags(cmd *cobra.Command, flagPrefix string, srv *grpc.Server, level zerolog.Level) error
- func GrpcServerFromFlags(cmd *cobra.Command, flagPrefix string, opts ...grpc.ServerOption) (*grpc.Server, error)
- func HTTPListenFromFlags(cmd *cobra.Command, flagPrefix string, srv *http.Server, level zerolog.Level) error
- func HTTPServerFromFlags(cmd *cobra.Command, flagPrefix string) *http.Server
- func IsBuiltinCommand(cmd *cobra.Command) bool
- 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 RegisterGrpcServerFlags(flags *pflag.FlagSet, flagPrefix, serviceName, defaultAddr string, ...)
- func RegisterHTTPServerFlags(flags *pflag.FlagSet, flagPrefix, serviceName, defaultAddr string, ...)
- func RegisterOpenTelemetryFlags(flags *pflag.FlagSet, flagPrefix, serviceName string)
- func RegisterVersionFlags(flags *pflag.FlagSet)
- func RegisterZeroLogFlags(flags *pflag.FlagSet, flagPrefix string)
- func UsageVersion(programName string, includeDeps bool) string
- func VersionWithFallbacks(bi *debug.BuildInfo) string
- type CobraRunFunc
- func CommandStack(cmdfns ...CobraRunFunc) CobraRunFunc
- func OpenTelemetryRunE(flagPrefix string, prerunLevel zerolog.Level) CobraRunFunc
- func SyncViperPreRunE(prefix string) CobraRunFunc
- func VersionRunFunc(programName string) CobraRunFunc
- func ZeroLogRunE(flagPrefix string, prerunLevel zerolog.Level) CobraRunFunc
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 GrpcListenFromFlags ¶ added in v0.0.6
func GrpcListenFromFlags(cmd *cobra.Command, flagPrefix string, srv *grpc.Server, level zerolog.Level) error
GrpcListenFromFlags listens on an gRPC server using the configuration stored in the cobra command that was registered with RegisterGrpcServerFlags.
func GrpcServerFromFlags ¶ added in v0.0.3
func GrpcServerFromFlags(cmd *cobra.Command, flagPrefix string, opts ...grpc.ServerOption) (*grpc.Server, error)
GrpcServerFromFlags creates an *grpc.Server as configured by the flags from RegisterGrpcServerFlags().
func HTTPListenFromFlags ¶ added in v0.0.8
func HTTPListenFromFlags(cmd *cobra.Command, flagPrefix string, srv *http.Server, level zerolog.Level) error
HTTPListenFromFlags listens on an HTTP server using the configuration stored in the cobra command that was registered with RegisterHttpServerFlags.
func HTTPServerFromFlags ¶ added in v0.0.8
HTTPServerFromFlags creates an *http.Server as configured by the flags from RegisterHttpServerFlags().
func IsBuiltinCommand ¶ added in v0.0.4
IsBuiltinCommand checks against a hard-coded list of the names of commands that cobra provides out-of-the-box.
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 ¶ added in v0.0.5
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 RegisterGrpcServerFlags ¶ added in v0.0.3
func RegisterGrpcServerFlags(flags *pflag.FlagSet, flagPrefix, serviceName, defaultAddr string, defaultEnabled bool)
RegisterGrpcServerFlags adds the following flags for use with GrpcServerFromFlags: - "$PREFIX-addr" - "$PREFIX-tls-cert-path" - "$PREFIX-tls-key-path" - "$PREFIX-max-conn-age"
func RegisterHTTPServerFlags ¶ added in v0.0.8
func RegisterHTTPServerFlags(flags *pflag.FlagSet, flagPrefix, serviceName, defaultAddr string, defaultEnabled bool)
RegisterHTTPServerFlags adds the following flags for use with HttpServerFromFlags: - "$PREFIX-addr" - "$PREFIX-tls-cert-path" - "$PREFIX-tls-key-path" - "$PREFIX-enabled"
func RegisterOpenTelemetryFlags ¶ added in v0.0.3
RegisterOpenTelemetryFlags adds the following flags for use with OpenTelemetryPreRunE: - "$PREFIX-provider" - "$PREFIX-endpoint" - "$PREFIX-service-name"
func RegisterVersionFlags ¶ added in v0.0.9
RegisterVersionFlags registers the flags used for the VersionRunFunc.
func RegisterZeroLogFlags ¶
RegisterZeroLogFlags adds flags for use in with ZeroLogPreRunE: - "$PREFIX-level" - "$PREFIX-format"
Example ¶
package main import ( "github.com/rs/zerolog" "github.com/spf13/cobra" "github.com/jzelinskie/cobrautil" ) func main() { cmd := &cobra.Command{ Use: "mycmd", PreRunE: cobrautil.ZeroLogRunE("log", zerolog.InfoLevel), } cobrautil.RegisterZeroLogFlags(cmd.PersistentFlags(), "log") }
Output:
func UsageVersion ¶ added in v0.0.9
UsageVersion introspects the process debug data for Go modules to return a version string.
func VersionWithFallbacks ¶ added in v0.0.12
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
RunFuncStack chains together a collection of CobraCommandFuncs into one.
Example ¶
package main import ( "github.com/rs/zerolog" "github.com/spf13/cobra" "github.com/jzelinskie/cobrautil" ) func main() { cmd := &cobra.Command{ Use: "mycmd", PreRunE: cobrautil.CommandStack( cobrautil.SyncViperPreRunE("myprogram"), cobrautil.ZeroLogRunE("log", zerolog.InfoLevel), ), } cobrautil.RegisterZeroLogFlags(cmd.PersistentFlags(), "log") }
Output:
func OpenTelemetryRunE ¶ added in v0.0.8
func OpenTelemetryRunE(flagPrefix string, prerunLevel zerolog.Level) CobraRunFunc
OpenTelemetryRunE returns a Cobra run func that configures the corresponding otel provider from a command.
The required flags can be added to a command by using RegisterOpenTelemetryFlags().
func SyncViperPreRunE ¶
func SyncViperPreRunE(prefix string) CobraRunFunc
SyncViperPreRunE returns a Cobra run func that synchronizes Viper environment flags prefixed with the provided argument.
Thanks to Carolyn Van Slyck: https://github.com/carolynvs/stingoftheviper
func VersionRunFunc ¶ added in v0.0.9
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.
func ZeroLogRunE ¶ added in v0.0.8
func ZeroLogRunE(flagPrefix string, prerunLevel zerolog.Level) CobraRunFunc
ZeroLogRunE returns a Cobra run func that configures the corresponding log level from a command.
The required flags can be added to a command by using RegisterLoggingPersistentFlags().
Example ¶
package main import ( "github.com/rs/zerolog" "github.com/spf13/cobra" "github.com/jzelinskie/cobrautil" ) func main() { cmd := &cobra.Command{ Use: "mycmd", PreRunE: cobrautil.ZeroLogRunE("log", zerolog.InfoLevel), } cobrautil.RegisterZeroLogFlags(cmd.PersistentFlags(), "log") }
Output: