diff options
author | Mark Sargent <99003+sarge@users.noreply.github.com> | 2020-01-10 15:34:22 +1300 |
---|---|---|
committer | Matt Holt <mholt@users.noreply.github.com> | 2020-01-09 19:34:22 -0700 |
commit | 871abf1053e99274e7336db3143ca5eb17013e38 (patch) | |
tree | 533fc30739dcb56d6ea3ee7065e28983acd53ea5 /caddyconfig/caddyfile/parse.go | |
parent | 29315847a8e6c17503408ac74567942533fc3d48 (diff) |
caddyfile: fix replacing variables on imported files (#2970)
* fix replacing variables on imported files
* refactored replaceEnvVars to ensure it is always called
* Use byte slices for easier use
Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
Diffstat (limited to 'caddyconfig/caddyfile/parse.go')
-rwxr-xr-x | caddyconfig/caddyfile/parse.go | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/caddyconfig/caddyfile/parse.go b/caddyconfig/caddyfile/parse.go index 5792cf8..f376033 100755 --- a/caddyconfig/caddyfile/parse.go +++ b/caddyconfig/caddyfile/parse.go @@ -16,7 +16,7 @@ package caddyfile import ( "bytes" - "io" + "io/ioutil" "log" "os" "path/filepath" @@ -33,8 +33,7 @@ import ( // Environment variables in {$ENVIRONMENT_VARIABLE} notation // will be replaced before parsing begins. func Parse(filename string, input []byte) ([]ServerBlock, error) { - input = replaceEnvVars(input) - tokens, err := allTokens(filename, bytes.NewReader(input)) + tokens, err := allTokens(filename, input) if err != nil { return nil, err } @@ -43,7 +42,7 @@ func Parse(filename string, input []byte) ([]ServerBlock, error) { } // replaceEnvVars replaces all occurrences of environment variables. -func replaceEnvVars(input []byte) []byte { +func replaceEnvVars(input []byte) ([]byte, error) { var offset int for { begin := bytes.Index(input[offset:], spanOpen) @@ -74,15 +73,19 @@ func replaceEnvVars(input []byte) []byte { // continue at the end of the replacement offset = begin + len(envVarValue) } - return input + return input, nil } // allTokens lexes the entire input, but does not parse it. // It returns all the tokens from the input, unstructured // and in order. -func allTokens(filename string, input io.Reader) ([]Token, error) { +func allTokens(filename string, input []byte) ([]Token, error) { + input, err := replaceEnvVars(input) + if err != nil { + return nil, err + } l := new(lexer) - err := l.load(input) + err = l.load(bytes.NewReader(input)) if err != nil { return nil, err } @@ -368,7 +371,12 @@ func (p *parser) doSingleImport(importFile string) ([]Token, error) { return nil, p.Errf("Could not import %s: is a directory", importFile) } - importedTokens, err := allTokens(importFile, file) + input, err := ioutil.ReadAll(file) + if err != nil { + return nil, p.Errf("Could not read imported file %s: %v", importFile, err) + } + + importedTokens, err := allTokens(importFile, input) if err != nil { return nil, p.Errf("Could not read tokens while importing %s: %v", importFile, err) } |