ref: 2a2c9838671b5401331d20f8c72e2b934fe34e8d
parent: 15b1e269ade91ddc6a74c552bc61b0c5e527d268
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Mon Apr 9 16:37:17 EDT 2018
commands: Make the import commands non-global See #4598
--- a/commands/hugo.go
+++ b/commands/hugo.go
@@ -202,7 +202,7 @@
HugoCmd.AddCommand(newConvertCmd().getCommand())
HugoCmd.AddCommand(newNewCmd().getCommand())
HugoCmd.AddCommand(listCmd)
- HugoCmd.AddCommand(importCmd)
+ HugoCmd.AddCommand(newImportCmd().getCommand())
HugoCmd.AddCommand(genCmd)
genCmd.AddCommand(genautocompleteCmd)
--- a/commands/import_jekyll.go
+++ b/commands/import_jekyll.go
@@ -35,33 +35,49 @@
jww "github.com/spf13/jwalterweatherman"
)
-func init() {- importCmd.AddCommand(importJekyllCmd)
+var _ cmder = (*newThemeCmd)(nil)
+
+type importCmd struct {+ cmd *cobra.Command
}
-var importCmd = &cobra.Command{- Use: "import",
- Short: "Import your site from others.",
- Long: `Import your site from other web site generators like Jekyll.
+func (c *importCmd) getCommand() *cobra.Command {+ return c.cmd
+}
+func newImportCmd() *importCmd {+ cc := &importCmd{}+
+ cc.cmd = &cobra.Command{+ Use: "import",
+ Short: "Import your site from others.",
+ Long: `Import your site from other web site generators like Jekyll.
+
Import requires a subcommand, e.g. ` + "`hugo import jekyll jekyll_root_path target_path`.",
- RunE: nil,
-}
+ RunE: nil,
+ }
-var importJekyllCmd = &cobra.Command{- Use: "jekyll",
- Short: "hugo import from Jekyll",
- Long: `hugo import from Jekyll.
+ importJekyllCmd := &cobra.Command{+ Use: "jekyll",
+ Short: "hugo import from Jekyll",
+ Long: `hugo import from Jekyll.
Import from Jekyll requires two paths, e.g. ` + "`hugo import jekyll jekyll_root_path target_path`.",
- RunE: importFromJekyll,
+ RunE: cc.importFromJekyll,
+ }
+
+ importJekyllCmd.Flags().Bool("force", false, "allow import into non-empty target directory")+
+ cc.cmd.AddCommand(importJekyllCmd)
+
+ return cc
+
}
func init() {- importJekyllCmd.Flags().Bool("force", false, "allow import into non-empty target directory")}
-func importFromJekyll(cmd *cobra.Command, args []string) error {+func (i *importCmd) importFromJekyll(cmd *cobra.Command, args []string) error { if len(args) < 2 {return newUserError(`Import from Jekyll requires two paths, e.g. ` + "`hugo import jekyll jekyll_root_path target_path`.")
@@ -86,12 +102,12 @@
forceImport, _ := cmd.Flags().GetBool("force")fs := afero.NewOsFs()
- jekyllPostDirs, hasAnyPost := getJekyllDirInfo(fs, jekyllRoot)
+ jekyllPostDirs, hasAnyPost := i.getJekyllDirInfo(fs, jekyllRoot)
if !hasAnyPost { return errors.New("Your Jekyll root contains neither posts nor drafts, aborting.")}
- site, err := createSiteFromJekyll(jekyllRoot, targetDir, jekyllPostDirs, forceImport)
+ site, err := i.createSiteFromJekyll(jekyllRoot, targetDir, jekyllPostDirs, forceImport)
if err != nil {return newUserError(err)
@@ -147,7 +163,7 @@
return nil
}
-func getJekyllDirInfo(fs afero.Fs, jekyllRoot string) (map[string]bool, bool) {+func (i *importCmd) getJekyllDirInfo(fs afero.Fs, jekyllRoot string) (map[string]bool, bool) {postDirs := make(map[string]bool)
hasAnyPost := false
if entries, err := ioutil.ReadDir(jekyllRoot); err == nil {@@ -154,7 +170,7 @@
for _, entry := range entries { if entry.IsDir() {subDir := filepath.Join(jekyllRoot, entry.Name())
- if isPostDir, hasAnyPostInDir := retrieveJekyllPostDir(fs, subDir); isPostDir {+ if isPostDir, hasAnyPostInDir := i.retrieveJekyllPostDir(fs, subDir); isPostDir {postDirs[entry.Name()] = hasAnyPostInDir
if hasAnyPostInDir {hasAnyPost = true
@@ -166,7 +182,7 @@
return postDirs, hasAnyPost
}
-func retrieveJekyllPostDir(fs afero.Fs, dir string) (bool, bool) {+func (i *importCmd) retrieveJekyllPostDir(fs afero.Fs, dir string) (bool, bool) { if strings.HasSuffix(dir, "_posts") || strings.HasSuffix(dir, "_drafts") {isEmpty, _ := helpers.IsEmpty(dir, fs)
return true, !isEmpty
@@ -176,7 +192,7 @@
for _, entry := range entries { if entry.IsDir() {subDir := filepath.Join(dir, entry.Name())
- if isPostDir, hasAnyPost := retrieveJekyllPostDir(fs, subDir); isPostDir {+ if isPostDir, hasAnyPost := i.retrieveJekyllPostDir(fs, subDir); isPostDir {return isPostDir, hasAnyPost
}
}
@@ -186,7 +202,7 @@
return false, true
}
-func createSiteFromJekyll(jekyllRoot, targetDir string, jekyllPostDirs map[string]bool, force bool) (*hugolib.Site, error) {+func (i *importCmd) createSiteFromJekyll(jekyllRoot, targetDir string, jekyllPostDirs map[string]bool, force bool) (*hugolib.Site, error) {s, err := hugolib.NewSiteDefaultLang()
if err != nil {return nil, err
@@ -205,7 +221,7 @@
}
}
- jekyllConfig := loadJekyllConfig(fs, jekyllRoot)
+ jekyllConfig := i.loadJekyllConfig(fs, jekyllRoot)
mkdir(targetDir, "layouts")
mkdir(targetDir, "content")
@@ -214,14 +230,14 @@
mkdir(targetDir, "data")
mkdir(targetDir, "themes")
- createConfigFromJekyll(fs, targetDir, "yaml", jekyllConfig)
+ i.createConfigFromJekyll(fs, targetDir, "yaml", jekyllConfig)
- copyJekyllFilesAndFolders(jekyllRoot, filepath.Join(targetDir, "static"), jekyllPostDirs)
+ i.copyJekyllFilesAndFolders(jekyllRoot, filepath.Join(targetDir, "static"), jekyllPostDirs)
return s, nil
}
-func loadJekyllConfig(fs afero.Fs, jekyllRoot string) map[string]interface{} {+func (i *importCmd) loadJekyllConfig(fs afero.Fs, jekyllRoot string) map[string]interface{} {path := filepath.Join(jekyllRoot, "_config.yml")
exists, err := helpers.Exists(path, fs)
@@ -253,7 +269,7 @@
return c
}
-func createConfigFromJekyll(fs afero.Fs, inpath string, kind string, jekyllConfig map[string]interface{}) (err error) {+func (i *importCmd) createConfigFromJekyll(fs afero.Fs, inpath string, kind string, jekyllConfig map[string]interface{}) (err error) {title := "My New Hugo Site"
baseURL := "http://example.org/"
@@ -348,7 +364,7 @@
return nil
}
-func copyJekyllFilesAndFolders(jekyllRoot, dest string, jekyllPostDirs map[string]bool) (err error) {+func (i *importCmd) copyJekyllFilesAndFolders(jekyllRoot, dest string, jekyllPostDirs map[string]bool) (err error) {fi, err := os.Stat(jekyllRoot)
if err != nil {return err
--
⑨