mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
069401a304
This should prevent from using incorrect config files
31 lines
722 B
Go
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
|
|
}
|