Giter Site home page Giter Site logo

venom's Introduction

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.