Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

spider-pigs/envlookup

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

envlookup

https://travis-ci.org/spider-pigs/envlookup.svg?branch=master https://goreportcard.com/badge/github.com/spider-pigs/envlookup?.svg https://godoc.org/github.com/spider-pigs/envlookup?status.svg

Small lib to avoid boiler plate code around environment variables.

Install

import "github.com/spider-pigs/envlookup"

Usage

Set some environment variables:

export JAZZ_ARTIST="John Coltrane"
export PLAYED_WITH_MILES_DAVIES=true
export NO_OF_STUDIO_ALBUMS="51"
export RECORD_LABELS="Impulse!,Atlantic,Prestige,Blue Note"
export LONGEST_RECORDED_TRACK="27m32s"

Get env

Get an env var value. If the value does not exist, an error will be returned:

s, err := envlookup.String("JAZZ_ARTIST")

Get env or default value

Get an env var value or a default value if it does not exist:

s, err := envlookup.String("JAZZ_SAXOPHONIST", "Wayne Shorter")

Get mandatory env

There are must helper functions for mandatory env vars (panics if err is non-nil):

s := envlookup.MustString(envlookup.String("JAZZ_SAXOPHONIST"))

Get slice env

To get values as a slice (comma-separated string):

s, err := envlookup.Slice("RECORD_LABELS")

Get bool env

Boolean values are supported:

b, err := envlookup.Bool("PLAYED_WITH_MILES_DAVIES")

Get float or int env

Get float (64 bit) or int envs:

i, err := envlookup.Int("NO_OF_STUDIO_ALBUMS")

Get duration env

To get a duration value:

d, err := envlookup.Duration("LONGEST_RECORDED_TRACK")

Errors

If an env var is not set (and there is no default value set), a NotFoundError will be returned:

d, err := envlookup.Duration("LONGEST_ALBUM")
if _, ok := err.(*envlookup.NotFoundError); ok {
    log.Println(err)
    // could not find environment variable "LONGEST_ALBUM"
    ...
}

If an env var can not be parsed as the expected return type, a ParseError will be returned:

f, err := envlookup.Float64("LONGEST_RECORDED_TRACK")
if _, ok := err.(*envlookup.ParseError); ok {
    log.Println(err)
    // could not parse environment variable "LONGEST_RECORDED_TRACK": strconv.ParseFloat: parsing "27m32s": invalid syntax
    ...
}

About

Golang library for managing environment variables.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.