forked from a8m/envsubst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
envsubst.go
59 lines (51 loc) · 2.2 KB
/
envsubst.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package envsubst
import (
"io/ioutil"
"os"
"github.com/a8m/envsubst/parse"
)
// String returns the parsed template string after processing it.
// If the parser encounters invalid input, it returns an error describing the failure.
func String(s string) (string, error) {
return StringRestricted(s, false, false, false)
}
// StringRestricted returns the parsed template string after processing it.
// If the parser encounters invalid input, or a restriction is violated, it returns
// an error describing the failure.
// Errors on first failure or returns a collection of failures if failOnFirst is false
func StringRestricted(s string, noUnset, noEmpty, ignoreEmpty bool) (string, error) {
return parse.New("string", os.Environ(),
&parse.Restrictions{noUnset, noEmpty, ignoreEmpty}).Parse(s)
}
// Bytes returns the bytes represented by the parsed template after processing it.
// If the parser encounters invalid input, it returns an error describing the failure.
func Bytes(b []byte) ([]byte, error) {
return BytesRestricted(b, false, false, false)
}
// BytesRestricted returns the bytes represented by the parsed template after processing it.
// If the parser encounters invalid input, or a restriction is violated, it returns
// an error describing the failure.
func BytesRestricted(b []byte, noUnset, noEmpty, ignoreEmpty bool) ([]byte, error) {
s, err := parse.New("bytes", os.Environ(),
&parse.Restrictions{noUnset, noEmpty, ignoreEmpty}).Parse(string(b))
if err != nil {
return nil, err
}
return []byte(s), nil
}
// ReadFile call io.ReadFile with the given file name.
// If the call to io.ReadFile failed it returns the error; otherwise it will
// call envsubst.Bytes with the returned content.
func ReadFile(filename string) ([]byte, error) {
return ReadFileRestricted(filename, false, false, false)
}
// ReadFileRestricted calls io.ReadFile with the given file name.
// If the call to io.ReadFile failed it returns the error; otherwise it will
// call envsubst.Bytes with the returned content.
func ReadFileRestricted(filename string, noUnset, noEmpty, ignoreEmpty bool) ([]byte, error) {
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return BytesRestricted(b, noUnset, noEmpty, ignoreEmpty)
}