summaryrefslogtreecommitdiff
path: root/funcs/env.go
blob: bf60200639f90fe90f80700c5262049354913e80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package funcs

import (
	"sync"

	"github.com/hairyhenderson/gomplate/conv"
	"github.com/hairyhenderson/gomplate/env"
)

var (
	ef     *EnvFuncs
	efInit sync.Once
)

// EnvNS - the Env namespace
func EnvNS() *EnvFuncs {
	efInit.Do(func() { ef = &EnvFuncs{} })
	return ef
}

// AddEnvFuncs -
func AddEnvFuncs(f map[string]interface{}) {
	f["env"] = EnvNS

	// global aliases - for backwards compatibility
	f["getenv"] = EnvNS().Getenv
}

// EnvFuncs -
type EnvFuncs struct{}

// Getenv -
func (f *EnvFuncs) Getenv(key interface{}, def ...string) string {
	return env.Getenv(conv.ToString(key), def...)
}

// ExpandEnv -
func (f *EnvFuncs) ExpandEnv(s interface{}) string {
	return env.ExpandEnv(conv.ToString(s))
}