diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2015-12-02 11:42:53 +0100 |
---|---|---|
committer | Anthony Fok <foka@debian.org> | 2015-12-02 07:07:05 -0700 |
commit | 3f0f7eed68f44486c1e053bbce25c46c1d52a12f (patch) | |
tree | 4bafd37bcd2ede6fb1c7f5838679a345dae83668 /commands/import_jekyll.go | |
parent | 6959b7fa80f22aead6fa8c9b8ff3c4b8cc222a30 (diff) |
Improve error handling in commands
Cobra, the CLI commander in use in Hugo, has some long awaited improvements in the error handling department.
This enables a more centralized error handling approach.
This commit introduces that by changing all the command funcs to `RunE`:
* The core part of the error logging, usage logging and `os.Exit(-1)` is now performed in one place and that one place only.
* The usage text is now only shown on invalid arguments etc. (user errors)
Fixes #1502
Diffstat (limited to 'commands/import_jekyll.go')
-rw-r--r-- | commands/import_jekyll.go | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/commands/import_jekyll.go b/commands/import_jekyll.go index 38d352671..00893f854 100644 --- a/commands/import_jekyll.go +++ b/commands/import_jekyll.go @@ -35,34 +35,44 @@ import ( jww "github.com/spf13/jwalterweatherman" ) +func init() { + importCmd.AddCommand(importJekyllCmd) +} + +var importCmd = &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, +} + var 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`.", - Run: importFromJekyll, + RunE: importFromJekyll, } -func importFromJekyll(cmd *cobra.Command, args []string) { +func importFromJekyll(cmd *cobra.Command, args []string) error { jww.SetLogThreshold(jww.LevelTrace) jww.SetStdoutThreshold(jww.LevelWarn) if len(args) < 2 { - jww.ERROR.Println(`Import from Jekyll requires two paths, e.g. ` + "`hugo import jekyll jekyll_root_path target_path`.") - return + return newUserError(`Import from Jekyll requires two paths, e.g. ` + "`hugo import jekyll jekyll_root_path target_path`.") } jekyllRoot, err := filepath.Abs(filepath.Clean(args[0])) if err != nil { - jww.ERROR.Println("Path error:", args[0]) - return + return newUserError("Path error:", args[0]) } targetDir, err := filepath.Abs(filepath.Clean(args[1])) if err != nil { - jww.ERROR.Println("Path error:", args[1]) - return + return newUserError("Path error:", args[1]) } createSiteFromJekyll(jekyllRoot, targetDir) @@ -82,8 +92,7 @@ func importFromJekyll(cmd *cobra.Command, args []string) { relPath, err := filepath.Rel(jekyllRoot, path) if err != nil { - jww.ERROR.Println("Get rel path error:", path) - return err + return newUserError("Get rel path error:", path) } relPath = filepath.ToSlash(relPath) @@ -106,13 +115,15 @@ func importFromJekyll(cmd *cobra.Command, args []string) { err = filepath.Walk(jekyllRoot, callback) if err != nil { - fmt.Println(err) + return err } else { fmt.Println("Congratulations!", fileCount, "posts 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") } + + return nil } func createSiteFromJekyll(jekyllRoot, targetDir string) { |