Giter VIP home page Giter VIP logo

envconf's Introduction

envconf

Tiny environment variable mapper for golang

Getting Started

Prerequisites

  • Go 1.11+

Installing

$ go get -u github.com/akito0107/envconf

Examples

  1. Simple mapping case

set environment variables.

export DB_HOST=localhost
export DB_PORT=5432

and implement Go code like this

package main

import (
	"log"

	"github.com/akito0107/envconf"
)

// struct with `env` tagged literal
type Config struct {
    DBHost string `env:"DB_HOST"` // 
    DBPort int `env:"DB_PORT"`
}

func main() {
    var conf Config
    
    // load environment variables with `Load` method
    if err := envconf.Load(&conf); err != nil {
    	log.Fatal(err)
    }
    
    log.Printf("%+v\n", conf) // should be `{DBHost:localhost DBPort:5432}`
}

You can confirm that variables are mapped to struct via envconf.Load.

$ go run main.go
2019/03/01 16:46:14 {DBHost:localhost DBPort:5432}
  1. Blank environment variables

envconf.Load, in default behaviour, returns error when specified environment variables are blank. If missing DB_PORT variable, returns error.

$ export DB_HOST=localhost
$ go run main.go
2019/03/01 16:49:56 init: relpaceEnv failed: environmentVariableNotFound Envname: DB_PORT
exit status 1
  1. Allow Empty

If you want to allow empty variables, you must specify allow-empty on env tag. This feature convenient for setting default parameter.

// struct with `env` tagged literal
type Config struct {
	DBHost string `env:"DB_HOST"`
	DBPort int    `env:"DB_PORT,allow-empty"` // set allow-empty
}

....
	
var conf Config

	// load environment variables with `Load` method
if err := envconf.Load(&conf); err != nil {
	log.Fatal(err) // should not be error
}

log.Printf("%+v\n", conf) // 2019/03/01 16:57:57 {DBHost:localhost DBPort:0}

conf2 := Config{DBPort: 12345} // passing default param

// load environment variables with `Load` method
if err := envconf.Load(&conf2); err != nil {
	log.Fatal(err)
}

log.Printf("%+v\n", conf2) // 2019/03/01 16:57:57 {DBHost:localhost DBPort:12345}
  1. with Dotenv

envconf support using with dotenv by passing UseDotEnv option.

In default case, .env vars override environment variables

Go code.

// load environment variables and dotenv variables with `Load` method
if err := envconf.Load(&conf, envconf.UseDotEnv()); err != nil {
	log.Fatal(err)
}
log.Printf("%+v\n", conf)
$ cat .env
DB_USER=test

$ export DB_HOST=localhost
$ export DB_PORT=5432
$ export DB_USER=test2
$ go run main.go
2019/03/01 17:15:31 {DBHost:localhost DBPort:5432 DBUser:test} # overrided .env
  1. disable Dotenv via ENVCONF_LOAD_DOTFILE environment variables

if ENVCONF_LOAD_DOTFILE is set to disable, skip load .env. This feature convenient for avoid to set undesired vars from .env.

$ cat .env
DB_USER=test

$ export ENVCONF_LOAD_DOTFILE=disable
$ export DB_HOST=localhost
$ export DB_PORT=5432
$ export DB_USER=test2
$ go run main.go
2019/03/01 17:15:31 {DBHost:localhost DBPort:5432 DBUser:test2} # overrided .env

License

This project is licensed under the Apache License 2.0 License - see the LICENSE file for details

envconf's People

Contributors

akito0107 avatar

Stargazers

 avatar

Watchers

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