Git Product home page Git Product logo

go-env's Introduction

API Documentation MIT License

See the Godoc for usage.

Package env handles environment variables in a structured manner. It uses reflection to set fields on a struct pointer passed to the main public method, Populate.

Example (from the test):

type testEnv struct {
	RequiredString string `env:"REQUIRED_STRING,required"`
	OptionalString string `env:"OPTIONAL_STRING"`
	OptionalInt    int    `env:"OPTIONAL_INT"`
	OptionalBool   bool   `env:"OPTIONAL_BOOL"`
	OptionalStruct struct {
		StructOptionalInt int `env:"STRUCT_OPTIONAL_INT"`
	} `env:"OPTIONAL_STRUCT,match=FOO"`
	OptionalStructTwo struct {
		StructOptionalInt int `env:"STRUCT_OPTIONAL_INT"`
	} `env:"OPTIONAL_STRUCT_TWO,match=^(FOO|BAR)$"`
}

To populate this, would would call:

testEnv := &testEnv{}
if err := env.Populate(env, PopulateOptionsP{}); err != nil {
	return err
}

Non-struct fields have the name of the environment variable, optionally followed by a "required" option, which will throw an error if the environment variable is not set. Struct fields have the name of an environment variable, (not) optionally followed at a "match=REGEX" option, which says that if the value of the environment variable matches the regex, set the fields within the struct. This is useful for situations such as:

type env struct {
	QueueType string `env:"QUEUE_TYPE,required"`
	Sqs struct {
		AwsRegion string `env:"AWS_REGION,required"`
	} `env:"QUEUE_TYPE,match=^SQS$"`
	Redis struct {
		DatabaseURL string `env:"DATABASE_URL,required"`
	} `env:"QUEUE_TYPE,match=^REDIS$"`
}

PopulateOptions has two fields:

  • RestrictTo: this is a list of all allowed environment variables on structs. This is useful if you have a bunch of mini-services within a repo, and you want to record all variables that are used across your services in one comment place in code.
  • Decoders: you can set additional Decoders (both an env file decoder and a JSON decoder are provided) to provider additional environment variables on top of those set at the system level. Decoders overwrite system level values, and Decoders later in the slice override those earlier in the slice.

go-env's People

Contributors

bufdev avatar

Watchers

 avatar  avatar

Forkers

kleopatra999

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.