envh: github.com/antham/envh Index | Examples | Files

package envh

import "github.com/antham/envh"

Package envh provides convenient helpers to manage easily your environment variables.

Index

Examples

Package Files

cast.go env.go env_tree.go error.go node.go struct.go var.go

type Env Uses

type Env struct {
    // contains filtered or unexported fields
}

Env manages environment variables by giving a convenient helper to interact with them

func NewEnv Uses

func NewEnv() Env

NewEnv creates a new Env instance

func (Env) FindEntries Uses

func (e Env) FindEntries(reg string) (map[string]string, error)

FindEntries retrieves all keys matching a given regexp and their corresponding values

Code:

os.Clearenv()
setEnv("API_USERNAME", "user")
setEnv("API_PASSWORD", "password")
setEnv("DB_USERNAME", "user")
setEnv("DB_PASSWORD", "user")

env := NewEnv()

entries, err := env.FindEntries("API.*")

fmt.Printf("API -> PASSWORD = %s, API -> USERNAME = %s ", entries["API_PASSWORD"], entries["API_PASSWORD"])
fmt.Println(err)
fmt.Println(env.FindEntries("*"))

Output:

API -> PASSWORD = password, API -> USERNAME = password <nil>
map[] error parsing regexp: missing argument to repetition operator: `*`

func (Env) GetAllKeys Uses

func (e Env) GetAllKeys() []string

GetAllKeys retrieves a slice of all environment variables keys

Code:

os.Clearenv()
setEnv("HELLO", "world")
setEnv("FOO", "bar")

env := NewEnv()

keys := env.GetAllKeys()

sort.Strings(keys)

fmt.Println(keys)

Output:

[FOO HELLO]

func (Env) GetAllValues Uses

func (e Env) GetAllValues() []string

GetAllValues retrieves a slice of all environment variables values

Code:

os.Clearenv()
setEnv("HELLO", "world")
setEnv("FOO", "bar")

env := NewEnv()

values := env.GetAllValues()

sort.Strings(values)

fmt.Println(values)

Output:

[bar world]

func (Env) GetBool Uses

func (e Env) GetBool(key string) (bool, error)

GetBool returns a boolean if variable exists or an error if value is not a boolean or doesn't exist

Code:

os.Clearenv()
setEnv("BOOL", "true")
setEnv("STRING", "TEST")

env := NewEnv()

fmt.Println(env.GetBool("BOOL"))
fmt.Println(env.GetBool("STRING"))

Output:

true <nil>
false Value "TEST" can't be converted to type "bool"

func (Env) GetFloat Uses

func (e Env) GetFloat(key string) (float32, error)

GetFloat returns a float if variable exists or an error if value is not a float or doesn't exist

Code:

os.Clearenv()
setEnv("FLOAT", "1.1")
setEnv("STRING", "TEST")

env := NewEnv()

f, err := env.GetFloat("FLOAT")

fmt.Printf("%0.1f ", f)
fmt.Println(err)
fmt.Println(env.GetFloat("STRING"))

Output:

1.1 <nil>
0 Value "TEST" can't be converted to type "float"

func (Env) GetInt Uses

func (e Env) GetInt(key string) (int, error)

GetInt returns an integer if variable exists or an error if value is not an integer or doesn't exist

Code:

os.Clearenv()
setEnv("INT", "1")
setEnv("STRING", "TEST")

env := NewEnv()

fmt.Println(env.GetInt("INT"))
fmt.Println(env.GetInt("STRING"))

Output:

1 <nil>
0 Value "TEST" can't be converted to type "int"

func (Env) GetString Uses

func (e Env) GetString(key string) (string, error)

GetString returns a string if variable exists or an error otherwise

Code:

os.Clearenv()
setEnv("HELLO", "world")

env := NewEnv()

fmt.Println(env.GetString("HELLO"))

Output:

world <nil>

type EnvTree Uses

type EnvTree struct {
    // contains filtered or unexported fields
}

EnvTree manages environment variables through a tree structure to store a config the same way as in a yaml file or whatever format allows to store a config hierarchically

func NewEnvTree Uses

func NewEnvTree(reg string, delimiter string) (EnvTree, error)

NewEnvTree creates an environment variable tree. A delimiter is used to split key, reg is a regexp used to filter entries

