Skip to content

licaonfee/envy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

envy

Environment variables utilities

Usage

package main

import (
	"flag"
	"fmt"
	"github.com/licaonfee/envy"
	"os"
)

func main() {
    //this value should be ignored, because -my-flag is set
    os.Setenv("MY_FLAG", "fooo")
    //this value is passed to my-time
    os.Setenv("MY_TIME", "1m")
    os.Args = []string{"my-bin", "-my-flag", "bar"}
    
    myflag := flag.String("my-flag", "", "my flag")
    mytime := flag.Duration("my-time", 0, "my-time")
    
    flag.Parse()
    envy.FillFlags(flag.CommandLine)

    fmt.Printf("my-flag: %s \n", *myflag)
    fmt.Printf("my-time: %v \n", *mytime)
}

With mapstructure

package main

import (
	"fmt"
	"log"

	"github.com/licaonfee/envy"
	"github.com/mitchellh/mapstructure"
)

type config struct {
	Path  string   `mapstructure:"path"`
	Users []string `mapstructure:"users"`
	Address map[string]string `mapstructure:"address"`
}

// Environment
// APP_PATH=log.log
// APP_USERS_0=one_user
// APP_USERS_1=other_user
// APP_ADDRESS_LOCAL=localhost
// APP_ADDRESS_REMOTE=8.8.8.8

func main() {
	var cfg config
	m := envy.FillMap(envy.NewOsEnv(), envy.FilterPrefix("APP_"))
	dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
		Result: &cfg,
	})
	if err != nil {
		log.Fatal(err)
	}
	if err := dec.Decode(m); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%#v\n", cfg)
}