env

package module
v0.0.0-...-a3ec8ef Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2019 License: MIT Imports: 3 Imported by: 0

README

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
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(env string, def bool) bool

Bool will return true when the environment applies to the strconv true rules, it returns false if it applies to the strconv false rules. It will return the default value if none of these apply and the notifier will be notified.

func Duration

func Duration(env string, def time.Duration) time.Duration

Duration will try and parse the duration given in the environment variable. If the variable is not set or not a valid duration, the notifier will be notified and the default value will be returned.

func Float32

func Float32(env string, def float32) float32

Float32 will try and parse the value given in the environment variable as an float32. If the variable is not set or the value is not a valid float32, the notifier will be notified and the default value will be returned.

func Float64

func Float64(env string, def float64) float64

Float64 will try and parse the value given in the environment variable as an float64. If the variable is not set or the value is not a valid float64, the notifier will be notified and the default value will be returned.

func Int

func Int(env string, def int) int

Int will try and parse the value given in the environment variable as an int. If the variable is not set or the value is not a valid int, the notifier will be notified and the default value will be returned.

func Int32

func Int32(env string, def int32) int32

Int32 will try and parse the value given in the environment variable as an int32. If the variable is not set or the value is not a valid int32, the notifier will be notified and the default value will be returned.

func Int64

func Int64(env string, def int64) int64

Int64 will try and parse the value given in the environment variable as an int64. If the variable is not set or the value is not a valid int64, the notifier will be notified and the default value will be returned.

func String

func String(env, def string) string

String will return the environment variable when it is not empty. If the value is empty (or not set), the notifier will be notified and the default value will be returned.

func UseNotifier

func UseNotifier(n Notifier)

UseNotifier sets the notifier being used for the package to the given notifier. The default notifier is a nil notifier which does nothing with the notifications.

Types

type Notifier

type Notifier interface {
	Notify(msg string, i ...interface{})
}

Notifier is an interface used when getter methods encounter an error and should the application. The getters will always notify the notifier upon an error and return a fitting value.

Directories

Path Synopsis
notifier

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL