feat(yaml): extend environment variable substitution to all YAML files

- returns error for unset environment variables
This commit is contained in:
yusing
2025-09-11 22:04:13 +08:00
parent acf7490991
commit f7de703c15
4 changed files with 39 additions and 53 deletions

View File

@@ -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 {