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/new.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/new.go')
-rw-r--r-- | commands/new.go | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/commands/new.go b/commands/new.go index f2c362ac3..579d12a52 100644 --- a/commands/new.go +++ b/commands/new.go @@ -55,7 +55,7 @@ You can also specify the kind with ` + "`-k KIND`" + `. If archetypes are provided in your theme or site, they will be used.`, - Run: NewContent, + RunE: NewContent, } var newSiteCmd = &cobra.Command{ @@ -64,7 +64,7 @@ var newSiteCmd = &cobra.Command{ Long: `Create a new site in the provided directory. The new site will have the correct structure, but no content or theme yet. Use ` + "`hugo new [contentPath]`" + ` to create new content.`, - Run: NewSite, + RunE: NewSite, } var newThemeCmd = &cobra.Command{ @@ -74,20 +74,21 @@ var newThemeCmd = &cobra.Command{ New theme is a skeleton. Please add content to the touched files. Add your name to the copyright line in the license and adjust the theme.toml file as you see fit.`, - Run: NewTheme, + RunE: NewTheme, } // NewContent adds new content to a Hugo site. -func NewContent(cmd *cobra.Command, args []string) { - InitializeConfig() +func NewContent(cmd *cobra.Command, args []string) error { + if err := InitializeConfig(); err != nil { + return err + } if cmd.Flags().Lookup("format").Changed { viper.Set("MetaDataFormat", configFormat) } if len(args) < 1 { - cmd.Usage() - jww.FATAL.Fatalln("path needs to be provided") + return newUserError("path needs to be provided") } createpath := args[0] @@ -100,10 +101,8 @@ func NewContent(cmd *cobra.Command, args []string) { kind = contentType } - err := create.NewContent(kind, createpath) - if err != nil { - jww.ERROR.Println(err) - } + return create.NewContent(kind, createpath) + } func doNewSite(basepath string, force bool) error { @@ -146,32 +145,31 @@ func doNewSite(basepath string, force bool) error { } // NewSite creates a new hugo site and initializes a structured Hugo directory. -func NewSite(cmd *cobra.Command, args []string) { +func NewSite(cmd *cobra.Command, args []string) error { if len(args) < 1 { - cmd.Usage() - jww.FATAL.Fatalln("path needs to be provided") + return newUserError("path needs to be provided") } createpath, err := filepath.Abs(filepath.Clean(args[0])) if err != nil { - cmd.Usage() - jww.FATAL.Fatalln(err) + return newUserError(err) } forceNew, _ := cmd.Flags().GetBool("force") - if err := doNewSite(createpath, forceNew); err != nil { - cmd.Usage() - jww.FATAL.Fatalln(err) - } + + return doNewSite(createpath, forceNew) + } // NewTheme creates a new Hugo theme. -func NewTheme(cmd *cobra.Command, args []string) { - InitializeConfig() +func NewTheme(cmd *cobra.Command, args []string) error { + if err := InitializeConfig(); err != nil { + return err + } if len(args) < 1 { - cmd.Usage() - jww.FATAL.Fatalln("theme name needs to be provided") + + return newUserError("theme name needs to be provided") } createpath := helpers.AbsPathify(filepath.Join("themes", args[0])) @@ -229,10 +227,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. err = helpers.WriteToDisk(filepath.Join(createpath, "LICENSE.md"), bytes.NewReader(by), hugofs.SourceFs) if err != nil { - jww.FATAL.Fatalln(err) + return nil } createThemeMD(createpath) + + return nil } func mkdir(x ...string) { |