Skip to content

Commit 90a39a1

Browse files
committed
Fix issue where failure message appears before log messages
1 parent 1a64919 commit 90a39a1

File tree

3 files changed

+16
-25
lines changed

3 files changed

+16
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Code Ownership & Review Assignment Tool - GitHub CODEOWNERS but better
44

55
[![Go Report Card](https://goreportcard.com/badge/github.com/multimediallc/codeowners-plus)](https://goreportcard.com/report/github.com/multimediallc/codeowners-plus?kill_cache=1)
66
[![Tests](https://github.com/multimediallc/codeowners-plus/actions/workflows/go.yml/badge.svg)](https://github.com/multimediallc/codeowners-plus/actions/workflows/go.yml)
7-
![Coverage](https://img.shields.io/badge/Coverage-83.1%25-brightgreen)
7+
![Coverage](https://img.shields.io/badge/Coverage-83.6%25-brightgreen)
88
[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
99
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](CODE_OF_CONDUCT.md)
1010

main.go

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"flag"
66
"fmt"
7+
"io"
78
"os"
89
"slices"
910
"strconv"
@@ -86,7 +87,7 @@ func initFlags(flags *Flags) error {
8687
func NewApp(cfg AppConfig) (*App, error) {
8788
repoSplit := strings.Split(cfg.Repo, "/")
8889
if len(repoSplit) != 2 {
89-
return nil, fmt.Errorf("invalid repo name: %s", cfg.Repo)
90+
return nil, fmt.Errorf("Invalid repo name: %s", cfg.Repo)
9091
}
9192
owner := repoSplit[0]
9293
repo := repoSplit[1]
@@ -379,19 +380,19 @@ func ignoreError[V any, E error](res V, _ E) V {
379380
return res
380381
}
381382

382-
func errorAndExit(shouldFail bool, format string, args ...interface{}) {
383-
_, err := WarningBuffer.WriteTo(os.Stderr)
383+
func outputAndExit(w io.Writer, shouldFail bool, message string) {
384+
_, err := WarningBuffer.WriteTo(w)
384385
if err != nil {
385386
fmt.Fprintf(os.Stderr, "Error writing warning buffer: %v\n", err)
386387
}
387388
if *flags.Verbose {
388-
_, err := InfoBuffer.WriteTo(os.Stderr)
389+
_, err := InfoBuffer.WriteTo(w)
389390
if err != nil {
390391
fmt.Fprintf(os.Stderr, "Error writing info buffer: %v\n", err)
391392
}
392393
}
393394

394-
fmt.Fprintf(os.Stderr, format, args...)
395+
fmt.Fprint(w, message)
395396
if testing.Testing() {
396397
return
397398
}
@@ -415,7 +416,7 @@ func printWarning(format string, args ...interface{}) {
415416
func main() {
416417
err := initFlags(flags)
417418
if err != nil {
418-
errorAndExit(true, "%v\n", err)
419+
outputAndExit(os.Stderr, true, fmt.Sprintln(err))
419420
}
420421

421422
cfg := AppConfig{
@@ -428,29 +429,19 @@ func main() {
428429

429430
app, err := NewApp(cfg)
430431
if err != nil {
431-
errorAndExit(true, "Failed to initialize app: %v\n", err)
432+
outputAndExit(os.Stderr, true, fmt.Sprintf("Failed to initialize app: %v\n", err))
432433
}
433434

434435
success, message, err := app.Run()
435436
if err != nil {
436-
errorAndExit(true, "%v\n", err)
437-
}
438-
_, err = WarningBuffer.WriteTo(os.Stderr)
439-
if err != nil {
440-
fmt.Fprintf(os.Stderr, "Error writing warning buffer: %v\n", err)
441-
}
442-
if *flags.Verbose {
443-
_, err = InfoBuffer.WriteTo(os.Stdout)
444-
if err != nil {
445-
fmt.Fprintf(os.Stderr, "Error writing info buffer: %v\n", err)
446-
}
437+
outputAndExit(os.Stderr, true, fmt.Sprintln(err))
447438
}
439+
var w io.Writer
448440
if success {
449-
fmt.Fprintln(os.Stdout, message)
441+
w = os.Stdout
450442
} else {
451-
fmt.Fprintln(os.Stderr, message)
452-
}
453-
if !success && app.conf.Enforcement.FailCheck {
454-
os.Exit(1)
443+
w = os.Stderr
455444
}
445+
shouldFail := !success && app.conf.Enforcement.FailCheck
446+
outputAndExit(w, shouldFail, message)
456447
}

main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ func TestErrorAndExit(t *testing.T) {
556556
InfoBuffer.WriteString(tc.info)
557557
*flags.Verbose = tc.verbose
558558

559-
errorAndExit(tc.shouldFail, tc.format, tc.args...)
559+
outputAndExit(io.Discard, tc.shouldFail, fmt.Sprintf(tc.format, tc.args...))
560560
})
561561
}
562562
}

0 commit comments

Comments
 (0)