mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-03 12:09:09 +02:00
[management,signal,proxy,relay,misc] Unify service configuration loading
Service entry points currently resolve defaults, files, environment variables, and flags differently, which makes precedence inconsistent and prevents some services from using config files. Introduce one Viper-backed loader and migrate Combined, Management, Relay, Signal, and Proxy while preserving compatibility aliases and Management template expansion.
This commit is contained in:
+5
-29
@@ -1,7 +1,6 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@@ -10,10 +9,10 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
configloader "github.com/netbirdio/netbird/util/config"
|
||||
)
|
||||
|
||||
func WriteBytesWithRestrictedPermission(ctx context.Context, file string, bs []byte) error {
|
||||
@@ -233,8 +232,6 @@ func ListFiles(dir, pattern string) ([]string, error) {
|
||||
|
||||
// ReadJsonWithEnvSub reads JSON config file and maps to a provided interface with environment variable substitution
|
||||
func ReadJsonWithEnvSub(file string, res interface{}) (interface{}, error) {
|
||||
envVars := getEnvMap()
|
||||
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -246,19 +243,12 @@ func ReadJsonWithEnvSub(file string, res interface{}) (interface{}, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
t, err := template.New("").Parse(string(bs))
|
||||
output, err := configloader.ExpandEnvTemplate(bs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing template: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var output bytes.Buffer
|
||||
// Execute the template, substituting environment variables
|
||||
err = t.Execute(&output, envVars)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error executing template: %v", err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(output.Bytes(), &res)
|
||||
err = json.Unmarshal(output, &res)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed parsing Json file after template was executed, err: %v", err)
|
||||
}
|
||||
@@ -266,20 +256,6 @@ func ReadJsonWithEnvSub(file string, res interface{}) (interface{}, error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// getEnvMap Convert the output of os.Environ() to a map
|
||||
func getEnvMap() map[string]string {
|
||||
envMap := make(map[string]string)
|
||||
|
||||
for _, env := range os.Environ() {
|
||||
parts := strings.SplitN(env, "=", 2)
|
||||
if len(parts) == 2 {
|
||||
envMap[parts[0]] = parts[1]
|
||||
}
|
||||
}
|
||||
|
||||
return envMap
|
||||
}
|
||||
|
||||
// CopyFileContents copies contents of the given src file to the dst file
|
||||
func CopyFileContents(src, dst string) (err error) {
|
||||
in, err := os.Open(src)
|
||||
|
||||
Reference in New Issue
Block a user