mirror of
https://github.com/caddyserver/caddy.git
synced 2026-05-31 20:04:48 -04:00
* Add Files action to template context. (#1198) * Fixes to testFiles(). - Set os.ModePerm on directories created during test. - Use filepath.Join() to create directory path. - Use Fatalf, not Fatal. * Make additional fixes to test cases. * Fix test cases to use correct path format. Dir.Open() in net/http requires '/'-separated paths while filepath.Join() may produce paths with different separator. * Remove directory created by test at end of loop. * Close the FileSystem before returning. * Initialize names slice to the number of entries. Also, do not call os.RemoveAll() unless the path to the directory is a valid one.
This commit is contained in:
@@ -288,3 +288,33 @@ func (c Context) Map(values ...interface{}) (map[string]interface{}, error) {
|
||||
}
|
||||
return dict, nil
|
||||
}
|
||||
|
||||
// Files reads and returns a slice of names from the given directory
|
||||
// relative to the root of Context c.
|
||||
func (c Context) Files(name string) ([]string, error) {
|
||||
dir, err := c.Root.Open(path.Clean(name))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer dir.Close()
|
||||
|
||||
stat, err := dir.Stat()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !stat.IsDir() {
|
||||
return nil, fmt.Errorf("%v is not a directory", name)
|
||||
}
|
||||
|
||||
dirInfo, err := dir.Readdir(0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
names := make([]string, len(dirInfo))
|
||||
for i, fileInfo := range dirInfo {
|
||||
names[i] = fileInfo.Name()
|
||||
}
|
||||
|
||||
return names, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user