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/server.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/server.go')
-rw-r--r-- | commands/server.go | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/commands/server.go b/commands/server.go index 8d69304c3..378637b6c 100644 --- a/commands/server.go +++ b/commands/server.go @@ -57,7 +57,7 @@ By default hugo will also watch your files for any changes you make and automatically rebuild the site. It will then live reload any open browser pages and push the latest content to them. As most Hugo sites are built in a fraction of a second, you will be able to save and see your changes nearly instantly.`, - //Run: server, + //RunE: server, } type filesOnlyFs struct { @@ -90,10 +90,10 @@ func init() { serverCmd.Flags().BoolVarP(&NoTimes, "noTimes", "", false, "Don't sync modification time of files") serverCmd.Flags().String("memstats", "", "log memory usage to this file") serverCmd.Flags().Int("meminterval", 100, "interval to poll memory usage (requires --memstats)") - serverCmd.Run = server + serverCmd.RunE = server } -func server(cmd *cobra.Command, args []string) { +func server(cmd *cobra.Command, args []string) error { InitializeConfig() if cmd.Flags().Lookup("disableLiveReload").Changed { @@ -116,8 +116,7 @@ func server(cmd *cobra.Command, args []string) { jww.ERROR.Println("port", serverPort, "already in use, attempting to use an available port") sp, err := helpers.FindAvailablePort() if err != nil { - jww.ERROR.Println("Unable to find alternative port to use") - jww.ERROR.Fatalln(err) + return newSystemError("Unable to find alternative port to use:", err) } serverPort = sp.Port } @@ -126,7 +125,7 @@ func server(cmd *cobra.Command, args []string) { BaseURL, err := fixURL(BaseURL) if err != nil { - jww.ERROR.Fatal(err) + return err } viper.Set("BaseURL", BaseURL) @@ -146,7 +145,9 @@ func server(cmd *cobra.Command, args []string) { viper.Set("PublishDir", "/") } - build(serverWatch) + if err := build(serverWatch); err != nil { + return err + } // Watch runs its own server as part of the routine if serverWatch { @@ -160,12 +161,15 @@ func server(cmd *cobra.Command, args []string) { jww.FEEDBACK.Printf("Watching for changes in %s/{%s}\n", baseWatchDir, rootWatchDirs) err := NewWatcher(serverPort) + if err != nil { - fmt.Println(err) + return err } } serve(serverPort) + + return nil } func serve(port int) { |