From cbb95f301a9442e892f3592edb8c12d2fdb2d22f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Stemmer?= Date: Fri, 11 Mar 2022 22:46:18 +0000 Subject: [PATCH] Create exitf function to print message and exit with non-zero exit code --- go-junit-report.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/go-junit-report.go b/go-junit-report.go index 2ce2921..08caaca 100644 --- a/go-junit-report.go +++ b/go-junit-report.go @@ -39,14 +39,13 @@ func main() { if flag.NArg() != 0 { fmt.Fprintf(os.Stderr, "%s does not accept positional arguments\n", os.Args[0]) flag.Usage() - os.Exit(1) + exitf("") } // Read input events, err := gotest.Parse(os.Stdin) if err != nil { - fmt.Fprintf(os.Stderr, "Error reading input: %s\n", err) - os.Exit(1) + exitf("error reading input: %s\n", err) } if *printEvents { @@ -66,12 +65,10 @@ func main() { enc := xml.NewEncoder(os.Stdout) enc.Indent("", "\t") if err := enc.Encode(testsuites); err != nil { - fmt.Fprintf(os.Stderr, "Error writing XML: %s\n", err) - os.Exit(1) + exitf("error writing XML: %v", err) } if err := enc.Flush(); err != nil { - fmt.Fprintf(os.Stderr, "Error flusing XML: %s\n", err) - os.Exit(1) + exitf("error flusing XML: %v", err) } fmt.Fprintf(os.Stdout, "\n") @@ -79,3 +76,10 @@ func main() { os.Exit(1) } } + +func exitf(msg string, args ...interface{}) { + if msg != "" { + fmt.Fprintf(os.Stderr, msg+"\n", args...) + } + os.Exit(2) +}