summaryrefslogtreecommitdiffstats
path: root/commands/benchmark.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/benchmark.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/benchmark.go')
-rw-r--r--commands/benchmark.go17
1 files changed, 11 insertions, 6 deletions
diff --git a/commands/benchmark.go b/commands/benchmark.go
index a55ed7535..530bf3906 100644
--- a/commands/benchmark.go
+++ b/commands/benchmark.go
@@ -28,9 +28,12 @@ var benchmark = &cobra.Command{
Short: "Benchmark hugo by building a site a number of times.",
Long: `Hugo can build a site many times over and analyze the running process
creating a benchmark.`,
- Run: func(cmd *cobra.Command, args []string) {
- InitializeConfig()
- bench(cmd, args)
+ RunE: func(cmd *cobra.Command, args []string) error {
+ if err := InitializeConfig(); err != nil {
+ return err
+ }
+
+ return bench(cmd, args)
},
}
@@ -41,13 +44,13 @@ func init() {
benchmark.Flags().IntVarP(&benchmarkTimes, "count", "n", 13, "number of times to build the site")
}
-func bench(cmd *cobra.Command, args []string) {
+func bench(cmd *cobra.Command, args []string) error {
if memProfilefile != "" {
f, err := os.Create(memProfilefile)
if err != nil {
- panic(err)
+ return err
}
for i := 0; i < benchmarkTimes; i++ {
_ = buildSite()
@@ -62,7 +65,7 @@ func bench(cmd *cobra.Command, args []string) {
f, err := os.Create(cpuProfilefile)
if err != nil {
- panic(err)
+ return err
}
pprof.StartCPUProfile(f)
@@ -72,4 +75,6 @@ func bench(cmd *cobra.Command, args []string) {
}
}
+ return nil
+
}