shithub: hugo

Download patch

ref: 6b8244ba67cede0c2c95bde695cf3c54002bce74
parent: df4bbcef30250f6bd22e38ee2dc3314f2c786f20
author: spf13 <[email protected]>
date: Mon May 19 05:16:40 EDT 2014

new site works in an empty directory now

--- a/commands/new.go
+++ b/commands/new.go
@@ -110,7 +110,12 @@
 	}
 
 	if x, _ := helpers.Exists(createpath); x {
-		jww.FATAL.Fatalln(createpath, "already exists")
+		y, _ := helpers.IsDir(createpath)
+		if z, _ := helpers.IsEmpty(createpath); y && z {
+			jww.INFO.Println(createpath, "already exists and is empty")
+		} else {
+			jww.FATAL.Fatalln(createpath, "already exists and is not empty")
+		}
 	}
 
 	mkdir(createpath, "layouts")
--- a/helpers/path.go
+++ b/helpers/path.go
@@ -72,6 +72,35 @@
 	return false, err
 }
 
+func IsDir(path string) (bool, error) {
+	fi, err := os.Stat(path)
+	if err != nil {
+		return false, err
+	}
+	return fi.IsDir(), nil
+}
+
+func IsEmpty(path string) (bool, error) {
+	if b, _ := Exists(path); !b {
+		return false, fmt.Errorf("%q path does not exist", path)
+	}
+	fi, err := os.Stat(path)
+	if err != nil {
+		return false, err
+	}
+	if fi.IsDir() {
+		f, err := os.Open(path)
+		if err != nil {
+			return false, err
+		}
+		list, err := f.Readdir(-1)
+		f.Close()
+		return len(list) == 0, nil
+	} else {
+		return fi.Size() == 0, nil
+	}
+}
+
 // Check if File / Directory Exists
 func Exists(path string) (bool, error) {
 	_, err := os.Stat(path)