All Projects → vrischmann → Envconfig

vrischmann / Envconfig

Licence: mit
Small library to read your configuration from environment variables

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Envconfig

Toml
A PHP parser for TOML
Stars: ✭ 152 (-21.65%)
Mutual labels:  configuration
Dot Hammerspoon
My personal Hammerspoon configuration - mirrored from GitLab
Stars: ✭ 165 (-14.95%)
Mutual labels:  configuration
Nestedtext
Human Readable and Writable Data Interchange Format
Stars: ✭ 187 (-3.61%)
Mutual labels:  configuration
Libelektra
Elektra serves as a universal and secure framework to access configuration parameters in a global, hierarchical key database.
Stars: ✭ 155 (-20.1%)
Mutual labels:  configuration
Configs
Scala wrapper for Typesafe config
Stars: ✭ 164 (-15.46%)
Mutual labels:  configuration
Glow
mpv Config File Generator for Windows
Stars: ✭ 167 (-13.92%)
Mutual labels:  configuration
Env
Simple lib to parse environment variables to structs
Stars: ✭ 2,164 (+1015.46%)
Mutual labels:  configuration
Go Flagz
Dynamic flag management for Go.
Stars: ✭ 191 (-1.55%)
Mutual labels:  configuration
Apollo.net
Apollo配置中心.Net客户端
Stars: ✭ 165 (-14.95%)
Mutual labels:  configuration
Microconfig
Modern tool for microservice configuration management
Stars: ✭ 180 (-7.22%)
Mutual labels:  configuration
Magento2 Configurator
Magento 2 Configurator
Stars: ✭ 158 (-18.56%)
Mutual labels:  configuration
Color Scheme
color-scheme,颜色方案,color主题,scheme,webstrom,phpstrom,pytharm,配色方案,编辑器,xfce4主题,护眼,暗色,dark
Stars: ✭ 162 (-16.49%)
Mutual labels:  configuration
Config
Various program configuration files and scripts
Stars: ✭ 173 (-10.82%)
Mutual labels:  configuration
Dotter
A dotfile manager and templater written in rust 🦀
Stars: ✭ 154 (-20.62%)
Mutual labels:  configuration
Aconfig
Simple, useful and opinionated config loader.
Stars: ✭ 187 (-3.61%)
Mutual labels:  configuration
Qconf
Qihoo Distributed Configuration Management System
Stars: ✭ 1,843 (+850%)
Mutual labels:  configuration
Simple Settings
A simple way to manage your project settings.
Stars: ✭ 165 (-14.95%)
Mutual labels:  configuration
Golib
Go Library [DEPRECATED]
Stars: ✭ 194 (+0%)
Mutual labels:  configuration
Knobs
A reasonable configuration library for Scala
Stars: ✭ 188 (-3.09%)
Mutual labels:  configuration
Prettyconf
A extensible library for Settings/Code separation
Stars: ✭ 173 (-10.82%)
Mutual labels:  configuration

envconfig

CI Go Reference

envconfig is a library which allows you to parse your configuration from environment variables and fill an arbitrary struct.

See the example to understand how to use it, it's pretty simple.

Supported types

  • Almost all standard types plus time.Duration are supported by default.
  • Slices and arrays
  • Arbitrary structs
  • Custom types via the Unmarshaler interface.

How does it work

envconfig takes the hierarchy of your configuration struct and the names of the fields to create a environment variable key.

For example:

var conf struct {
    Name string
    Shard struct {
        Host string
        Port int
    }
}

This will check for those 3 keys:

  • NAME or name
  • SHARD_HOST, or shard_host
  • SHARD_PORT, or shard_port

Flexible key naming

envconfig supports having underscores in the key names where there is a word boundary. Now, that term is not super explicit, so let me show you an example:

var conf struct {
    Cassandra struct {
        SSLCert string
        SslKey string
    }
}

This will check all of the following keys:

  • CASSANDRA_SSL_CERT, CASSANDRA_SSLCERT, cassandra_ssl_cert, cassandra_sslcert
  • CASSANDRA_SSL_KEY, CASSANDRA_SSLKEY, cassandra_ssl_key, cassandra_sslkey

If that is not good enough, look just below.

Custom environment variable names

envconfig supports custom environment variable names:

var conf struct {
    Name string `envconfig:"myName"`
}

Default values

envconfig supports default values:

var conf struct {
    Name string `envconfig:"default=Vincent"`
}

Optional values

envconfig supports optional values:

var conf struct {
    Name string `envconfig:"optional"`
}

Skipping fields

envconfig supports skipping struct fields:

var conf struct {
    Internal string `envconfig:"-"`
}

Combining multiple options in one tag

You can of course combine multiple options:

var conf struct {
    Name string `envconfig:"default=Vincent,myName"`
}

Slices or arrays

With slices or arrays, the same naming is applied for the slice. To put multiple elements into the slice or array, you need to separate them with a , (will probably be configurable in the future, or at least have a way to escape)

For example:

var conf struct {
    Ports []int
}

This will check for the key PORTS:

  • if your variable is 9000 the slice will contain only 9000
  • if your variable is 9000,100 the slice will contain 9000 and 100

For slices of structs, it's a little more complicated. The same splitting of slice elements is done with a comma, however, each token must follow a specific format like this: {<first field>,<second field>,...}

For example:

var conf struct {
    Shards []struct {
        Name string
        Port int
    }
}

This will check for the key SHARDS. Example variable content: {foobar,9000},{barbaz,20000}

This will result in two struct defined in the Shards slice.

If you want to set default value for slice or array, you have to use ; as separator, instead of ,:

var conf struct {
    Ports []int `envconfig:"default=9000;100"`
}

Same for slices of structs:

var conf struct {
    Shards []struct {
        Name string
        Port int
    } `envconfig:"default={foobar;localhost:2929};{barbaz;localhost:2828}"`
}

Development state

I consider envconfig to be pretty much done.

It has been used extensively at Batch for more than 5 years now without much problems, with no need for new features either.

So, while I will keep maintaining this library (fixing bugs, making it compatible with new versions of Go and so on) for the foreseeable future, I don't plan on adding new features.

But I'm open to discussion so if you have a need for a particular feature we can discuss it.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].