From cb89ae63e948709aa68d0ccc2a2438794be4f38d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 31 Mar 2016 23:06:51 +0200 Subject: [PATCH] tpl: Make readDir use the WorkingDir fs Fixes #2010 --- tpl/template_funcs.go | 17 ++++++++++++++++- tpl/template_resources.go | 23 ----------------------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/tpl/template_funcs.go b/tpl/template_funcs.go index 8abd70b8c..e9cc26869 100644 --- a/tpl/template_funcs.go +++ b/tpl/template_funcs.go @@ -1501,6 +1501,21 @@ func readFileFromWorkingDir(i interface{}) (string, error) { return readFile(hugofs.WorkingDir(), cast.ToString(i)) } +// readDirFromWorkingDir listst the directory content relative to the +// configured WorkingDir. +func readDirFromWorkingDir(i interface{}) ([]os.FileInfo, error) { + + path := cast.ToString(i) + + list, err := afero.ReadDir(hugofs.WorkingDir(), path) + + if err != nil { + return nil, fmt.Errorf("Failed to read Directory %s with error message %s", path, err) + } + + return list, nil +} + // safeHTMLAttr returns a given string as html/template HTMLAttr content. // // safeHTMLAttr is currently disabled, pending further discussion @@ -1722,7 +1737,7 @@ func init() { "partial": partial, "plainify": plainify, "pluralize": pluralize, - "readDir": readDir, + "readDir": readDirFromWorkingDir, "readFile": readFileFromWorkingDir, "ref": ref, "relURL": func(a string) template.HTML { return template.HTML(helpers.RelURL(a)) }, diff --git a/tpl/template_resources.go b/tpl/template_resources.go index 0d0ea8332..76a83a87f 100644 --- a/tpl/template_resources.go +++ b/tpl/template_resources.go @@ -21,7 +21,6 @@ import ( "io/ioutil" "net/http" "net/url" - "os" "path/filepath" "strings" "sync" @@ -259,25 +258,3 @@ func getCSV(sep string, urlParts ...string) [][]string { } return d } - -func readDir(path string) []os.FileInfo { - wd := "" - p := "" - if viper.GetString("WorkingDir") != "" { - wd = viper.GetString("WorkingDir") - } - if strings.Contains(path, "..") { - jww.ERROR.Printf("Path %s contains parent directory marker", path) - return nil - } - - p = filepath.Clean(path) - p = filepath.Join(wd, p) - - list, err := ioutil.ReadDir(p) - if err != nil { - jww.ERROR.Printf("Failed to read Directory %s with error message %s", path, err) - return nil - } - return list -}