2013-10-09 18:52:29 -04:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2014-08-23 17:12:36 -04:00
|
|
|
"strings"
|
2014-03-31 13:23:34 -04:00
|
|
|
|
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
2013-10-09 18:52:29 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func CheckErr(err error, s ...string) {
|
|
|
|
if err != nil {
|
2014-06-30 00:32:53 -04:00
|
|
|
if len(s) == 0 {
|
|
|
|
jww.CRITICAL.Println(err)
|
|
|
|
} else {
|
|
|
|
for _, message := range s {
|
|
|
|
jww.ERROR.Println(message)
|
|
|
|
}
|
2014-09-05 21:38:36 -04:00
|
|
|
jww.ERROR.Println(err)
|
2013-10-09 18:52:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-09 19:06:47 -04:00
|
|
|
func StopOnErr(err error, s ...string) {
|
2013-10-09 18:52:29 -04:00
|
|
|
if err != nil {
|
2015-02-17 05:35:23 -05:00
|
|
|
if len(s) == 0 {
|
|
|
|
newMessage := cutUsageMessage(err.Error())
|
2014-08-23 17:12:36 -04:00
|
|
|
|
2015-02-17 05:35:23 -05:00
|
|
|
// Printing an empty string results in a error with
|
|
|
|
// no message, no bueno.
|
|
|
|
if newMessage != "" {
|
|
|
|
jww.CRITICAL.Println(newMessage)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, message := range s {
|
|
|
|
message := cutUsageMessage(message)
|
2014-08-23 17:12:36 -04:00
|
|
|
|
2015-02-17 05:35:23 -05:00
|
|
|
if message != "" {
|
|
|
|
jww.CRITICAL.Println(message)
|
|
|
|
}
|
2014-06-30 00:32:53 -04:00
|
|
|
}
|
2014-03-31 13:23:34 -04:00
|
|
|
}
|
2015-02-17 05:35:23 -05:00
|
|
|
os.Exit(-1)
|
2013-10-09 18:52:29 -04:00
|
|
|
}
|
|
|
|
}
|
2014-08-23 17:12:36 -04:00
|
|
|
|
|
|
|
// cutUsageMessage splits the incoming string on the beginning of the usage
|
|
|
|
// message text. Anything in the first element of the returned slice, trimmed
|
2015-03-05 17:19:10 -05:00
|
|
|
// of its Unicode defined spaces, should be returned. The 2nd element of the
|
2014-08-23 17:12:36 -04:00
|
|
|
// slice will have the usage message that we wish to elide.
|
|
|
|
//
|
|
|
|
// This is done because Cobra already prints Hugo's usage message; not eliding
|
2015-03-05 17:19:10 -05:00
|
|
|
// would result in the usage output being printed twice, which leads to bug
|
2014-08-23 17:12:36 -04:00
|
|
|
// reports, more specifically: https://github.com/spf13/hugo/issues/374
|
|
|
|
func cutUsageMessage(s string) string {
|
2015-03-05 17:19:10 -05:00
|
|
|
pieces := strings.Split(s, "Usage of")
|
2014-08-23 17:12:36 -04:00
|
|
|
return strings.TrimSpace(pieces[0])
|
|
|
|
}
|