Add sanity checks for hugo import jekyll

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

Fixes #1625
This commit is contained in:
Anthony Fok 2015-12-02 23:51:48 -07:00
parent 00d04774f0
commit c1b5a48f4e

View file

@ -57,6 +57,10 @@ Import from Jekyll requires two paths, e.g. ` + "`hugo import jekyll jekyll_root
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 @@ func importFromJekyll(cmd *cobra.Command, args []string) error {
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 @@ func importFromJekyll(cmd *cobra.Command, args []string) error {
if err != nil {
return err
} else {
fmt.Println("Congratulations!", fileCount, "posts imported!")
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 @@ func createSiteFromJekyll(jekyllRoot, targetDir string) {
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 @@ func loadJekyllConfig(jekyllRoot string) map[string]interface{} {
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
}