Documentation ¶
Index ¶
- Constants
- Variables
- func ContextWithLogTag(ctx context.Context, key string, val interface{}) context.Context
- func DefaultMessageWriter(ctx context.Context, logger *log.Logger, level string, message string, ...)
- func FormatJSON(v interface{}) (string, error)
- func FormatJSONWithFilter(v interface{}, filter string) (out string, err error)
- func GetOptions(a interface{}, cfg *viper.Viper) errordeprecated
- func HandleError(cmd *cobra.Command, err error, prefixes ...string)
- func HasSpace(s string) bool
- func InitConfig(envPrefix string) (c *viper.Viper)
- func IsLetters(s string) bool
- func IsNumber(s string) bool
- func LogLevel(level string) (id int)
- func LogLevelValid(level string) (ok bool)
- func ParseIntSlice(s string) ([]int, error)
- func ParseKeyValue(s string) map[string]string
- func PrintJSON(v interface{}) (err error)
- func PrintJSONWithFilter(v interface{}, filter string) error
- func ReadOptions(a interface{}, cfg *viper.Viper) (err error)
- func RegisterOptionTypeFunc(name string, fn OptionTypeFunc)
- func Sequence(start, end int) []int
- func SetBoolValue(cfg *viper.Viper, name string, v *bool)
- func SetFloat64Value(cfg *viper.Viper, name string, v *float64)
- func SetIntSliceValue(cfg *viper.Viper, name string, v *[]int)
- func SetIntValue(cfg *viper.Viper, name string, v *int)
- func SetOptionMetadata(name string, meta map[string]string)
- func SetStringValue(cfg *viper.Viper, name string, v *string)
- func Use(command string, args ...string) (use string)
- func WriteError(w io.Writer, err error, prefixes ...string)
- type BoolOption
- type BoolStringOption
- type EventListener
- type Flagger
- func (f *Flagger) Bool(name, shorthand string, value bool, usage string)
- func (f *Flagger) Float64(name, shorthand string, value float64, usage string)
- func (f *Flagger) Int(name, shorthand string, value int, usage string)
- func (f *Flagger) IntSlice(name, shorthand string, value []int, usage string)
- func (f *Flagger) PersistentBool(name, shorthand string, value bool, usage string)
- func (f *Flagger) PersistentFloat64(name, shorthand string, value float64, usage string)
- func (f *Flagger) PersistentInt(name, shorthand string, value int, usage string)
- func (f *Flagger) PersistentIntSlice(name, shorthand string, value []int, usage string)
- func (f *Flagger) PersistentString(name, shorthand, value, usage string)
- func (f *Flagger) SetOptions(a interface{}) error
- func (f *Flagger) String(name, shorthand, value, usage string)
- type Float64Option
- type IOReaderOption
- type IntOption
- type IntSliceOption
- type KeyValueOption
- type LeveledLogger
- func (l LeveledLogger) Debug(ctx context.Context, message string)
- func (l LeveledLogger) Error(ctx context.Context, message string, err error)
- func (l LeveledLogger) ErrorIfError(ctx context.Context, message string, err error)
- func (l LeveledLogger) Fatal(ctx context.Context, message string, err error)
- func (l LeveledLogger) FatalIfError(ctx context.Context, message string, err error)
- func (l LeveledLogger) Info(ctx context.Context, message string)
- func (l LeveledLogger) Notice(ctx context.Context, message string)
- func (l *LeveledLogger) SetFlags(flag int)
- func (l *LeveledLogger) SetLevel(level string)
- func (l *LeveledLogger) SetMessageWriter(fn MessageWriter)
- func (l *LeveledLogger) SetOutput(w io.Writer)
- func (l *LeveledLogger) SetPrefix(prefix string)
- type MessageWriter
- type OptionType
- func NewBoolOption(tag map[string]string) OptionType
- func NewBoolStringOption(tag map[string]string) OptionType
- func NewFloat64Option(tag map[string]string) OptionType
- func NewIOReaderOption(tag map[string]string) OptionType
- func NewIntOption(tag map[string]string) OptionType
- func NewIntSliceOption(tag map[string]string) OptionType
- func NewKeyValueOption(tag map[string]string) OptionType
- func NewStdinOption(tag map[string]string) OptionType
- func NewStringOption(tag map[string]string) OptionType
- type OptionTypeFunc
- type StdinOption
- type StringOption
Examples ¶
Constants ¶
const ( LogNone = "none" LogFatal = "fatal" LogError = "error" LogNotice = "notice" LogInfo = "info" LogDebug = "debug" )
Log* constants represent the log levels as strings for configuration.
const ( LogLevelNone = iota LogLevelFatal LogLevelError LogLevelNotice LogLevelInfo LogLevelDebug )
LogLevel* represents log levels as integers for comparrison.
const (
CtxLogTags ctxKey = iota
)
Ctx* constants contain the keys for contexts with values.
const LogTagTransactionID = "transid"
LogTagTransactionID is the log tag that contains the transaction ID.
const TagName = "cliutil"
TagName is the name of the tag.
Variables ¶
var ( ErrStructRequired = errors.New("value must be a struct") ErrTypeNotSupported = errors.New("type not supported") ErrFuncNotRegistered = errors.New("option type func not registered") ErrZeroValue = errors.New("value is a zero value for its type") )
Err* variables contain comon errors.
Functions ¶
func ContextWithLogTag ¶
ContextWithLogTag returns a new context with log tags appended.
func DefaultMessageWriter ¶
func DefaultMessageWriter(ctx context.Context, logger *log.Logger, level string, message string, err error)
DefaultMessageWriter formats log messages according to Splunk's best practices. It is the default MessageWriter.
See https://dev.splunk.com/enterprise/docs/developapps/addsupport/logging/loggingbestpractices/
func FormatJSON ¶
FormatJSON returns pretty-printed JSON as a string.
func FormatJSONWithFilter ¶
FormatJSONWithFilter applies a JMESPath filter and returns pretty-printed JSON as a string and panics on any marshal errors.
func GetOptions
deprecated
func HandleError ¶
HandleError either performs a no-op if err is nil or writes the error plus command usage to os.Stderr and exits with a non-zero status otherwise.
func InitConfig ¶
InitConfig returns a *viper.Viper with an environment variable prefix set so that options can be passed from environment variables.
func LogLevelValid ¶
LogLevelValid return true if the log level is valid.
func ParseIntSlice ¶ added in v0.2.0
ParseIntSlice parses a slice of integers from a string. We expect integers and ranges to be separated by commas, e.g., 1,2,3:5 = []int{1,2,3,4,5}.
func ParseKeyValue ¶
ParseKeyValue parses key value pairs. See https://stackoverflow.com/a/44282136
func PrintJSON ¶
func PrintJSON(v interface{}) (err error)
PrintJSON writes pretty-printed JSON to STDOUT.
Example ¶
package main import ( "github.com/cpliakas/cliutil" ) type JsonData struct { Data string `json:"data"` } func main() { cliutil.PrintJSON(&JsonData{Data: "test"}) }
Output: { "data": "test" }
func PrintJSONWithFilter ¶
PrintJSONWithFilter applies a JMESPath filter and writes pretty-printed JSON to STDOUT.
Example ¶
package main import ( "github.com/cpliakas/cliutil" ) type JsonData struct { Data string `json:"data"` } func main() { cliutil.PrintJSONWithFilter(&JsonData{Data: "test"}, "data") }
Output: "test"
func ReadOptions ¶ added in v0.2.0
ReadOptions reads options from cfg into a.
func RegisterOptionTypeFunc ¶ added in v0.2.0
func RegisterOptionTypeFunc(name string, fn OptionTypeFunc)
RegisterOptionTypeFunc registers an OptionReadSetter by naame.
func Sequence ¶ added in v0.2.2
Sequence returns a sequencce of numbers between start and end as an []int.
func SetBoolValue ¶
SetBoolValue sets s if the name flag is passed.
func SetFloat64Value ¶
SetFloat64Value sets v if the name flag is passed.
func SetIntSliceValue ¶ added in v0.2.0
SetIntSliceValue sets v if the name flag is passed.
func SetIntValue ¶
SetIntValue sets v if the name flag is passed.
func SetOptionMetadata ¶ added in v0.2.3
SetOptionMetadata sets metadata for an option.
Valid keys for meta are: - short - default - usage - func
func SetStringValue ¶
SetStringValue sets v if the name flag is passed.
func WriteError ¶
WriteError formats and writes an error message to io.Writer w. All prefixes are prepended to the error message and separated by a colon plus space (: ). Two new line characters are printed after the error message, as it is assumed that command usage follows the error message.
Types ¶
type BoolOption ¶ added in v0.2.0
type BoolOption struct {
// contains filtered or unexported fields
}
BoolOption implements Option for bool options.
func (*BoolOption) Set ¶ added in v0.2.0
func (opt *BoolOption) Set(f *Flagger) (err error)
Set implements OptionType.Set.
type BoolStringOption ¶ added in v0.2.6
type BoolStringOption struct {
// contains filtered or unexported fields
}
BoolStringOption implements Option for string options.
func (*BoolStringOption) Set ¶ added in v0.2.6
func (opt *BoolStringOption) Set(f *Flagger) (err error)
Set implements OptionType.Set.
type EventListener ¶
type EventListener struct {
// contains filtered or unexported fields
}
EventListener listens for SIGINT and SIGTERM signals and notifies the shutdown channel if it detects that either was sent.
func NewEventListener ¶
func NewEventListener() *EventListener
NewEventListener returns an EventListener with the channels initialized.
func (*EventListener) Run ¶
func (e *EventListener) Run() *EventListener
Run runs the event listener in a goroutine and sends e message to EventListener.shutdown if a SIGINT or SIGTERM signal is detected.
func (*EventListener) StopSignal ¶
func (e *EventListener) StopSignal()
StopSignal stops relaying incoming signals to EventListener.signal.
func (*EventListener) Wait ¶
func (e *EventListener) Wait()
Wait waits for EventListener.shutdown to receive a message.
type Flagger ¶
type Flagger struct {
// contains filtered or unexported fields
}
Flagger is a utility that streamlines adding flags to commands.
func AddCommand ¶
AddCommand adds a comand to it's parent, initializes the configuration, and returns a flagger to easily add options.
func NewFlagger ¶
NewFlagger returns a new Flagger with the *cobra.Command and *viper.Viper set as properties.
func (*Flagger) IntSlice ¶ added in v0.2.0
IntSlice adds a local flag that accepts an integer slice.
func (*Flagger) PersistentBool ¶
PersistentBool adds a persistent flag that accepts a boolean.
func (*Flagger) PersistentFloat64 ¶
PersistentFloat64 adds a persistent flag that accepts a 64-bit float.
func (*Flagger) PersistentInt ¶
PersistentInt adds a persistent flag that accepts an integer.
func (*Flagger) PersistentIntSlice ¶ added in v0.2.0
PersistentIntSlice adds a persistent flag that accepts an integer slice.
func (*Flagger) PersistentString ¶
PersistentString adds a persistent flag that accepts an string.
func (*Flagger) SetOptions ¶
SetOptions sets flags based on the cliutil tag.
type Float64Option ¶ added in v0.2.0
type Float64Option struct {
// contains filtered or unexported fields
}
Float64Option implements Option for float64 options.
func (*Float64Option) Set ¶ added in v0.2.0
func (opt *Float64Option) Set(f *Flagger) (err error)
Set implements OptionType.Set.
type IOReaderOption ¶ added in v0.2.1
type IOReaderOption struct {
// contains filtered or unexported fields
}
IOReaderOption implements Option for string options read from an io.Reader.
func (*IOReaderOption) Set ¶ added in v0.2.1
func (opt *IOReaderOption) Set(f *Flagger) error
Set implements OptionType.Set.
type IntOption ¶ added in v0.2.0
type IntOption struct {
// contains filtered or unexported fields
}
IntOption implements Option for int options.
type IntSliceOption ¶ added in v0.2.0
type IntSliceOption struct {
// contains filtered or unexported fields
}
IntSliceOption implements Option for []int options.
func (*IntSliceOption) Set ¶ added in v0.2.0
func (opt *IntSliceOption) Set(f *Flagger) error
Set implements OptionType.Set.
type KeyValueOption ¶ added in v0.2.4
type KeyValueOption struct {
// contains filtered or unexported fields
}
KeyValueOption implements Option for []int options.
func (*KeyValueOption) Set ¶ added in v0.2.4
func (opt *KeyValueOption) Set(f *Flagger) error
Set implements OptionType.Set.
type LeveledLogger ¶
type LeveledLogger struct {
// contains filtered or unexported fields
}
LeveledLogger is a simple leveled logger that writes logs to STDOUT.
func NewLogger ¶
func NewLogger(level string) *LeveledLogger
NewLogger returns a LeveledLogger that writes logs to either os.Stdout or ioutil.Discard depending on the passed minimum log level.
func NewLoggerWithContext ¶
func NewLoggerWithContext(ctx context.Context, level string) (context.Context, *LeveledLogger, xid.ID)
NewLoggerWithContext returns a leveled logger with a context that is initialized with a unique transaction ID.
func (LeveledLogger) Debug ¶
func (l LeveledLogger) Debug(ctx context.Context, message string)
Debug writes a debug level log.
func (LeveledLogger) Error ¶
func (l LeveledLogger) Error(ctx context.Context, message string, err error)
Error writes an error level log.
func (LeveledLogger) ErrorIfError ¶
func (l LeveledLogger) ErrorIfError(ctx context.Context, message string, err error)
ErrorIfError writes an error level log if err is not nil.
func (LeveledLogger) Fatal ¶
func (l LeveledLogger) Fatal(ctx context.Context, message string, err error)
Fatal writes an fatal level log and exits with a non-zero exit code.
func (LeveledLogger) FatalIfError ¶
func (l LeveledLogger) FatalIfError(ctx context.Context, message string, err error)
FatalIfError writes a fatal level log and exits with a non-zero exit code if err != nil. This function is a no-op if err == nil.
func (LeveledLogger) Info ¶
func (l LeveledLogger) Info(ctx context.Context, message string)
Info writes an info level log.
func (LeveledLogger) Notice ¶
func (l LeveledLogger) Notice(ctx context.Context, message string)
Notice writes an notice level log.
func (*LeveledLogger) SetFlags ¶
func (l *LeveledLogger) SetFlags(flag int)
SetFlags sets the output flags for all loggers.
func (*LeveledLogger) SetLevel ¶
func (l *LeveledLogger) SetLevel(level string)
SetLevel sets the minimum log level. The log level defaults to "info" if the passed log level is not valid.
func (*LeveledLogger) SetMessageWriter ¶
func (l *LeveledLogger) SetMessageWriter(fn MessageWriter)
SetMessageWriter sets the MessageWriter for all loggers.
func (*LeveledLogger) SetOutput ¶
func (l *LeveledLogger) SetOutput(w io.Writer)
SetOutput sets the output for all loggers.
func (*LeveledLogger) SetPrefix ¶
func (l *LeveledLogger) SetPrefix(prefix string)
SetPrefix sets the prefix for all loggers.
type MessageWriter ¶
type MessageWriter func(ctx context.Context, logger *log.Logger, level string, message string, err error)
MessageWriter defines a function the writes the log messages.
type OptionType ¶ added in v0.2.0
type OptionType interface { // Set sets an option as a flag. Set(*Flagger) error // Read reads an option reflect.Value. Read(*viper.Viper, reflect.Value) error }
OptionType is implemented by structs that set and read options.
func NewBoolOption ¶ added in v0.2.0
func NewBoolOption(tag map[string]string) OptionType
NewBoolOption is an OptionTypeFunc that returns a *BoolOption.
func NewBoolStringOption ¶ added in v0.2.6
func NewBoolStringOption(tag map[string]string) OptionType
NewBoolStringOption is an OptionTypeFunc that returns a *BoolStringOption.
func NewFloat64Option ¶ added in v0.2.0
func NewFloat64Option(tag map[string]string) OptionType
NewFloat64Option is an OptionTypeFunc that returns a *Float64Option.
func NewIOReaderOption ¶ added in v0.2.1
func NewIOReaderOption(tag map[string]string) OptionType
NewIOReaderOption is a OptionTypeFunc that returns a *GroupOption.
func NewIntOption ¶ added in v0.2.0
func NewIntOption(tag map[string]string) OptionType
NewIntOption is an OptionTypeFunc that returns an *IntOption.
func NewIntSliceOption ¶ added in v0.2.0
func NewIntSliceOption(tag map[string]string) OptionType
NewIntSliceOption is an OptionTypeFunc that returns an *IntSliceOption.
func NewKeyValueOption ¶ added in v0.2.4
func NewKeyValueOption(tag map[string]string) OptionType
NewKeyValueOption is an OptionTypeFunc that returns an *KeyValueOption.
func NewStdinOption ¶ added in v0.2.3
func NewStdinOption(tag map[string]string) OptionType
NewStdinOption is an OptionTypeFunc that returns a *StdinOption.
func NewStringOption ¶ added in v0.2.0
func NewStringOption(tag map[string]string) OptionType
NewStringOption is an OptionTypeFunc that returns a *StringOption.
type OptionTypeFunc ¶ added in v0.2.0
type OptionTypeFunc func(map[string]string) OptionType
OptionTypeFunc is a definition for functions that return an OptionType.
type StdinOption ¶ added in v0.2.3
type StdinOption struct {
// contains filtered or unexported fields
}
StdinOption implements Option for string options read via stdin.
func (*StdinOption) Set ¶ added in v0.2.3
func (opt *StdinOption) Set(f *Flagger) error
Set implements OptionType.Set.
type StringOption ¶ added in v0.2.0
type StringOption struct {
// contains filtered or unexported fields
}
StringOption implements Option for string options.
func (*StringOption) Set ¶ added in v0.2.0
func (opt *StringOption) Set(f *Flagger) error
Set implements OptionType.Set.