func (EnvTree) FindBool Uses

func (e EnvTree) FindBool(keyChain ...string) (bool, error)

FindBool returns a boolean if key chain exists or an error if value is not a boolean or doesn't exist

Code:

os.Clearenv()
setEnv("ENVH_DB_USERNAME", "foo")
setEnv("ENVH_DB_PASSWORD", "bar")
setEnv("ENVH_DB_PORT", "3306")
setEnv("ENVH_DB_USAGE_LIMIT", "95.6")
setEnv("ENVH_MAILER_HOST", "127.0.0.1")
setEnv("ENVH_MAILER_USERNAME", "foo")
setEnv("ENVH_MAILER_PASSWORD", "bar")
setEnv("ENVH_MAILER_ENABLED", "true")

env, err := NewEnvTree("^ENVH", "_")

if err != nil {
    return
}

fmt.Println(env.FindBool("ENVH", "MAILER", "ENABLED"))
fmt.Println(env.FindBool("ENVH", "DB", "USERNAME"))
fmt.Println(env.FindBool("ENVH", "DB", "WHATEVER"))

Output:

true <nil>
false Value "foo" can't be converted to type "bool"
false Variable not found

func (EnvTree) FindChildrenKeys Uses

func (e EnvTree) FindChildrenKeys(keyChain ...string) ([]string, error)

FindChildrenKeys returns all children keys for a given key chain. If sub node doesn't exist, it returns an error ErrNodeNotFound as second value

Code:

os.Clearenv()
setEnv("ENVH_DB_USERNAME", "foo")
setEnv("ENVH_DB_PASSWORD", "bar")
setEnv("ENVH_DB_PORT", "3306")
setEnv("ENVH_DB_USAGE_LIMIT", "95.6")
setEnv("ENVH_MAILER_HOST", "127.0.0.1")
setEnv("ENVH_MAILER_USERNAME", "foo")
setEnv("ENVH_MAILER_PASSWORD", "bar")
setEnv("ENVH_MAILER_ENABLED", "true")

env, err := NewEnvTree("^ENVH", "_")

if err != nil {
    return
}

children, err := env.FindChildrenKeys("ENVH", "DB")

sort.Strings(children)

fmt.Print(children)
fmt.Print(" ")
fmt.Println(err)
fmt.Println(env.FindChildrenKeys("ENVH", "WHATEVER"))

Output:

[PASSWORD PORT USAGE USERNAME] <nil>
[] No node found at path "ENVH -> WHATEVER"

func (EnvTree) FindFloat Uses

func (e EnvTree) FindFloat(keyChain ...string) (float32, error)

FindFloat returns a float if key chain exists or an error if value is not a float or doesn't exist

Code:

os.Clearenv()
setEnv("ENVH_DB_USERNAME", "foo")
setEnv("ENVH_DB_PASSWORD", "bar")
setEnv("ENVH_DB_PORT", "3306")
setEnv("ENVH_DB_USAGE_LIMIT", "95.6")
setEnv("ENVH_MAILER_HOST", "127.0.0.1")
setEnv("ENVH_MAILER_USERNAME", "foo")
setEnv("ENVH_MAILER_PASSWORD", "bar")
setEnv("ENVH_MAILER_ENABLED", "true")

env, err := NewEnvTree("^ENVH", "_")

if err != nil {
    return
}

fmt.Println(env.FindFloat("ENVH", "DB", "USAGE", "LIMIT"))
fmt.Println(env.FindFloat("ENVH", "DB", "USERNAME"))
fmt.Println(env.FindFloat("ENVH", "DB", "WHATEVER"))

Output:

95.6 <nil>
0 Value "foo" can't be converted to type "float"
0 Variable not found

func (EnvTree) FindInt Uses

func (e EnvTree) FindInt(keyChain ...string) (int, error)

FindInt returns an integer if key chain exists or an error if value is not an integer or doesn't exist

Code:

os.Clearenv()
setEnv("ENVH_DB_USERNAME", "foo")
setEnv("ENVH_DB_PASSWORD", "bar")
setEnv("ENVH_DB_PORT", "3306")
setEnv("ENVH_DB_USAGE_LIMIT", "95.6")
setEnv("ENVH_MAILER_HOST", "127.0.0.1")
setEnv("ENVH_MAILER_USERNAME", "foo")
setEnv("ENVH_MAILER_PASSWORD", "bar")
setEnv("ENVH_MAILER_ENABLED", "true")

