From fdf2a77feb53cb9dd6de772f811e96a5f6b2d2c4 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Mon, 1 Jun 2020 12:43:06 -0400 Subject: caddyfile: Add args on imports (#3423) * caddyfile: Add support for args on imports * caddyfile: Add more import args tests --- caddyconfig/caddyfile/parse.go | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) (limited to 'caddyconfig/caddyfile/parse.go') diff --git a/caddyconfig/caddyfile/parse.go b/caddyconfig/caddyfile/parse.go index cdcac26..5cb5b68 100755 --- a/caddyconfig/caddyfile/parse.go +++ b/caddyconfig/caddyfile/parse.go @@ -20,7 +20,10 @@ import ( "log" "os" "path/filepath" + "strconv" "strings" + + "github.com/caddyserver/caddy/v2" ) // Parse parses the input just enough to group tokens, in @@ -292,11 +295,19 @@ func (p *parser) doImport() error { if importPattern == "" { return p.Err("Import requires a non-empty filepath") } - if p.NextArg() { - return p.Err("Import takes only one argument (glob pattern or file)") + + // grab remaining args as placeholder replacements + args := p.RemainingArgs() + + // add args to the replacer + repl := caddy.NewReplacer() + for index, arg := range args { + repl.Set("args."+strconv.Itoa(index), arg) } - // splice out the import directive and its argument (2 tokens total) - tokensBefore := p.tokens[:p.cursor-1] + + // splice out the import directive and its arguments + // (2 tokens, plus the length of args) + tokensBefore := p.tokens[:p.cursor-1-len(args)] tokensAfter := p.tokens[p.cursor+1:] var importedTokens []Token @@ -348,10 +359,20 @@ func (p *parser) doImport() error { } } + // copy the tokens so we don't overwrite p.definedSnippets + tokensCopy := make([]Token, len(importedTokens)) + copy(tokensCopy, importedTokens) + + // run the argument replacer on the tokens + for index, token := range tokensCopy { + token.Text = repl.ReplaceKnown(token.Text, "") + tokensCopy[index] = token + } + // splice the imported tokens in the place of the import statement // and rewind cursor so Next() will land on first imported token - p.tokens = append(tokensBefore, append(importedTokens, tokensAfter...)...) - p.cursor-- + p.tokens = append(tokensBefore, append(tokensCopy, tokensAfter...)...) + p.cursor -= len(args) + 1 return nil } -- cgit v1.2.3