Skip to content

Typed environment variables with default fallbacks.

License

Notifications You must be signed in to change notification settings

jelmersnoeck/env

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Env

Build Status GoDoc

Env makes it easy to get your environment variables in the correct typed format. It also introduces a fallback system in case the environmen variable is not set or is not valid for the requested type.

Supported methods

Have a look at the available methods in the documentation.

Notifiers

Notifiers can be used to take action when something goes wrong. This could be when an environment variabe isn't set or when it doesn't match what we expect it to be, for example when parsing durations.

There are several implementations of notifiers, the most basic one being the nilNotifier which does nothing with the notifications.

Another implementation is the panicNotifier, which will panic when notifications happen, terminating the application.

Usage

Defaults

import (
    "github.com/jelmersnoeck/env"
)

func main() {
    os.Setenv("MY_ENV", "5s") // do this on launching your application

    time.Sleep(env.Duration("MY_ENV", time.Second)) // this will sleep for 5 seconds
    time.Sleep(env.Duration("MY_OTHER_ENV", time.Second)) // this will sleep for a second

    os.Setenv("WRONG_ENV", "hello")
    time.Sleep(env.Duration("WRONG_ENV", time.Minute)) // this will sleep for a minute
}

Panic notifier

import (
    "github.com/jelmersnoeck/env"
    "github.com/jelmersnoeck/env/notifier/panic"
)

func main() {
    env.UseNotifier(panic.NewNotifier())

    os.Setenv("MY_ENV", "5s") // do this on launching your application

    time.Sleep(env.Duration("MY_ENV", time.Second)) // this will sleep for 5 seconds
    time.Sleep(env.Duration("MY_OTHER_ENV", time.Second)) // this will panic because the env is not set

    os.Setenv("WRONG_ENV", "hello")
    time.Sleep(env.Duration("WRONG_ENV", time.Minute)) // this will panic because the env is not a duration
}

About

Typed environment variables with default fallbacks.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages