fix: error msg for missing env variables in config (#10681)
This commit is contained in:
parent
855e23d978
commit
063ef6d517
|
|
@ -1025,6 +1025,7 @@ func parseConfig(contents []byte) (*ast.Table, error) {
|
||||||
contents = trimBOM(contents)
|
contents = trimBOM(contents)
|
||||||
|
|
||||||
parameters := envVarRe.FindAllSubmatch(contents, -1)
|
parameters := envVarRe.FindAllSubmatch(contents, -1)
|
||||||
|
var missingEnvVars []string
|
||||||
for _, parameter := range parameters {
|
for _, parameter := range parameters {
|
||||||
if len(parameter) != 3 {
|
if len(parameter) != 3 {
|
||||||
continue
|
continue
|
||||||
|
|
@ -1043,9 +1044,21 @@ func parseConfig(contents []byte) (*ast.Table, error) {
|
||||||
if ok {
|
if ok {
|
||||||
envVal = escapeEnv(envVal)
|
envVal = escapeEnv(envVal)
|
||||||
contents = bytes.Replace(contents, parameter[0], []byte(envVal), 1)
|
contents = bytes.Replace(contents, parameter[0], []byte(envVal), 1)
|
||||||
|
} else {
|
||||||
|
missingEnvVars = append(missingEnvVars, string(envVar))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Report all missing environment variables to the user
|
||||||
|
if len(missingEnvVars) > 0 {
|
||||||
|
errorMsg := "environment variable(s) not set:"
|
||||||
|
for _, e := range missingEnvVars {
|
||||||
|
errorMsg += e + " "
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, fmt.Errorf(errorMsg)
|
||||||
|
}
|
||||||
|
|
||||||
return toml.Parse(contents)
|
return toml.Parse(contents)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -264,13 +264,15 @@ func TestConfig_WrongFieldType(t *testing.T) {
|
||||||
func TestConfig_InlineTables(t *testing.T) {
|
func TestConfig_InlineTables(t *testing.T) {
|
||||||
// #4098
|
// #4098
|
||||||
c := NewConfig()
|
c := NewConfig()
|
||||||
|
require.NoError(t, os.Setenv("TOKEN", "test"))
|
||||||
require.NoError(t, c.LoadConfig("./testdata/inline_table.toml"))
|
require.NoError(t, c.LoadConfig("./testdata/inline_table.toml"))
|
||||||
require.Len(t, c.Outputs, 2)
|
require.Len(t, c.Outputs, 2)
|
||||||
|
|
||||||
output, ok := c.Outputs[1].Output.(*MockupOuputPlugin)
|
output, ok := c.Outputs[1].Output.(*MockupOuputPlugin)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
require.Equal(t, map[string]string{"Authorization": "Token $TOKEN", "Content-Type": "application/json"}, output.Headers)
|
require.Equal(t, map[string]string{"Authorization": "Token test", "Content-Type": "application/json"}, output.Headers)
|
||||||
require.Equal(t, []string{"org_id"}, c.Outputs[0].Config.Filter.TagInclude)
|
require.Equal(t, []string{"org_id"}, c.Outputs[0].Config.Filter.TagInclude)
|
||||||
|
require.NoError(t, os.Unsetenv("TOKEN"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfig_SliceComment(t *testing.T) {
|
func TestConfig_SliceComment(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue