shithub: hugo

Download patch

ref: 6042fc2b83453ee92a3585848fdcac1a617bcb95
parent: de14ceecc9dd23e6924f3649eba0de7d7fb5d2e0
author: Steve Francia <[email protected]>
date: Tue Dec 8 11:41:36 EST 2015

move some of the path helper utilities to afero
and provide wrappers in Hugo.

--- a/helpers/path.go
+++ b/helpers/path.go
@@ -16,11 +16,6 @@
 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"
@@ -27,6 +22,11 @@
 	"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 @@
 	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 @@
 
 // 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)
+	return afero.WriteReader(fs, inpath, r)
+}
 
-	if ospath != "" {
-		err = fs.MkdirAll(ospath, 0777) // rwx, rw, r
-		if err != nil {
-			if err != os.ErrExist {
-				jww.FATAL.Fatalln(err)
-			}
-		}
-	}
+func GetTempDir(subPath string, fs afero.Fs) string {
+	return afero.GetTempDir(fs, subPath)
+}
 
-	file, err := fs.Create(inpath)
-	if err != nil {
-		return
-	}
-	defer file.Close()
+// DirExists checks if a path exists and is a directory.
+func DirExists(path string, fs afero.Fs) (bool, error) {
+	return afero.DirExists(fs, path)
+}
 
-	_, err = io.Copy(file, r)
-	return
+// IsDir checks if a given path is a directory.
+func IsDir(path string, fs afero.Fs) (bool, error) {
+	return afero.IsDir(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())
+// IsEmpty checks if a given path is empty.
+func IsEmpty(path string, fs afero.Fs) (bool, error) {
+	return afero.IsEmpty(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)
-		}
+// 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)
+}
 
-		if exists, _ := Exists(dir, fs); exists {
-			return addSlash(dir)
-		}
-
-		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)
 }
--- a/helpers/path_test.go
+++ b/helpers/path_test.go
@@ -755,13 +755,13 @@
 		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 {