env, err := NewEnvTree("^ENVH", "_")

if err != nil {
    return
}

fmt.Println(env.FindInt("ENVH", "DB", "PORT"))
fmt.Println(env.FindInt("ENVH", "DB", "USERNAME"))
fmt.Println(env.FindInt("ENVH", "DB", "WHATEVER"))

Output:

3306 <nil>
0 Value "foo" can't be converted to type "int"
0 Variable not found

func (EnvTree) FindString Uses

func (e EnvTree) FindString(keyChain ...string) (string, error)

FindString returns a string if key chain exists or an error otherwise

Code:

os.Clearenv()
setEnv("ENVH_DB_USERNAME", "foo")
setEnv("ENVH_DB_PASSWORD", "bar")
setEnv("ENVH_DB_PORT", "3306")
setEnv("ENVH_DB_USAGE_LIMIT", "95.6")
setEnv("ENVH_MAILER_HOST", "127.0.0.1")
setEnv("ENVH_MAILER_USERNAME", "foo")
setEnv("ENVH_MAILER_PASSWORD", "bar")
setEnv("ENVH_MAILER_ENABLED", "true")

env, err := NewEnvTree("^ENVH", "_")

if err != nil {
    return
}

fmt.Println(env.FindString("ENVH", "DB", "USERNAME"))
fmt.Println(env.FindString("ENVH", "DB", "WHATEVER"))

Output:

foo <nil>
 Variable not found

func (EnvTree) FindSubTree Uses

func (e EnvTree) FindSubTree(keyChain ...string) (EnvTree, error)

FindSubTree returns underlying tree from key chain, for instance given A -> B -> C -> D tree, "A" "B" "C" key chain will return C sub tree. If no node is found, it returns an error ErrNodeNotFound as second value

Code:

os.Clearenv()
setEnv("ENVH_DB_USERNAME", "foo")
setEnv("ENVH_DB_PASSWORD", "bar")
setEnv("ENVH_DB_PORT", "3306")
setEnv("ENVH_DB_USAGE_LIMIT", "95.6")
setEnv("ENVH_MAILER_HOST", "127.0.0.1")
setEnv("ENVH_MAILER_USERNAME", "foo")
setEnv("ENVH_MAILER_PASSWORD", "bar")
setEnv("ENVH_MAILER_ENABLED", "true")

env, err := NewEnvTree("^ENVH", "_")

if err != nil {
    return
}

dbTree, err := env.FindSubTree("ENVH", "DB")
dbChildrenKeys := dbTree.GetChildrenKeys()
sort.Strings(dbChildrenKeys)

fmt.Print(dbChildrenKeys)
fmt.Print(" ")
fmt.Println(err)

mailerTree, err := env.FindSubTree("ENVH", "MAILER")
mailerChildrenKeys := mailerTree.GetChildrenKeys()
sort.Strings(mailerChildrenKeys)

fmt.Print(mailerChildrenKeys)
fmt.Print(" ")
fmt.Println(err)

fmt.Println(env.FindSubTree("ENVH", "MAILER", "WHATEVER"))

Output:

[PASSWORD PORT USAGE USERNAME] <nil>
[ENABLED HOST PASSWORD USERNAME] <nil>
{<nil>} No node found at path "ENVH -> MAILER -> WHATEVER"

func (EnvTree) GetBool Uses

func (e EnvTree) GetBool() (bool, error)

GetBool returns current tree value as boolean if value exists or an error if value is not a boolean or doesn't exist

Code:

os.Clearenv()
setEnv("ENVH_DB_USERNAME", "foo")
setEnv("ENVH_DB_PASSWORD", "bar")
setEnv("ENVH_DB_PORT", "3306")
setEnv("ENVH_DB_USAGE_LIMIT", "95.6")
setEnv("ENVH_MAILER_HOST", "127.0.0.1")
setEnv("ENVH_MAILER_USERNAME", "foo")
setEnv("ENVH_MAILER_PASSWORD", "bar")
setEnv("ENVH_MAILER_ENABLED", "true")

