VictoriaMetrics/lib/envtemplate/envtemplate.go
Aliaksandr Valialkin 069401a304
all: log error when environment variables referred from -promscrape.config are missing
This should prevent from using incorrect config files
2022-10-18 10:47:16 +03:00

31 lines
722 B
Go

package envtemplate
import (
"bytes"
"fmt"
"io"
"os"
"github.com/valyala/fasttemplate"
)
// Replace replaces `%{ENV_VAR}` placeholders in b with the corresponding ENV_VAR values.
//
// Error is returned if ENV_VAR isn't set for some `%{ENV_VAR}` placeholder.
func Replace(b []byte) ([]byte, error) {
if !bytes.Contains(b, []byte("%{")) {
// Fast path - nothing to replace.
return b, nil
}
s, err := fasttemplate.ExecuteFuncStringWithErr(string(b), "%{", "}", func(w io.Writer, tag string) (int, error) {
v, ok := os.LookupEnv(tag)
if !ok {
return 0, fmt.Errorf("missing %q environment variable", tag)
}
return w.Write([]byte(v))
})
if err != nil {
return nil, err
}
return []byte(s), nil
}