summaryrefslogtreecommitdiff
path: root/env/env.go
blob: 59fcf2ac86ec698f6db0181f5742492eb96f09ee (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
// Package env contains functions that retrieve data from the environment
package env

import (
	osfs "github.com/hack-pad/hackpadfs/os"
	"github.com/hairyhenderson/gomplate/v4/internal/datafs"
)

// Getenv - retrieves the value of the environment variable named by the key.
// If the variable is unset, but the same variable ending in `_FILE` is set, the
// referenced file will be read into the value.
// Otherwise the provided default (or an empty string) is returned.
func Getenv(key string, def ...string) string {
	fsys := datafs.WrapWdFS(osfs.NewFS())
	return datafs.GetenvFsys(fsys, key, def...)
}

// ExpandEnv - like os.ExpandEnv, except supports `_FILE` vars as well
func ExpandEnv(s string) string {
	fsys := datafs.WrapWdFS(osfs.NewFS())
	return datafs.ExpandEnvFsys(fsys, s)
}

// LookupEnv - retrieves the value of the environment variable named by the key.
// If the variable is unset, but the same variable ending in `_FILE` is set, the
// referenced file will be read into the value. If the key is not set, the
// second return value will be false.
// Otherwise the provided default (or an empty string) is returned.
func LookupEnv(key string) (string, bool) {
	fsys := datafs.WrapWdFS(osfs.NewFS())
	return datafs.LookupEnvFsys(fsys, key)
}