0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

Import paths now relative to Caddyfile (closes #867)

This is inconsistent with the other directives, but import is a special
case and frankly the behavior of import shouldn't change depending
on the directory from which you run caddy. Breaking change but I think
it's for the better, and best to do it now before 1.0.
This commit is contained in:
Matthew Holt 2016-06-28 22:39:29 -06:00
parent a1bc94e409
commit d2be213e10
No known key found for this signature in database
GPG key ID: 0D97CC73664F4D03
2 changed files with 17 additions and 7 deletions

View file

@ -210,8 +210,16 @@ func (p *parser) doImport() error {
return p.Err("Import takes only one argument (glob pattern or file)") return p.Err("Import takes only one argument (glob pattern or file)")
} }
// do glob // make path relative to Caddyfile rather than current working directory (issue #867)
matches, err := filepath.Glob(importPattern) // and then use glob to get list of matching filenames
var matches []string
relImportPattern, err := filepath.Rel(filepath.Dir(p.Dispenser.filename), importPattern)
if err == nil {
matches, err = filepath.Glob(relImportPattern)
} else {
matches, err = filepath.Glob(importPattern)
}
if err != nil { if err != nil {
return p.Errf("Failed to use import pattern %s: %v", importPattern, err) return p.Errf("Failed to use import pattern %s: %v", importPattern, err)
} }

View file

@ -187,7 +187,7 @@ func TestParseOneAndImport(t *testing.T) {
{`localhost {`localhost
dir1 arg1 dir1 arg1
import testdata/import_test1.txt`, false, []string{ import ../testdata/import_test1.txt`, false, []string{
"localhost", "localhost",
}, map[string]int{ }, map[string]int{
"dir1": 2, "dir1": 2,
@ -195,16 +195,16 @@ func TestParseOneAndImport(t *testing.T) {
"dir3": 1, "dir3": 1,
}}, }},
{`import testdata/import_test2.txt`, false, []string{ {`import ../testdata/import_test2.txt`, false, []string{
"host1", "host1",
}, map[string]int{ }, map[string]int{
"dir1": 1, "dir1": 1,
"dir2": 2, "dir2": 2,
}}, }},
{`import testdata/import_test1.txt testdata/import_test2.txt`, true, []string{}, map[string]int{}}, {`import ../testdata/import_test1.txt ../testdata/import_test2.txt`, true, []string{}, map[string]int{}},
{`import testdata/not_found.txt`, true, []string{}, map[string]int{}}, {`import ../testdata/not_found.txt`, true, []string{}, map[string]int{}},
{`""`, false, []string{}, map[string]int{}}, {`""`, false, []string{}, map[string]int{}},
@ -394,6 +394,8 @@ func TestEnvironmentReplacement(t *testing.T) {
func testParser(input string) parser { func testParser(input string) parser {
buf := strings.NewReader(input) buf := strings.NewReader(input)
p := parser{Dispenser: NewDispenser("Test", buf)} // use relative path to test that import paths are relative to
// the Caddyfile rather than the current working directory (issue #867)
p := parser{Dispenser: NewDispenser("../Test", buf)}
return p return p
} }