Giter VIP home page Giter VIP logo

enviper's Introduction

enviper

PkgGoDev Build Status Test Coverage Maintainability Go Report Card

Package enviper is a helper/wrapper for viper with the same API. It makes it possible to unmarshal config to struct considering environment variables.

Problem

Viper package doesn't consider environment variables while unmarshaling. Please, see: 188 and 761

Solution

Just wrap viper instance and use the same Unmarshal method as you did before:

e := enviper.New(viper.New())
e.Unmarshal(&config)

Example

package main

import (
	"github.com/iamolegga/enviper"
	"github.com/spf13/viper"
)

type barry struct {
    Bar int `mapstructure:"bar"`
}
type bazzy struct {
    Baz bool
}
type config struct {
    Foo string
    Barry barry
    Barries map[string]barry
    Bazzy bazzy `mapstructure:",squash"`
}

// For example this kind of structure can be unmarshaled with next yaml:
//  Foo: foo
//  Barry:
//    bar: 42
//  Baz: true
//  Barries: 
//    key1:
//      Bar: 255
//    key2:
//      Bar: 256
//
// And then it could be overriden by next env variables:
//  FOO=foo
//  BARRY_BAR=42
//  BAZ=true
//  BARRIES_KEY1_BAR=42
//
// Or with prefix:
//  MYAPP_FOO=foo
//  MYAPP_BARRY_BAR=42
//  MYAPP_BAZ=true
//  MYAPP_BARRIES_KEY1_BAR=42

func main() {    
    var c config

    e := enviper.New(viper.New())
    e.SetEnvPrefix("MYAPP")
    e.AddConfigPath("/my/config/path")
    e.SetConfigName("config")

    e.Unmarshal(&c)
}

Credits

Thanks to krak3n (issuecomment-399884438) and celian-garcia (issuecomment-626122696) for inspiring.

enviper's People

Contributors

celian-garcia avatar iamolegga avatar pzeinlinger avatar

Watchers

 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.