shithub: hugo

Download patch

ref: c1b5a48f4e3d91b38156006e5362bf2b545818db
parent: 00d04774f0d9789262557af6a6465d8b6271839d
author: Anthony Fok <[email protected]>
date: Wed Dec 2 18:51:48 EST 2015

Add sanity checks for `hugo import jekyll`

Also add `--force` flag copied from `hugo new site`.

Fixes #1625

--- a/commands/import_jekyll.go
+++ b/commands/import_jekyll.go
@@ -57,6 +57,10 @@
 	RunE: importFromJekyll,
 }
 
+func init() {
+	importJekyllCmd.Flags().Bool("force", false, "Allow import into non-empty target directory")
+}
+
 func importFromJekyll(cmd *cobra.Command, args []string) error {
 	jww.SetLogThreshold(jww.LevelTrace)
 	jww.SetStdoutThreshold(jww.LevelWarn)
@@ -75,9 +79,17 @@
 		return newUserError("Path error:", args[1])
 	}
 
-	createSiteFromJekyll(jekyllRoot, targetDir)
-
 	jww.INFO.Println("Import Jekyll from:", jekyllRoot, "to:", targetDir)
+
+	if strings.HasPrefix(targetDir, jekyllRoot) {
+		return newUserError("Target path should not be inside the Jekyll root, aborting.")
+	}
+
+	forceImport, _ := cmd.Flags().GetBool("force")
+	if err := createSiteFromJekyll(jekyllRoot, targetDir, forceImport); err != nil {
+		return newUserError(err)
+	}
+
 	fmt.Println("Importing...")
 
 	fileCount := 0
@@ -117,16 +129,50 @@
 	if err != nil {
 		return err
 	} else {
-		fmt.Println("Congratulations!", fileCount, "posts imported!")
-		fmt.Println("Now, start Hugo by yourself: \n" +
+		fmt.Println("Congratulations!", fileCount, "post(s) imported!")
+		fmt.Println("Now, start Hugo by yourself:\n" +
 			"$ git clone https://github.com/spf13/herring-cove.git " + args[1] + "/themes/herring-cove")
-		fmt.Println("$ cd " + args[1] + "\n$ hugo server -w --theme=herring-cove")
+		fmt.Println("$ cd " + args[1] + "\n$ hugo server --theme=herring-cove")
 	}
 
 	return nil
 }
 
-func createSiteFromJekyll(jekyllRoot, targetDir string) {
+// TODO: Consider calling doNewSite() instead?
+func createSiteFromJekyll(jekyllRoot, targetDir string, force bool) error {
+	fs := hugofs.SourceFs
+	if exists, _ := helpers.Exists(targetDir, fs); exists {
+		if isDir, _ := helpers.IsDir(targetDir, fs); !isDir {
+			return errors.New("Target path \"" + targetDir + "\" already exists but not a directory")
+		}
+
+		isEmpty, _ := helpers.IsEmpty(targetDir, fs)
+
+		if !isEmpty && !force {
+			return errors.New("Target path \"" + targetDir + "\" already exists and is not empty")
+		}
+	}
+
+	jekyllConfig := loadJekyllConfig(jekyllRoot)
+
+	// Crude test to make sure at least one of _drafts/ and _posts/ exists
+	// and is not empty.
+	hasPostsOrDrafts := false
+	postsDir := filepath.Join(jekyllRoot, "_posts")
+	draftsDir := filepath.Join(jekyllRoot, "_drafts")
+	for _, d := range []string{postsDir, draftsDir} {
+		if exists, _ := helpers.Exists(d, fs); exists {
+			if isDir, _ := helpers.IsDir(d, fs); isDir {
+				if isEmpty, _ := helpers.IsEmpty(d, fs); !isEmpty {
+					hasPostsOrDrafts = true
+				}
+			}
+		}
+	}
+	if !hasPostsOrDrafts {
+		return errors.New("Your Jekyll root contains neither posts nor drafts, aborting.")
+	}
+
 	mkdir(targetDir, "layouts")
 	mkdir(targetDir, "content")
 	mkdir(targetDir, "archetypes")
@@ -134,10 +180,11 @@
 	mkdir(targetDir, "data")
 	mkdir(targetDir, "themes")
 
-	jekyllConfig := loadJekyllConfig(jekyllRoot)
 	createConfigFromJekyll(targetDir, "yaml", jekyllConfig)
 
 	copyJekyllFilesAndFolders(jekyllRoot, filepath.Join(targetDir, "static"))
+
+	return nil
 }
 
 func loadJekyllConfig(jekyllRoot string) map[string]interface{} {
@@ -147,6 +194,7 @@
 	exists, err := helpers.Exists(path, fs)
 
 	if err != nil || !exists {
+		jww.WARN.Println("_config.yaml not found: Is the specified Jekyll root correct?")
 		return nil
 	}