env, err := NewEnvTree("^ENVH", "_")

if err != nil {
    return
}

enabledTree, err := env.FindSubTree("ENVH", "MAILER", "ENABLED")

if err != nil {
    return
}

fmt.Println(env.GetBool())
fmt.Println(enabledTree.GetBool())

Output:

false Variable not found
true <nil>

func (EnvTree) GetChildrenKeys Uses

func (e EnvTree) GetChildrenKeys() []string

GetChildrenKeys retrieves all current tree children node keys

Code:

os.Clearenv()
setEnv("ENVH_DB_USERNAME", "foo")
setEnv("ENVH_DB_PASSWORD", "bar")
setEnv("ENVH_DB_PORT", "3306")
setEnv("ENVH_DB_USAGE_LIMIT", "95.6")
setEnv("ENVH_MAILER_HOST", "127.0.0.1")
setEnv("ENVH_MAILER_USERNAME", "foo")
setEnv("ENVH_MAILER_PASSWORD", "bar")
setEnv("ENVH_MAILER_ENABLED", "true")

env, err := NewEnvTree("^ENVH", "_")

if err != nil {
    return
}

dbTree, err := env.FindSubTree("ENVH", "DB")

if err != nil {
    return
}

children := dbTree.GetChildrenKeys()

sort.Strings(children)

fmt.Println(children)

Output:

[PASSWORD PORT USAGE USERNAME]

func (EnvTree) GetFloat Uses

func (e EnvTree) GetFloat() (float32, error)

GetFloat returns current tree value as float if value exists or an error if value is not a float or doesn't exist

Code:

os.Clearenv()
setEnv("ENVH_DB_USERNAME", "foo")
setEnv("ENVH_DB_PASSWORD", "bar")
setEnv("ENVH_DB_PORT", "3306")
setEnv("ENVH_DB_USAGE_LIMIT", "95.6")
setEnv("ENVH_MAILER_HOST", "127.0.0.1")
setEnv("ENVH_MAILER_USERNAME", "foo")
setEnv("ENVH_MAILER_PASSWORD", "bar")
setEnv("ENVH_MAILER_ENABLED", "true")

env, err := NewEnvTree("^ENVH", "_")

if err != nil {
    return
}

portTree, err := env.FindSubTree("ENVH", "DB", "USAGE", "LIMIT")

if err != nil {
    return
}

fmt.Println(env.GetFloat())
fmt.Println(portTree.GetFloat())

Output:

0 Variable not found
95.6 <nil>

func (EnvTree) GetInt Uses

func (e EnvTree) GetInt() (int, error)

GetInt returns current tree value as int if value exists or an error if value is not an integer or doesn't exist

Code:

os.Clearenv()
setEnv("ENVH_DB_USERNAME", "foo")
setEnv("ENVH_DB_PASSWORD", "bar")
setEnv("ENVH_DB_PORT", "3306")
setEnv("ENVH_DB_USAGE_LIMIT", "95.6")
setEnv("ENVH_MAILER_HOST", "127.0.0.1")
setEnv("ENVH_MAILER_USERNAME", "foo")
setEnv("ENVH_MAILER_PASSWORD", "bar")
setEnv("ENVH_MAILER_ENABLED", "true")

env, err := NewEnvTree("^ENVH", "_")

if err != nil {
    return
}

portTree, err := env.FindSubTree("ENVH", "DB", "PORT")

if err != nil {
    return
}

fmt.Println(env.GetInt())
fmt.Println(portTree.GetInt())

Output:

0 Variable not found
3306 <nil>

func (EnvTree) GetKey Uses

func (e EnvTree) GetKey() string

GetKey returns current tree key

Code:

os.Clearenv()
setEnv("ENVH_DB_USERNAME", "foo")
setEnv("ENVH_DB_PASSWORD", "bar")
setEnv("ENVH_DB_PORT", "3306")
setEnv("ENVH_DB_USAGE_LIMIT", "95.6")
setEnv("ENVH_MAILER_HOST", "127.0.0.1")
setEnv("ENVH_MAILER_USERNAME", "foo")
setEnv("ENVH_MAILER_PASSWORD", "bar")
setEnv("ENVH_MAILER_ENABLED", "true")

env, err := NewEnvTree("^ENVH", "_")

