From 12f594779c9b8a23b69aefe5363e1eac087db646 Mon Sep 17 00:00:00 2001 From: Radim Marek Date: Fri, 4 Dec 2015 23:04:12 +0100 Subject: [PATCH 1/6] Added support magic characters in import pattern Import now allows to use the star wildcard, question mark and square brackets as used by filepath.Glob --- caddy/parse/parsing.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/caddy/parse/parsing.go b/caddy/parse/parsing.go index 03d9d800a..e71a03f5b 100644 --- a/caddy/parse/parsing.go +++ b/caddy/parse/parsing.go @@ -184,11 +184,26 @@ func (p *parser) doImport() error { if !p.NextArg() { return p.ArgErr() } - importFile := p.Val() + importPattern := p.Val() if p.NextArg() { return p.Err("Import allows only one file to import") } + matches, err := filepath.Glob(importPattern) + if err != nil { + return p.Errf("Failed to use import pattern %s - %s", importPattern, err.Error()) + } + + for _, importFile := range matches { + if err := p.doSingleImport(importFile); err != nil { + return err + } + } + + return nil +} + +func (p *parser) doSingleImport(importFile string) error { file, err := os.Open(importFile) if err != nil { return p.Errf("Could not import %s - %v", importFile, err) From d1216f409dd8f7278757be951277758347e66172 Mon Sep 17 00:00:00 2001 From: Radim Marek Date: Fri, 4 Dec 2015 23:21:28 +0100 Subject: [PATCH 2/6] Handle no matches --- caddy/parse/parsing.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/caddy/parse/parsing.go b/caddy/parse/parsing.go index e71a03f5b..8aa711c3f 100644 --- a/caddy/parse/parsing.go +++ b/caddy/parse/parsing.go @@ -194,6 +194,10 @@ func (p *parser) doImport() error { return p.Errf("Failed to use import pattern %s - %s", importPattern, err.Error()) } + if len(matches) == 0 { + return p.Errf("No files matching the import pattern %s", importPattern) + } + for _, importFile := range matches { if err := p.doSingleImport(importFile); err != nil { return err From d56a9a1c5dcbfa3191de905884c7016feb168936 Mon Sep 17 00:00:00 2001 From: Radim Marek Date: Sun, 6 Dec 2015 23:49:21 +0100 Subject: [PATCH 3/6] Correct position of the newly imported tokens --- caddy/parse/parsing.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/caddy/parse/parsing.go b/caddy/parse/parsing.go index 8aa711c3f..e011fd8cc 100644 --- a/caddy/parse/parsing.go +++ b/caddy/parse/parsing.go @@ -198,12 +198,23 @@ func (p *parser) doImport() error { return p.Errf("No files matching the import pattern %s", importPattern) } + // Splice out the import directive and its argument (2 tokens total) + // and insert the imported tokens in their place. + tokensBefore := p.tokens[:p.cursor-1] + tokensAfter := p.tokens[p.cursor+1:] + // cursor was advanced one position to read filename; rewind it + p.cursor-- + + p.tokens = tokensBefore + for _, importFile := range matches { if err := p.doSingleImport(importFile); err != nil { return err } } + p.tokens = append(p.tokens, append(tokensAfter)...) + return nil } @@ -222,10 +233,7 @@ func (p *parser) doSingleImport(importFile string) error { // Splice out the import directive and its argument (2 tokens total) // and insert the imported tokens in their place. - tokensBefore := p.tokens[:p.cursor-1] - tokensAfter := p.tokens[p.cursor+1:] - p.tokens = append(tokensBefore, append(importedTokens, tokensAfter...)...) - p.cursor-- // cursor was advanced one position to read the filename; rewind it + p.tokens = append(p.tokens, append(importedTokens)...) return nil } From afbda595f6c031ebb09b013858895167b79803c9 Mon Sep 17 00:00:00 2001 From: Radim Marek Date: Fri, 11 Dec 2015 21:47:38 +0100 Subject: [PATCH 4/6] import glob tests --- caddy/parse/import_glob0.txt | 6 ++++++ caddy/parse/import_glob1.txt | 4 ++++ caddy/parse/import_glob2.txt | 3 +++ caddy/parse/parsing_test.go | 7 +++++++ 4 files changed, 20 insertions(+) create mode 100644 caddy/parse/import_glob0.txt create mode 100644 caddy/parse/import_glob1.txt create mode 100644 caddy/parse/import_glob2.txt diff --git a/caddy/parse/import_glob0.txt b/caddy/parse/import_glob0.txt new file mode 100644 index 000000000..e610b5e7c --- /dev/null +++ b/caddy/parse/import_glob0.txt @@ -0,0 +1,6 @@ +glob0.host0 { + dir2 arg1 +} + +glob0.host1 { +} diff --git a/caddy/parse/import_glob1.txt b/caddy/parse/import_glob1.txt new file mode 100644 index 000000000..111eb044d --- /dev/null +++ b/caddy/parse/import_glob1.txt @@ -0,0 +1,4 @@ +glob1.host0 { + dir1 + dir2 arg1 +} diff --git a/caddy/parse/import_glob2.txt b/caddy/parse/import_glob2.txt new file mode 100644 index 000000000..c09f784ec --- /dev/null +++ b/caddy/parse/import_glob2.txt @@ -0,0 +1,3 @@ +glob2.host0 { + dir2 arg1 +} diff --git a/caddy/parse/parsing_test.go b/caddy/parse/parsing_test.go index 97c86808a..bda6b29bc 100644 --- a/caddy/parse/parsing_test.go +++ b/caddy/parse/parsing_test.go @@ -329,6 +329,13 @@ func TestParseAll(t *testing.T) { []address{{"host1.com", "http"}, {"host2.com", "http"}}, []address{{"host3.com", "https"}, {"host4.com", "https"}}, }}, + + {`import import_glob*.txt`, false, [][]address{ + []address{{"glob0.host0", ""}}, + []address{{"glob0.host1", ""}}, + []address{{"glob1.host0", ""}}, + []address{{"glob2.host0", ""}}, + }}, } { p := testParser(test.input) blocks, err := p.parseAll() From eb48885d4d24305e183223dbe3e9d947125d3274 Mon Sep 17 00:00:00 2001 From: Radim Marek Date: Fri, 11 Dec 2015 22:02:31 +0100 Subject: [PATCH 5/6] Updated comments --- caddy/parse/parsing.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/caddy/parse/parsing.go b/caddy/parse/parsing.go index e011fd8cc..db4b7e425 100644 --- a/caddy/parse/parsing.go +++ b/caddy/parse/parsing.go @@ -176,10 +176,11 @@ func (p *parser) directives() error { } // doImport swaps out the import directive and its argument -// (a total of 2 tokens) with the tokens in the file specified. -// When the function returns, the cursor is on the token before -// where the import directive was. In other words, call Next() -// to access the first token that was imported. +// (a total of 2 tokens) with the tokens in the specified file +// or globbing pattern. When the function returns, the cursor +// is on the token before where the import directive was. In +// other words, call Next() to access the first token that was +// imported. func (p *parser) doImport() error { if !p.NextArg() { return p.ArgErr() @@ -218,6 +219,8 @@ func (p *parser) doImport() error { return nil } +// doSingleImport lexes the individual files matching the +// globbing pattern from of the import directive. func (p *parser) doSingleImport(importFile string) error { file, err := os.Open(importFile) if err != nil { From 1e7ec3397b8e67d9e0f0879a2238b556b7d51988 Mon Sep 17 00:00:00 2001 From: Radim Marek Date: Tue, 29 Dec 2015 23:32:59 +0100 Subject: [PATCH 6/6] Import allows only one expression --- caddy/parse/parsing.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/caddy/parse/parsing.go b/caddy/parse/parsing.go index db4b7e425..6ef908b0b 100644 --- a/caddy/parse/parsing.go +++ b/caddy/parse/parsing.go @@ -187,7 +187,7 @@ func (p *parser) doImport() error { } importPattern := p.Val() if p.NextArg() { - return p.Err("Import allows only one file to import") + return p.Err("Import allows only one expression, either file or glob pattern") } matches, err := filepath.Glob(importPattern)