mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-27 22:21:07 +03:00
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
This commit is contained in:
parent
6959b7fa80
commit
3f0f7eed68
17 changed files with 219 additions and 155 deletions
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue