github.com/greenboxal/deis@v1.12.1/builder/env/envvar.go (about)

     1  package env
     2  
     3  import (
     4  	"github.com/Masterminds/cookoo"
     5  	"github.com/Masterminds/cookoo/log"
     6  	"os"
     7  )
     8  
     9  // Get gets one or more environment variables and puts them into the context.
    10  //
    11  // Parameters passed in are of the form varname => defaultValue.
    12  //
    13  // 	r.Route("foo", "example").Does(envvar.Get).Using("HOME").WithDefault(".")
    14  //
    15  // As with all environment variables, the default value must be a string.
    16  //
    17  // WARNING: Since parameters are a map, order of processing is not
    18  // guaranteed. If order is important, you'll need to call this command
    19  // multiple times.
    20  //
    21  // For each parameter (`Using` clause), this command will look into the
    22  // environment for a matching variable. If it finds one, it will add that
    23  // variable to the context. If it does not find one, it will expand the
    24  // default value (so you can set a default to something like "$HOST:$PORT")
    25  // and also put the (unexpanded) default value back into the context in case
    26  // any subsequent call to `os.Getenv` occurs.
    27  func Get(c cookoo.Context, params *cookoo.Params) (interface{}, cookoo.Interrupt) {
    28  	for name, def := range params.AsMap() {
    29  		var val string
    30  		if val = os.Getenv(name); len(val) == 0 {
    31  			def := def.(string)
    32  			val = os.ExpandEnv(def)
    33  			// We want to make sure that any subsequent calls to Getenv
    34  			// return the same default.
    35  			os.Setenv(name, val)
    36  
    37  		}
    38  		c.Put(name, val)
    39  		log.Debugf(c, "Name: %s, Val: %s", name, val)
    40  	}
    41  	return true, nil
    42  }