All Projects → joeshaw → Envdecode

joeshaw / Envdecode

Licence: mit
Go package for populating structs from environment variables using struct tags

Programming Languages

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

Projects that are alternatives of or similar to Envdecode

Env Providers
👷 Load Laravel service providers based on your application's environment.
Stars: ✭ 73 (-58.05%)
Mutual labels:  environment-variables
Cra Runtime Environment Variables
Guide: Runtime environment variables within Create React App
Stars: ✭ 111 (-36.21%)
Mutual labels:  environment-variables
Fig
A minimalist Go configuration library
Stars: ✭ 142 (-18.39%)
Mutual labels:  environment-variables
Envinject Plugin
This plugin makes it possible to setup a custom environment for your jobs
Stars: ✭ 74 (-57.47%)
Mutual labels:  environment-variables
Startup
🔧 R package: startup - Friendly R Startup Configuration
Stars: ✭ 107 (-38.51%)
Mutual labels:  environment-variables
Config
A lightweight yet powerful config package for Go projects
Stars: ✭ 126 (-27.59%)
Mutual labels:  environment-variables
Senv
Friends don't let friends leak secrets on their terminal window 🙈
Stars: ✭ 71 (-59.2%)
Mutual labels:  environment-variables
Env
Simple lib to parse environment variables to structs
Stars: ✭ 2,164 (+1143.68%)
Mutual labels:  environment-variables
Fsconfig
FsConfig is a F# library for reading configuration data from environment variables and AppSettings with type safety.
Stars: ✭ 108 (-37.93%)
Mutual labels:  environment-variables
Node Convict
Featureful configuration management library for Node.js
Stars: ✭ 1,855 (+966.09%)
Mutual labels:  environment-variables
Phoenix gon
🔥 Phoenix variables in your JavaScript without headache.
Stars: ✭ 84 (-51.72%)
Mutual labels:  environment-variables
Flex Env
🌳 Manage your .env file in Laravel projects through artisan
Stars: ✭ 95 (-45.4%)
Mutual labels:  environment-variables
Envy
😠 Environmentally friendly environment variables
Stars: ✭ 132 (-24.14%)
Mutual labels:  environment-variables
Env
Simple library to read environment variables and convert to simple types.
Stars: ✭ 73 (-58.05%)
Mutual labels:  environment-variables
Phpdotenv
Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
Stars: ✭ 11,648 (+6594.25%)
Mutual labels:  environment-variables
Dotenv Java
🗝️ Dotenv is a no-dep, pure Java module that loads environment variables from a .env file
Stars: ✭ 72 (-58.62%)
Mutual labels:  environment-variables
Dynaconf
Configuration Management for Python ⚙
Stars: ✭ 2,082 (+1096.55%)
Mutual labels:  environment-variables
Nuxt Env
Inject env vars for your Nuxt app at runtime
Stars: ✭ 169 (-2.87%)
Mutual labels:  environment-variables
Envy
Envy automatically exposes environment variables for all of your Go flags
Stars: ✭ 150 (-13.79%)
Mutual labels:  environment-variables
Python Decouple
Strict separation of config from code.
Stars: ✭ 1,982 (+1039.08%)
Mutual labels:  environment-variables

envdecode Travis-CI GoDoc

envdecode is a Go package for populating structs from environment variables.

envdecode uses struct tags to map environment variables to fields, allowing you you use any names you want for environment variables. envdecode will recurse into nested structs, including pointers to nested structs, but it will not allocate new pointers to structs.

API

Full API docs are available on godoc.org.

Define a struct with env struct tags:

type Config struct {
    Hostname  string `env:"SERVER_HOSTNAME,default=localhost"`
    Port      uint16 `env:"SERVER_PORT,default=8080"`

    AWS struct {
        ID        string   `env:"AWS_ACCESS_KEY_ID"`
        Secret    string   `env:"AWS_SECRET_ACCESS_KEY,required"`
        SnsTopics []string `env:"AWS_SNS_TOPICS"`
    }

    Timeout time.Duration `env:"TIMEOUT,default=1m,strict"`
}

Fields must be exported (i.e. begin with a capital letter) in order for envdecode to work with them. An error will be returned if a struct with no exported fields is decoded (including one that contains no env tags at all). Default values may be provided by appending ",default=value" to the struct tag. Required values may be marked by appending ",required" to the struct tag. Strict values may be marked by appending ",strict" which will return an error on Decode if there is an error while parsing.

Then call envdecode.Decode:

var cfg Config
err := envdecode.Decode(&cfg)

If you want all fields to act strict, you may use envdecode.StrictDecode:

var cfg Config
err := envdecode.StrictDecode(&cfg)

All parse errors will fail fast and return an error in this mode.

Supported types

  • Structs (and pointer to structs)
  • Slices of below defined types, separated by semicolon
  • bool
  • float32, float64
  • int, int8, int16, int32, int64
  • uint, uint8, uint16, uint32, uint64
  • string
  • time.Duration, using the time.ParseDuration() format
  • *url.URL, using url.Parse()
  • Types those implement a Decoder interface

Custom Decoder

If you want a field to be decoded with custom behavior, you may implement the interface Decoder for the filed type.

type Config struct {
  IPAddr IP `env:"IP_ADDR"`
}

type IP net.IP

// Decode implements the interface `envdecode.Decoder`
func (i *IP) Decode(repl string) error {
  *i = net.ParseIP(repl)
  return nil
}

Decoder is the interface implemented by an object that can decode an environment variable string representation of itself.

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