Files
netbird/util/envtemplate/template.go
jnfrati 0646d8fc78 [management,misc] Fix configuration migration checks
The initial migration let an empty legacy Datadir override Management defaults and pulled Viper into WASM builds through the common util package.

Normalize the Management data directory, isolate template expansion from the loader dependency, and split source binding to satisfy the quality gate.
2026-08-25 11:03:33 +02:00

36 lines
810 B
Go

// Package envtemplate expands Go templates with environment variables.
package envtemplate
import (
"bytes"
"fmt"
"os"
"strings"
"text/template"
)
// Expand substitutes Go-template references with environment values.
func Expand(data []byte) ([]byte, error) {
tmpl, err := template.New("config").Parse(string(data))
if err != nil {
return nil, fmt.Errorf("parse environment template: %w", err)
}
var output bytes.Buffer
if err := tmpl.Execute(&output, environment()); err != nil {
return nil, fmt.Errorf("execute environment template: %w", err)
}
return output.Bytes(), nil
}
func environment() map[string]string {
values := make(map[string]string)
for _, entry := range os.Environ() {
key, value, ok := strings.Cut(entry, "=")
if ok {
values[key] = value
}
}
return values
}