Giter Club home page Giter Club logo

envlookup's Introduction

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
    ...
}

envlookup's People

Contributors

cabels avatar thepatrik avatar

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.