Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Francia <steve.francia@gmail.com>2015-12-09 00:41:36 +0300
committerSteve Francia <steve.francia@gmail.com>2015-12-09 00:46:43 +0300
commit6042fc2b83453ee92a3585848fdcac1a617bcb95 (patch)
tree10871c4a55480f56f1ebb2dab8213c240f56a3de /helpers
parentde14ceecc9dd23e6924f3649eba0de7d7fb5d2e0 (diff)
move some of the path helper utilities to afero
and provide wrappers in Hugo.
Diffstat (limited to 'helpers')
-rw-r--r--helpers/path.go175
-rw-r--r--helpers/path_test.go6
2 files changed, 32 insertions, 149 deletions
diff --git a/helpers/path.go b/helpers/path.go
index 079b84e99..ae33d2900 100644
--- a/helpers/path.go
+++ b/helpers/path.go
@@ -16,17 +16,17 @@ package helpers
import (
"errors"
"fmt"
- "github.com/spf13/afero"
- jww "github.com/spf13/jwalterweatherman"
- "github.com/spf13/viper"
- "golang.org/x/text/transform"
- "golang.org/x/text/unicode/norm"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"unicode"
+
+ "github.com/spf13/afero"
+ "github.com/spf13/viper"
+ "golang.org/x/text/transform"
+ "golang.org/x/text/unicode/norm"
)
// filepathPathBridge is a bridge for common functionality in filepath vs path
@@ -125,74 +125,6 @@ func ReplaceExtension(path string, newExt string) string {
return f + "." + newExt
}
-// DirExists checks if a path exists and is a directory.
-func DirExists(path string, fs afero.Fs) (bool, error) {
- fi, err := fs.Stat(path)
- if err == nil && fi.IsDir() {
- return true, nil
- }
- if os.IsNotExist(err) {
- return false, nil
- }
- return false, err
-}
-
-// IsDir checks if a given path is a directory.
-func IsDir(path string, fs afero.Fs) (bool, error) {
- fi, err := fs.Stat(path)
- if err != nil {
- return false, err
- }
- return fi.IsDir(), nil
-}
-
-// IsEmpty checks if a given path is empty.
-func IsEmpty(path string, fs afero.Fs) (bool, error) {
- if b, _ := Exists(path, fs); !b {
- return false, fmt.Errorf("%q path does not exist", path)
- }
- fi, err := fs.Stat(path)
- if err != nil {
- return false, err
- }
- if fi.IsDir() {
- f, err := os.Open(path)
- // FIX: Resource leak - f.close() should be called here by defer or is missed
- // if the err != nil branch is taken.
- defer f.Close()
- if err != nil {
- return false, err
- }
- list, err := f.Readdir(-1)
- // f.Close() - see bug fix above
- return len(list) == 0, nil
- }
- return fi.Size() == 0, nil
-}
-
-// Check if a file contains a specified string.
-func FileContains(filename string, subslice []byte, fs afero.Fs) (bool, error) {
- f, err := os.Open(filename)
- if err != nil {
- return false, err
- }
- defer f.Close()
-
- return ReaderContains(f, subslice), nil
-}
-
-// Check if a file or directory exists.
-func Exists(path string, fs afero.Fs) (bool, error) {
- _, err := fs.Stat(path)
- if err == nil {
- return true, nil
- }
- if os.IsNotExist(err) {
- return false, nil
- }
- return false, err
-}
-
func AbsPathify(inPath string) string {
if filepath.IsAbs(inPath) {
return filepath.Clean(inPath)
@@ -498,88 +430,39 @@ func FindCWD() (string, error) {
// Same as WriteToDisk but checks to see if file/directory already exists.
func SafeWriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) {
- dir, _ := filepath.Split(inpath)
- ospath := filepath.FromSlash(dir)
-
- if ospath != "" {
- err = fs.MkdirAll(ospath, 0777) // rwx, rw, r
- if err != nil {
- return
- }
- }
-
- exists, err := Exists(inpath, fs)
- if err != nil {
- return
- }
- if exists {
- return fmt.Errorf("%v already exists", inpath)
- }
-
- file, err := fs.Create(inpath)
- if err != nil {
- return
- }
- defer file.Close()
-
- _, err = io.Copy(file, r)
- return
+ return afero.SafeWriteReader(fs, inpath, r)
}
// Writes content to disk.
func WriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) {
- dir, _ := filepath.Split(inpath)
- ospath := filepath.FromSlash(dir)
-
- if ospath != "" {
- err = fs.MkdirAll(ospath, 0777) // rwx, rw, r
- if err != nil {
- if err != os.ErrExist {
- jww.FATAL.Fatalln(err)
- }
- }
- }
+ return afero.WriteReader(fs, inpath, r)
+}
- file, err := fs.Create(inpath)
- if err != nil {
- return
- }
- defer file.Close()
+func GetTempDir(subPath string, fs afero.Fs) string {
+ return afero.GetTempDir(fs, subPath)
+}
- _, err = io.Copy(file, r)
- return
+// DirExists checks if a path exists and is a directory.
+func DirExists(path string, fs afero.Fs) (bool, error) {
+ return afero.DirExists(fs, path)
}
-// GetTempDir returns the OS default temp directory with trailing slash
-// if subPath is not empty then it will be created recursively with mode 777 rwx rwx rwx
-func GetTempDir(subPath string, fs afero.Fs) string {
- addSlash := func(p string) string {
- if FilePathSeparator != p[len(p)-1:] {
- p = p + FilePathSeparator
- }
- return p
- }
- dir := addSlash(os.TempDir())
+// IsDir checks if a given path is a directory.
+func IsDir(path string, fs afero.Fs) (bool, error) {
+ return afero.IsDir(fs, path)
+}
- if subPath != "" {
- // preserve windows backslash :-(
- if FilePathSeparator == "\\" {
- subPath = strings.Replace(subPath, "\\", "____", -1)
- }
- dir = dir + MakePath(subPath)
- if FilePathSeparator == "\\" {
- dir = strings.Replace(dir, "____", "\\", -1)
- }
+// IsEmpty checks if a given path is empty.
+func IsEmpty(path string, fs afero.Fs) (bool, error) {
+ return afero.IsEmpty(fs, path)
+}
- if exists, _ := Exists(dir, fs); exists {
- return addSlash(dir)
- }
+// Check if a file contains a specified string.
+func FileContains(filename string, subslice []byte, fs afero.Fs) (bool, error) {
+ return afero.FileContainsBytes(fs, filename, subslice)
+}
- err := fs.MkdirAll(dir, 0777)
- if err != nil {
- panic(err)
- }
- dir = addSlash(dir)
- }
- return dir
+// Check if a file or directory exists.
+func Exists(path string, fs afero.Fs) (bool, error) {
+ return afero.Exists(fs, path)
}
diff --git a/helpers/path_test.go b/helpers/path_test.go
index a1d897df9..6a3d58053 100644
--- a/helpers/path_test.go
+++ b/helpers/path_test.go
@@ -755,13 +755,13 @@ func TestGetTempDir(t *testing.T) {
expected string
}{
{"", dir},
- {testDir + " Foo bar ", dir + testDir + "--Foo-bar" + FilePathSeparator},
+ {testDir + " Foo bar ", dir + testDir + " Foo bar " + FilePathSeparator},
{testDir + "Foo.Bar/foo_Bar-Foo", dir + testDir + "Foo.Bar/foo_Bar-Foo" + FilePathSeparator},
- {testDir + "fOO,bar:foo%bAR", dir + testDir + "fOObarfoobAR" + FilePathSeparator},
+ {testDir + "fOO,bar:foo%bAR", dir + testDir + "fOObarfoo%bAR" + FilePathSeparator},
{testDir + "FOo/BaR.html", dir + testDir + "FOo/BaR.html" + FilePathSeparator},
{testDir + "трям/трям", dir + testDir + "трям/трям" + FilePathSeparator},
{testDir + "은행", dir + testDir + "은행" + FilePathSeparator},
- {testDir + "Банковский кассир", dir + testDir + "Банковский-кассир" + FilePathSeparator},
+ {testDir + "Банковский кассир", dir + testDir + "Банковский кассир" + FilePathSeparator},
}
for _, test := range tests {