venom

package module
v0.0.0-...-4903606 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2018 License: MIT Imports: 27 Imported by: 0

README

venom Build Status

Add some venom to make cobra and viper even more dangerous!

Usage

See _example folder for an example how to use venom. You can also use it as a skeleton for any 12-factor app you plan to use. It won't solve all problems but it will take care to solve the config one i.e. it will allow you to store your configuration in the environment variables.

The twist is that it doesn't require you to store all configuration in the environment variables. It is up to you to define how exactly you want to configure the application. It is even OK to mix however you want:

Priority

If you use TwelveFactorCmd then here is the priority of resolution (highest to lowest):

  • $ example --foo 42
  • env as flags
  • $ example -e EXAMPLE_FOO 42
  • $ example --env-file example.env # (assuming it has EXAMPLE_FOO=42 line)
  • env as env
  • $ EXAMPLE_ENV=EXAMPLE_FOO=42 ./bin/example
  • $ EXAMPLE_ENV_FILE=example.env ./bin/example
  • $ EXAMPLE_FOO=42 example

You probably should not use env as env trick as it is very confusing for any user.

Autoflags

It is possible to ask venom to define flags for you. You need to provide a struct or pointer to struct that has special flag tag set e.g.

type Config struct {
	FooMoo int `flag:"foo-moo,m,Some mooness must be set"`
}

This will allow venom to find this tag and parse long flag name, short flag name and the description. It expect you to define it as a comma separated triplet. It has some logic to deduce what you meant in case you have use only one or two comma separated values.

To define flags you simply run DefineFlags. Note that in this case all flags will have default values set to zero values for their types i.e. 0 for int, "" for string, false for bool etc.:

flags := venom.DefineFlags(Config{})
RootCmd.PersistentFlags().AddFlagSet(flags)

Defaults

You can not only define flags by a special struct but also the default values for these flags. Keep in mind that this works for simple cases (int, unit, string, bool) and probably fails for the rest. It implements only a very minimal subset of what pflags/cobra/viper are capable of - fix what you miss:

type Config struct {
	FooMoo int `flag:"foo-moo,m,Some mooness must be set"`
}

To define default values simply override zero values:

defaults := Config {
    FooMoo: 42,
}
flags := venom.DefineFlags(defaults)
RootCmd.PersistentFlags().AddFlagSet(flags)

Documentation

Index

Constants

View Source
const (
	SquashFlagsTag = "++"
)

Variables

View Source
var (
	LogLevels  = []string{}
	LogFormats = []string{"text", "json"}

	DefaultTimestampFormat = "2006-01-02 15:04:05.000"
)
View Source
var (
	JSONFormat = Format("json")
	YAMLFormat = Format("yaml")

	InputFormats = []Format{
		JSONFormat,
		YAMLFormat,
	}
	OutputFormats = []Format{
		JSONFormat,
		YAMLFormat,
	}

	DefaultInputFormat  = YAMLFormat
	DefaultOutputFormat = YAMLFormat
)
View Source
var (
	DefaultTimeFormat = time.RFC3339
)

Functions

func AbortAbortAbort

func AbortAbortAbort()

func AppendErr

func AppendErr(err error, errs ...error) error

Same as multierror.Append but takes extra care to not create non-nil multierror.Error object with no errors.n

multierror.Append(nil, nil) -> &Error{} AppendErr(nil, nil) -> nil

func AutoFlagsTwelveFactorCmd

func AutoFlagsTwelveFactorCmd(name string, cmd *cobra.Command, defaults interface{}, viperMaybe ...*viper.Viper) error

Version of TwelveFactorCmd that parses flags from the provided defaults.

func AutomaticEnv

func AutomaticEnv(flags *pflag.FlagSet, v *viper.Viper)

Better version of viper.AutomaticEnv that searches FOO_BAR for every --foo-bar key in addition (?) to the default FOO-BAR.

Note that it must be called *after* all flags are added.

func Decode

func Decode(out Config, cfg *viper.Viper) error

Decode config from viper and self-validate.

func DefineFlags

func DefineFlags(defaults interface{}) (*pflag.FlagSet, error)

Define new flags based on the provided defaults.

func ListenAndServe

func ListenAndServe(addr string) error

func ListenAndServeMetrics

func ListenAndServeMetrics(name string, v *viper.Viper) error

func MustDefineFlags

func MustDefineFlags(defaults interface{}) *pflag.FlagSet

Define new flags based on the provided defaults.

It panics if something goes wrong.

func NopReadCloser

func NopReadCloser(r io.Reader) io.ReadCloser

NopCloser returns a ReadCloser with a no-op Close method wrapping the provided Reader r.

func NopWriteCloser

func NopWriteCloser(w io.Writer) io.WriteCloser

NopCloser returns a WriteCloser with a no-op Close method wrapping the provided Writer w.

func PrettyField

func PrettyField(in interface{}, format Format) string

Simple wrapper around any object that could be used in logrus e.g.

logrus.WithField("plan", venom.PrettyField(plan)).Info("Here is the plan")

func QuitQuitQuit

func QuitQuitQuit()

func ReadObject

func ReadObject(r io.Reader, format Format, out interface{}) error

func ReadObjectFrom

func ReadObjectFrom(path string, out interface{}) error

func TwelveFactor

func TwelveFactor(name string, flags *pflag.FlagSet, viperMaybe ...*viper.Viper) error

12-factor setup for viper-backed application.

Most important it will check environment variables for all flags in the provided flag set.

func TwelveFactorCmd

func TwelveFactorCmd(name string, cmd *cobra.Command, flags *pflag.FlagSet, viperMaybe ...*viper.Viper) error

Configure common flags and environment config considered (by me) as a good approach to _bootstrap_ any 12-factor app.

Note that we add some extra flags & alter PreRunE value.

func Unmarshal

func Unmarshal(out interface{}, cfg *viper.Viper) error

Same as viper.Unmarshal but with support for string slices, time etc.

func WriteObject

func WriteObject(in interface{}, format Format, w io.Writer) error

func WriteObjectTo

func WriteObjectTo(in interface{}, path string) error

Types

type Config

type Config interface {
	Valid() error
}

Config structure that is able to self-validate.

type Format

type Format string

type Func

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

func CronRunE

func CronRunE(runE Func, v *viper.Viper) Func

type GetCronFunc

type GetCronFunc func() *cron.Cron

type HasFlags

type HasFlags interface {
	Flags() *pflag.FlagSet
}

Structures implementing this interface won't be introspected and this function will be called instead.

Directories

Path Synopsis
_example

Jump to

Keyboard shortcuts

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