From 7b62ee10c2a81e03aeff5ceef8e26f1a9aacd300 Mon Sep 17 00:00:00 2001 From: umarcor Date: Fri, 7 Jun 2019 19:17:26 +0200 Subject: [PATCH] add helpers: 'er', 'Er' and 'WrStringAndCheck', use 'io.StringWriter' --- cobra.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/cobra.go b/cobra.go index d01becc8..00183ad7 100644 --- a/cobra.go +++ b/cobra.go @@ -19,6 +19,7 @@ package cobra import ( "fmt" "io" + "os" "reflect" "strconv" "strings" @@ -205,3 +206,18 @@ func stringInSlice(a string, list []string) bool { } return false } + +func er(msg interface{}) { + if msg != nil { + fmt.Println("Error:", msg) + os.Exit(1) + } +} + +// Er prints the msg and exits with error code 1, unless the msg is nil +func Er(msg interface{}) { er(msg) } + +func WrStringAndCheck(b io.StringWriter, s string) { + _, err := b.WriteString(s) + er(err) +}