if err != nil {
    return
}

dbTree, err := env.FindSubTree("ENVH", "DB")

if err != nil {
    return
}

fmt.Println(dbTree.GetKey())

Output:

DB

func (EnvTree) GetString Uses

func (e EnvTree) GetString() (string, error)

GetString returns current tree value as string if value exists or an error as second parameter

Code:

os.Clearenv()
setEnv("ENVH_DB_USERNAME", "foo")
setEnv("ENVH_DB_PASSWORD", "bar")
setEnv("ENVH_DB_PORT", "3306")
setEnv("ENVH_DB_USAGE_LIMIT", "95.6")
setEnv("ENVH_MAILER_HOST", "127.0.0.1")
setEnv("ENVH_MAILER_USERNAME", "foo")
setEnv("ENVH_MAILER_PASSWORD", "bar")
setEnv("ENVH_MAILER_ENABLED", "true")

env, err := NewEnvTree("^ENVH", "_")

if err != nil {
    return
}

usernameTree, err := env.FindSubTree("ENVH", "DB", "USERNAME")

if err != nil {
    return
}

fmt.Println(env.GetString())
fmt.Println(usernameTree.GetString())

Output:

 Variable not found
foo <nil>

func (EnvTree) HasSubTreeValue Uses

func (e EnvTree) HasSubTreeValue(keyChain ...string) (bool, error)

HasSubTreeValue returns true if key chain has a value or false if not. If sub node doesn't exist, it returns an error ErrNodeNotFound as second value

Code:

os.Clearenv()
setEnv("ENVH_DB_USERNAME", "foo")
setEnv("ENVH_DB_PASSWORD", "bar")
setEnv("ENVH_DB_PORT", "3306")
setEnv("ENVH_DB_USAGE_LIMIT", "95.6")
setEnv("ENVH_MAILER_HOST", "127.0.0.1")
setEnv("ENVH_MAILER_USERNAME", "foo")
setEnv("ENVH_MAILER_PASSWORD", "bar")
setEnv("ENVH_MAILER_ENABLED", "true")

env, err := NewEnvTree("^ENVH", "_")

if err != nil {
    return
}

fmt.Println(env.HasSubTreeValue("ENVH", "MAILER", "HOST"))
fmt.Println(env.HasSubTreeValue("ENVH", "MAILER"))
fmt.Println(env.HasSubTreeValue("ENVH", "MAILER", "WHATEVER"))

Output:

true <nil>
false <nil>
false No node found at path "ENVH -> MAILER -> WHATEVER"

func (EnvTree) HasValue Uses

func (e EnvTree) HasValue() bool

HasValue returns true if current tree has a value defined false otherwise

Code:

os.Clearenv()
setEnv("ENVH_DB_USERNAME", "foo")
setEnv("ENVH_DB_PASSWORD", "bar")
setEnv("ENVH_DB_PORT", "3306")
setEnv("ENVH_DB_USAGE_LIMIT", "95.6")
setEnv("ENVH_MAILER_HOST", "127.0.0.1")
setEnv("ENVH_MAILER_USERNAME", "foo")
setEnv("ENVH_MAILER_PASSWORD", "bar")
setEnv("ENVH_MAILER_ENABLED", "true")

env, err := NewEnvTree("^ENVH", "_")

if err != nil {
    return
}

usernameTree, err := env.FindSubTree("ENVH", "DB", "USERNAME")

if err != nil {
    return
}

fmt.Println(env.HasValue())
fmt.Println(usernameTree.HasValue())

Output:

false
true

func (EnvTree) IsExistingSubTree Uses

func (e EnvTree) IsExistingSubTree(keyChain ...string) bool

IsExistingSubTree returns true if key chain has a tree associated or false if not

Code:

os.Clearenv()
setEnv("ENVH_DB_USERNAME", "foo")
setEnv("ENVH_DB_PASSWORD", "bar")
setEnv("ENVH_DB_PORT", "3306")
setEnv("ENVH_DB_USAGE_LIMIT", "95.6")
setEnv("ENVH_MAILER_HOST", "127.0.0.1")
setEnv("ENVH_MAILER_USERNAME", "foo")
setEnv("ENVH_MAILER_PASSWORD", "bar")
setEnv("ENVH_MAILER_ENABLED", "true")

