commands: Make the import commands non-global

See #4598
This commit is contained in:
Bjørn Erik Pedersen 2018-04-09 20:37:17 +02:00
parent 15b1e269ad
commit 2a2c983867
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
2 changed files with 45 additions and 29 deletions

View file

@ -202,7 +202,7 @@ func AddCommands() {
HugoCmd.AddCommand(newConvertCmd().getCommand()) HugoCmd.AddCommand(newConvertCmd().getCommand())
HugoCmd.AddCommand(newNewCmd().getCommand()) HugoCmd.AddCommand(newNewCmd().getCommand())
HugoCmd.AddCommand(listCmd) HugoCmd.AddCommand(listCmd)
HugoCmd.AddCommand(importCmd) HugoCmd.AddCommand(newImportCmd().getCommand())
HugoCmd.AddCommand(genCmd) HugoCmd.AddCommand(genCmd)
genCmd.AddCommand(genautocompleteCmd) genCmd.AddCommand(genautocompleteCmd)

View file

@ -35,33 +35,49 @@ import (
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
) )
func init() { var _ cmder = (*newThemeCmd)(nil)
importCmd.AddCommand(importJekyllCmd)
type importCmd struct {
cmd *cobra.Command
} }
var importCmd = &cobra.Command{ func (c *importCmd) getCommand() *cobra.Command {
Use: "import", return c.cmd
Short: "Import your site from others.", }
Long: `Import your site from other web site generators like Jekyll.
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`.", Import requires a subcommand, e.g. ` + "`hugo import jekyll jekyll_root_path target_path`.",
RunE: nil, RunE: nil,
} }
var importJekyllCmd = &cobra.Command{ importJekyllCmd := &cobra.Command{
Use: "jekyll", Use: "jekyll",
Short: "hugo import from Jekyll", Short: "hugo import from Jekyll",
Long: `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`.", 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() { 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 { if len(args) < 2 {
return newUserError(`Import from Jekyll requires two paths, e.g. ` + "`hugo import jekyll jekyll_root_path target_path`.") return newUserError(`Import from Jekyll requires two paths, e.g. ` + "`hugo import jekyll jekyll_root_path target_path`.")
@ -86,12 +102,12 @@ func importFromJekyll(cmd *cobra.Command, args []string) error {
forceImport, _ := cmd.Flags().GetBool("force") forceImport, _ := cmd.Flags().GetBool("force")
fs := afero.NewOsFs() fs := afero.NewOsFs()
jekyllPostDirs, hasAnyPost := getJekyllDirInfo(fs, jekyllRoot) jekyllPostDirs, hasAnyPost := i.getJekyllDirInfo(fs, jekyllRoot)
if !hasAnyPost { if !hasAnyPost {
return errors.New("Your Jekyll root contains neither posts nor drafts, aborting.") 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 { if err != nil {
return newUserError(err) return newUserError(err)
@ -147,14 +163,14 @@ func importFromJekyll(cmd *cobra.Command, args []string) error {
return nil 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) postDirs := make(map[string]bool)
hasAnyPost := false hasAnyPost := false
if entries, err := ioutil.ReadDir(jekyllRoot); err == nil { if entries, err := ioutil.ReadDir(jekyllRoot); err == nil {
for _, entry := range entries { for _, entry := range entries {
if entry.IsDir() { if entry.IsDir() {
subDir := filepath.Join(jekyllRoot, entry.Name()) 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 postDirs[entry.Name()] = hasAnyPostInDir
if hasAnyPostInDir { if hasAnyPostInDir {
hasAnyPost = true hasAnyPost = true
@ -166,7 +182,7 @@ func getJekyllDirInfo(fs afero.Fs, jekyllRoot string) (map[string]bool, bool) {
return postDirs, hasAnyPost 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") { if strings.HasSuffix(dir, "_posts") || strings.HasSuffix(dir, "_drafts") {
isEmpty, _ := helpers.IsEmpty(dir, fs) isEmpty, _ := helpers.IsEmpty(dir, fs)
return true, !isEmpty return true, !isEmpty
@ -176,7 +192,7 @@ func retrieveJekyllPostDir(fs afero.Fs, dir string) (bool, bool) {
for _, entry := range entries { for _, entry := range entries {
if entry.IsDir() { if entry.IsDir() {
subDir := filepath.Join(dir, entry.Name()) subDir := filepath.Join(dir, entry.Name())
if isPostDir, hasAnyPost := retrieveJekyllPostDir(fs, subDir); isPostDir { if isPostDir, hasAnyPost := i.retrieveJekyllPostDir(fs, subDir); isPostDir {
return isPostDir, hasAnyPost return isPostDir, hasAnyPost
} }
} }
@ -186,7 +202,7 @@ func retrieveJekyllPostDir(fs afero.Fs, dir string) (bool, bool) {
return false, true 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() s, err := hugolib.NewSiteDefaultLang()
if err != nil { if err != nil {
return nil, err return nil, err
@ -205,7 +221,7 @@ func createSiteFromJekyll(jekyllRoot, targetDir string, jekyllPostDirs map[strin
} }
} }
jekyllConfig := loadJekyllConfig(fs, jekyllRoot) jekyllConfig := i.loadJekyllConfig(fs, jekyllRoot)
mkdir(targetDir, "layouts") mkdir(targetDir, "layouts")
mkdir(targetDir, "content") mkdir(targetDir, "content")
@ -214,14 +230,14 @@ func createSiteFromJekyll(jekyllRoot, targetDir string, jekyllPostDirs map[strin
mkdir(targetDir, "data") mkdir(targetDir, "data")
mkdir(targetDir, "themes") 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 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") path := filepath.Join(jekyllRoot, "_config.yml")
exists, err := helpers.Exists(path, fs) exists, err := helpers.Exists(path, fs)
@ -253,7 +269,7 @@ func loadJekyllConfig(fs afero.Fs, jekyllRoot string) map[string]interface{} {
return c 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" title := "My New Hugo Site"
baseURL := "http://example.org/" baseURL := "http://example.org/"
@ -348,7 +364,7 @@ func copyDir(source string, dest string) error {
return nil 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) fi, err := os.Stat(jekyllRoot)
if err != nil { if err != nil {
return err return err