summaryrefslogtreecommitdiffstats
path: root/commands/convert.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2015-12-02 11:42:53 +0100
committerAnthony Fok <foka@debian.org>2015-12-02 07:07:05 -0700
commit3f0f7eed68f44486c1e053bbce25c46c1d52a12f (patch)
tree4bafd37bcd2ede6fb1c7f5838679a345dae83668 /commands/convert.go
parent6959b7fa80f22aead6fa8c9b8ff3c4b8cc222a30 (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/convert.go')
-rw-r--r--commands/convert.go27
1 files changed, 10 insertions, 17 deletions
diff --git a/commands/convert.go b/commands/convert.go
index 9f7076d7b..3e66ba2bf 100644
--- a/commands/convert.go
+++ b/commands/convert.go
@@ -36,7 +36,7 @@ var convertCmd = &cobra.Command{
Long: `Convert your content (e.g. front matter) to different formats.
See convert's subcommands toJSON, toTOML and toYAML for more information.`,
- Run: nil,
+ RunE: nil,
}
var toJSONCmd = &cobra.Command{
@@ -44,11 +44,8 @@ var toJSONCmd = &cobra.Command{
Short: "Convert front matter to JSON",
Long: `toJSON converts all front matter in the content directory
to use JSON for the front matter.`,
- Run: func(cmd *cobra.Command, args []string) {
- err := convertContents(rune([]byte(parser.JSON_LEAD)[0]))
- if err != nil {
- jww.ERROR.Println(err)
- }
+ RunE: func(cmd *cobra.Command, args []string) error {
+ return convertContents(rune([]byte(parser.JSON_LEAD)[0]))
},
}
@@ -57,11 +54,8 @@ var toTOMLCmd = &cobra.Command{
Short: "Convert front matter to TOML",
Long: `toTOML converts all front matter in the content directory
to use TOML for the front matter.`,
- Run: func(cmd *cobra.Command, args []string) {
- err := convertContents(rune([]byte(parser.TOML_LEAD)[0]))
- if err != nil {
- jww.ERROR.Println(err)
- }
+ RunE: func(cmd *cobra.Command, args []string) error {
+ return convertContents(rune([]byte(parser.TOML_LEAD)[0]))
},
}
@@ -70,11 +64,8 @@ var toYAMLCmd = &cobra.Command{
Short: "Convert front matter to YAML",
Long: `toYAML converts all front matter in the content directory
to use YAML for the front matter.`,
- Run: func(cmd *cobra.Command, args []string) {
- err := convertContents(rune([]byte(parser.YAML_LEAD)[0]))
- if err != nil {
- jww.ERROR.Println(err)
- }
+ RunE: func(cmd *cobra.Command, args []string) error {
+ return convertContents(rune([]byte(parser.YAML_LEAD)[0]))
},
}
@@ -87,7 +78,9 @@ func init() {
}
func convertContents(mark rune) (err error) {
- InitializeConfig()
+ if err := InitializeConfig(); err != nil {
+ return err
+ }
site := &hugolib.Site{}
if err := site.Initialise(); err != nil {