new site works in an empty directory now
authorspf13 <steve.francia@gmail.com>
Mon, 19 May 2014 13:16:40 +0000 (09:16 -0400)
committerspf13 <steve.francia@gmail.com>
Mon, 19 May 2014 13:16:40 +0000 (09:16 -0400)
commands/new.go
helpers/path.go

index 1c3e8e9e59f5112ef3cae39cf71be6bacd24abe2..68ddbb4f66bfcf382e3dc9db4c8cc63d428193dc 100644 (file)
@@ -110,7 +110,12 @@ func NewSite(cmd *cobra.Command, args []string) {
        }
 
        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")
index 119974a3c9a044a76686571b57c7169c5a8e8376..0cd56e0d991ee58e0f4f9f65fc039dc93c1515a1 100644 (file)
@@ -72,6 +72,35 @@ func DirExists(path string) (bool, error) {
        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)