mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-27 11:31:06 +01:00
feat(yaml): extend environment variable substitution to all YAML files
- returns error for unset environment variables
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"os"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -517,7 +518,22 @@ func ConvertString(src string, dst reflect.Value) (convertible bool, convErr gpe
|
||||
return true, Convert(reflect.ValueOf(tmp), dst, true)
|
||||
}
|
||||
|
||||
var envRegex = regexp.MustCompile(`\$\{([^}]+)\}`) // e.g. ${CLOUDFLARE_API_KEY}
|
||||
|
||||
func UnmarshalValidateYAML[T any](data []byte, target *T) gperr.Error {
|
||||
envError := gperr.NewBuilder("env substitution error")
|
||||
data = envRegex.ReplaceAllFunc(data, func(match []byte) []byte {
|
||||
varName := string(match[2 : len(match)-1])
|
||||
env, ok := os.LookupEnv(varName)
|
||||
if !ok {
|
||||
envError.Addf("%s is not set", varName)
|
||||
}
|
||||
return strconv.AppendQuote(nil, env)
|
||||
})
|
||||
if envError.HasError() {
|
||||
return envError.Error()
|
||||
}
|
||||
|
||||
m := make(map[string]any)
|
||||
if err := yaml.Unmarshal(data, &m); err != nil {
|
||||
return gperr.Wrap(err)
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package serialization
|
||||
|
||||
import (
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/goccy/go-yaml"
|
||||
"github.com/stretchr/testify/require"
|
||||
. "github.com/yusing/go-proxy/internal/utils/testing"
|
||||
)
|
||||
|
||||
@@ -314,6 +316,26 @@ func TestStringToStruct(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestConfigEnvSubstitution(t *testing.T) {
|
||||
os.Setenv("CLOUDFLARE_AUTH_TOKEN", "test")
|
||||
data := []byte(`
|
||||
---
|
||||
autocert:
|
||||
options:
|
||||
auth_token: ${CLOUDFLARE_AUTH_TOKEN}
|
||||
`)
|
||||
|
||||
var cfg struct {
|
||||
Autocert struct {
|
||||
Options struct {
|
||||
AuthToken string `yaml:"auth_token"`
|
||||
} `yaml:"options"`
|
||||
} `yaml:"autocert"`
|
||||
}
|
||||
require.NoError(t, UnmarshalValidateYAML(data, &cfg))
|
||||
require.Equal(t, "test", cfg.Autocert.Options.AuthToken)
|
||||
}
|
||||
|
||||
func BenchmarkStringToStruct(b *testing.B) {
|
||||
for range b.N {
|
||||
dst := struct {
|
||||
|
||||
Reference in New Issue
Block a user