cobrautil

package module
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2022 License: Apache-2.0 Imports: 31 Imported by: 7

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

func HTTPServerFromFlags(cmd *cobra.Command, flagPrefix string) *http.Server

HTTPServerFromFlags creates an *http.Server as configured by the flags from RegisterHttpServerFlags().

func IsBuiltinCommand added in v0.0.4

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 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 added in v0.0.5

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

func RegisterOpenTelemetryFlags(flags *pflag.FlagSet, flagPrefix, serviceName string)

RegisterOpenTelemetryFlags adds the following flags for use with OpenTelemetryPreRunE: - "$PREFIX-provider" - "$PREFIX-endpoint" - "$PREFIX-service-name"

func RegisterVersionFlags added in v0.0.9

func RegisterVersionFlags(flags *pflag.FlagSet)

RegisterVersionFlags registers the flags used for the VersionRunFunc.

func RegisterZeroLogFlags

func RegisterZeroLogFlags(flags *pflag.FlagSet, flagPrefix string)

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

func UsageVersion(programName string, includeDeps bool) string

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

func VersionWithFallbacks added in v0.0.12

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

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:

Jump to

Keyboard shortcuts

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