Add ReadDir function to list local files.

Includes documentation.
This commit is contained in:
Russell Oliver
2015-06-26 20:23:37 +10:00
committed by Bjørn Erik Pedersen
parent 72ecd0cdc7
commit 81e69c416d
4 changed files with 92 additions and 0 deletions

View File

@@ -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) },

View File

@@ -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
}