0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-30 22:34:15 -05:00

chore: Fix caddyfile.replaceEnvVars return (#5311)

This commit is contained in:
Y.Horie 2023-01-17 20:57:42 +09:00 committed by GitHub
parent 223cbe3d0b
commit 62e8b21724
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 15 deletions

View file

@ -61,20 +61,12 @@ func Parse(filename string, input []byte) ([]ServerBlock, error) {
// It returns all the tokens from the input, unstructured // It returns all the tokens from the input, unstructured
// and in order. It may mutate input as it expands env vars. // and in order. It may mutate input as it expands env vars.
func allTokens(filename string, input []byte) ([]Token, error) { func allTokens(filename string, input []byte) ([]Token, error) {
inputCopy, err := replaceEnvVars(input) return Tokenize(replaceEnvVars(input), filename)
if err != nil {
return nil, err
}
tokens, err := Tokenize(inputCopy, filename)
if err != nil {
return nil, err
}
return tokens, nil
} }
// replaceEnvVars replaces all occurrences of environment variables. // replaceEnvVars replaces all occurrences of environment variables.
// It mutates the underlying array and returns the updated slice. // It mutates the underlying array and returns the updated slice.
func replaceEnvVars(input []byte) ([]byte, error) { func replaceEnvVars(input []byte) []byte {
var offset int var offset int
for { for {
begin := bytes.Index(input[offset:], spanOpen) begin := bytes.Index(input[offset:], spanOpen)
@ -115,7 +107,7 @@ func replaceEnvVars(input []byte) ([]byte, error) {
// continue at the end of the replacement // continue at the end of the replacement
offset = begin + len(envVarBytes) offset = begin + len(envVarBytes)
} }
return input, nil return input
} }
type parser struct { type parser struct {

View file

@ -604,10 +604,7 @@ func TestEnvironmentReplacement(t *testing.T) {
expect: "}{$", expect: "}{$",
}, },
} { } {
actual, err := replaceEnvVars([]byte(test.input)) actual := replaceEnvVars([]byte(test.input))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(actual, []byte(test.expect)) { if !bytes.Equal(actual, []byte(test.expect)) {
t.Errorf("Test %d: Expected: '%s' but got '%s'", i, test.expect, actual) t.Errorf("Test %d: Expected: '%s' but got '%s'", i, test.expect, actual)
} }