From 062510cf1f7b79aed2efe88c5b9340d009bdec0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 22 Jul 2018 00:34:17 +0200 Subject: Get rid of the utils package --- commands/commandeer.go | 4 +--- commands/commands.go | 39 +++++++++++++++++++++++++++++++++++++++ commands/hugo.go | 5 ++--- 3 files changed, 42 insertions(+), 6 deletions(-) (limited to 'commands') diff --git a/commands/commandeer.go b/commands/commandeer.go index 051787f6e..3ff0b6426 100644 --- a/commands/commandeer.go +++ b/commands/commandeer.go @@ -24,8 +24,6 @@ import ( "github.com/spf13/cobra" - "github.com/gohugoio/hugo/utils" - "github.com/spf13/afero" "github.com/gohugoio/hugo/hugolib" @@ -308,7 +306,7 @@ func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error { cacheDir = cacheDir + helpers.FilePathSeparator } isDir, err := helpers.DirExists(cacheDir, sourceFs) - utils.CheckErr(cfg.Logger, err) + checkErr(cfg.Logger, err) if !isDir { mkdir(cacheDir) } diff --git a/commands/commands.go b/commands/commands.go index 74bc709cc..88939e600 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -14,9 +14,12 @@ package commands import ( + "os" + "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/helpers" "github.com/spf13/cobra" + jww "github.com/spf13/jwalterweatherman" "github.com/spf13/nitro" ) @@ -241,3 +244,39 @@ func (cc *hugoBuilderCommon) handleFlags(cmd *cobra.Command) { _ = cmd.Flags().SetAnnotation("destination", cobra.BashCompSubdirsInDir, []string{}) _ = cmd.Flags().SetAnnotation("theme", cobra.BashCompSubdirsInDir, []string{"themes"}) } + +func checkErr(logger *jww.Notepad, err error, s ...string) { + if err == nil { + return + } + if len(s) == 0 { + logger.CRITICAL.Println(err) + return + } + for _, message := range s { + logger.ERROR.Println(message) + } + logger.ERROR.Println(err) +} + +func stopOnErr(logger *jww.Notepad, err error, s ...string) { + if err == nil { + return + } + + defer os.Exit(-1) + + if len(s) == 0 { + newMessage := err.Error() + // Printing an empty string results in a error with + // no message, no bueno. + if newMessage != "" { + logger.CRITICAL.Println(newMessage) + } + } + for _, message := range s { + if message != "" { + logger.CRITICAL.Println(message) + } + } +} diff --git a/commands/hugo.go b/commands/hugo.go index 980189c47..312767499 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -43,7 +43,6 @@ import ( "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/hugolib" "github.com/gohugoio/hugo/livereload" - "github.com/gohugoio/hugo/utils" "github.com/gohugoio/hugo/watcher" "github.com/spf13/afero" "github.com/spf13/cobra" @@ -351,7 +350,7 @@ func (c *commandeer) build() error { c.Logger.FEEDBACK.Println("Watching for changes in", c.hugo.PathSpec.AbsPathify(c.Cfg.GetString("contentDir"))) c.Logger.FEEDBACK.Println("Press Ctrl+C to stop") watcher, err := c.newWatcher(watchDirs...) - utils.CheckErr(c.Logger, err) + checkErr(c.Logger, err) defer watcher.Close() var sigs = make(chan os.Signal) @@ -813,7 +812,7 @@ func (c *commandeer) newWatcher(dirList ...string) (*watcher.Batcher, error) { c.Logger.FEEDBACK.Printf("Syncing all static files\n") _, err := c.copyStatic() if err != nil { - utils.StopOnErr(c.Logger, err, "Error copying static files to publish dir") + stopOnErr(c.Logger, err, "Error copying static files to publish dir") } } else { if err := staticSyncer.syncsStaticEvents(staticEvents); err != nil { -- cgit v1.2.3