github.com/hairyhenderson/templater@v3.5.0+incompatible/funcs/env.go (about)

     1  package funcs
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/hairyhenderson/gomplate/conv"
     7  	"github.com/hairyhenderson/gomplate/env"
     8  )
     9  
    10  var (
    11  	ef     *EnvFuncs
    12  	efInit sync.Once
    13  )
    14  
    15  // EnvNS - the Env namespace
    16  func EnvNS() *EnvFuncs {
    17  	efInit.Do(func() { ef = &EnvFuncs{} })
    18  	return ef
    19  }
    20  
    21  // AddEnvFuncs -
    22  func AddEnvFuncs(f map[string]interface{}) {
    23  	f["env"] = EnvNS
    24  
    25  	// global aliases - for backwards compatibility
    26  	f["getenv"] = EnvNS().Getenv
    27  }
    28  
    29  // EnvFuncs -
    30  type EnvFuncs struct{}
    31  
    32  // Getenv -
    33  func (f *EnvFuncs) Getenv(key interface{}, def ...string) string {
    34  	return env.Getenv(conv.ToString(key), def...)
    35  }
    36  
    37  // ExpandEnv -
    38  func (f *EnvFuncs) ExpandEnv(s interface{}) string {
    39  	return env.ExpandEnv(conv.ToString(s))
    40  }