env, err := NewEnvTree("^ENVH", "_")

if err != nil {
    return
}

fmt.Println(env.IsExistingSubTree("ENVH", "MAILER", "HOST"))
fmt.Println(env.IsExistingSubTree("ENVH", "MAILER", "WHATEVER"))

Output:

true
false

func (EnvTree) PopulateStruct Uses

func (e EnvTree) PopulateStruct(structure interface{}) error

PopulateStruct fills a structure with datas extracted. Missing values are ignored and only type errors are reported

Code:

type ENVH struct {
    DB  struct {
        USERNAME   string
        PASSWORD   string
        PORT       int
        USAGELIMIT float32
    }
    MAILER struct {
        HOST     string
        USERNAME string
        PASSWORD string
        ENABLED  bool
    }
}

os.Clearenv()
setEnv("ENVH_DB_USERNAME", "foo")
setEnv("ENVH_DB_PASSWORD", "bar")
setEnv("ENVH_DB_PORT", "3306")
setEnv("ENVH_DB_USAGELIMIT", "95.6")
setEnv("ENVH_MAILER_HOST", "127.0.0.1")
setEnv("ENVH_MAILER_USERNAME", "foo")
setEnv("ENVH_MAILER_PASSWORD", "bar")
setEnv("ENVH_MAILER_ENABLED", "true")

env, err := NewEnvTree("^ENVH", "_")

if err != nil {
    return
}

s := ENVH{}

err = env.PopulateStruct(&s)

if err != nil {
    return
}

b, err := json.Marshal(s)

if err != nil {
    return
}

fmt.Println(string(b))

Output:

{"DB":{"USERNAME":"foo","PASSWORD":"bar","PORT":3306,"USAGELIMIT":95.6},"MAILER":{"HOST":"127.0.0.1","USERNAME":"foo","PASSWORD":"bar","ENABLED":true}}

func (EnvTree) PopulateStructWithStrictMode Uses

func (e EnvTree) PopulateStructWithStrictMode(structure interface{}) error

PopulateStructWithStrictMode fills a structure with datas extracted. A missing environment variable returns an error and type errors are reported

Code:

type ENVH struct {
    DB  struct {
        USERNAME   string
        PASSWORD   string
        PORT       int
        USAGELIMIT float32
    }
    MAILER struct {
        HOST     string
        USERNAME string
        PASSWORD string
        ENABLED  bool
    }
}

os.Clearenv()
setEnv("ENVH_DB_USERNAME", "foo")
setEnv("ENVH_DB_PASSWORD", "bar")
setEnv("ENVH_DB_PORT", "3306")
setEnv("ENVH_DB_USAGELIMIT", "95.6")
setEnv("ENVH_MAILER_HOST", "127.0.0.1")
setEnv("ENVH_MAILER_USERNAME", "foo")
setEnv("ENVH_MAILER_PASSWORD", "bar")

env, err := NewEnvTree("^ENVH", "_")

if err != nil {
    return
}

s := ENVH{}

err = env.PopulateStructWithStrictMode(&s)

fmt.Println(err)

Output:

Variable not found

type NodeNotFoundError Uses

type NodeNotFoundError struct {
    KeyChain []string
}

NodeNotFoundError is triggered when tree node cannot be found

func (NodeNotFoundError) Error Uses

func (e NodeNotFoundError) Error() string

Error dump error

type TypeUnsupported Uses

type TypeUnsupported struct {
    ActualType   string
    RequiredType string
}

TypeUnsupported is triggered when a type isn't supported

func (TypeUnsupported) Error Uses

func (e TypeUnsupported) Error() string

Error dump error

type VariableNotFoundError Uses

type VariableNotFoundError struct {
}

VariableNotFoundError is triggered when environment variable cannot be found

func (VariableNotFoundError) Error Uses

func (e VariableNotFoundError) Error() string

Error dump error

type WrongTypeError Uses

type WrongTypeError struct {
    Value interface{}
    Type  string
}

WrongTypeError is triggered when we try to convert variable to a wrong type

func (WrongTypeError) Error Uses

func (e WrongTypeError) Error() string

Error dump error

Package envh imports 6 packages (graph). Updated 2017-04-18. Refresh now. Tools for package owners.