Create exitf function to print message and exit with non-zero exit code

This commit is contained in:
Joël Stemmer 2022-03-11 22:46:18 +00:00
parent c8de8e11aa
commit cbb95f301a

View File

@ -39,14 +39,13 @@ func main() {
if flag.NArg() != 0 { if flag.NArg() != 0 {
fmt.Fprintf(os.Stderr, "%s does not accept positional arguments\n", os.Args[0]) fmt.Fprintf(os.Stderr, "%s does not accept positional arguments\n", os.Args[0])
flag.Usage() flag.Usage()
os.Exit(1) exitf("")
} }
// Read input // Read input
events, err := gotest.Parse(os.Stdin) events, err := gotest.Parse(os.Stdin)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error reading input: %s\n", err) exitf("error reading input: %s\n", err)
os.Exit(1)
} }
if *printEvents { if *printEvents {
@ -66,12 +65,10 @@ func main() {
enc := xml.NewEncoder(os.Stdout) enc := xml.NewEncoder(os.Stdout)
enc.Indent("", "\t") enc.Indent("", "\t")
if err := enc.Encode(testsuites); err != nil { if err := enc.Encode(testsuites); err != nil {
fmt.Fprintf(os.Stderr, "Error writing XML: %s\n", err) exitf("error writing XML: %v", err)
os.Exit(1)
} }
if err := enc.Flush(); err != nil { if err := enc.Flush(); err != nil {
fmt.Fprintf(os.Stderr, "Error flusing XML: %s\n", err) exitf("error flusing XML: %v", err)
os.Exit(1)
} }
fmt.Fprintf(os.Stdout, "\n") fmt.Fprintf(os.Stdout, "\n")
@ -79,3 +76,10 @@ func main() {
os.Exit(1) os.Exit(1)
} }
} }
func exitf(msg string, args ...interface{}) {
if msg != "" {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
}
os.Exit(2)
}