From 8e505e28081df56c3a660a1fd1fab01e65f86aea Mon Sep 17 00:00:00 2001 From: Sven Walter Date: Mon, 16 Apr 2018 12:07:11 +0200 Subject: [PATCH] add cmdutil.Must --- cmdutil/exit.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/cmdutil/exit.go b/cmdutil/exit.go index c5aa5f6..6dc61c4 100644 --- a/cmdutil/exit.go +++ b/cmdutil/exit.go @@ -1,6 +1,14 @@ package cmdutil -import "os" +import ( + "os" + + log "github.com/sirupsen/logrus" +) + +const ( + mustExitCode = 1 +) type exitCode struct { code int @@ -27,3 +35,17 @@ func HandleExit() { panic(e) // not an Exit, bubble up } } + +// Must exits the application via Exit(1) and logs the error, if err does not +// equal nil. Additionally it logs the error with `%+v` to the debug log, so it +// can used together with github.com/pkg/errors to retrive more details about +// the error. +func Must(err error) { + if err == nil { + return + } + + log.Debugf("%+v", err) + log.Error(err) + Exit(mustExitCode) +}