mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-31 22:41:53 +02:00
Add ReadDir function to list local files.
Includes documentation.
This commit is contained in:
committed by
Bjørn Erik Pedersen
parent
72ecd0cdc7
commit
81e69c416d
@@ -1353,6 +1353,7 @@ func init() {
|
||||
"dateFormat": DateFormat,
|
||||
"getJSON": GetJSON,
|
||||
"getCSV": GetCSV,
|
||||
"ReadDir": ReadDir,
|
||||
"seq": helpers.Seq,
|
||||
"getenv": func(varName string) string { return os.Getenv(varName) },
|
||||
|
||||
|
@@ -21,6 +21,8 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -263,3 +265,25 @@ 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 contains parent directory marker ..\n", 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
|
||||
}
|
||||
|
Reference in New Issue
Block a user