Skip to content

Commit 5675ede

Browse files
committed
Buffer writes to stdout
1 parent 85cd95a commit 5675ede

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

cmd/codeowners/main.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

33
import (
4+
"bufio"
45
"fmt"
6+
"io"
57
"os"
68
"path/filepath"
79
"strings"
@@ -49,10 +51,13 @@ func main() {
4951
ownerFilters[i] = strings.TrimLeft(ownerFilters[i], "@")
5052
}
5153

54+
out := bufio.NewWriter(os.Stdout)
55+
defer out.Flush()
56+
5257
for _, startPath := range paths {
5358
// godirwalk only accepts directories, so we need to handle files separately
5459
if !isDir(startPath) {
55-
if err := printFileOwners(ruleset, startPath, ownerFilters, showUnowned); err != nil {
60+
if err := printFileOwners(out, ruleset, startPath, ownerFilters, showUnowned); err != nil {
5661
fmt.Fprintf(os.Stderr, "error: %v", err)
5762
os.Exit(1)
5863
}
@@ -66,7 +71,7 @@ func main() {
6671

6772
// Only show code owners for files, not directories
6873
if !d.IsDir() {
69-
return printFileOwners(ruleset, path, ownerFilters, showUnowned)
74+
return printFileOwners(out, ruleset, path, ownerFilters, showUnowned)
7075
}
7176
return nil
7277
})
@@ -78,7 +83,7 @@ func main() {
7883
}
7984
}
8085

81-
func printFileOwners(ruleset codeowners.Ruleset, path string, ownerFilters []string, showUnowned bool) error {
86+
func printFileOwners(out io.Writer, ruleset codeowners.Ruleset, path string, ownerFilters []string, showUnowned bool) error {
8287
rule, err := ruleset.Match(path)
8388
if err != nil {
8489
return err
@@ -87,7 +92,7 @@ func printFileOwners(ruleset codeowners.Ruleset, path string, ownerFilters []str
8792
if rule == nil || rule.Owners == nil {
8893
// Unless explicitly requested, don't show unowned files if we're filtering by owner
8994
if len(ownerFilters) == 0 || showUnowned {
90-
fmt.Printf("%-70s (unowned)\n", path)
95+
fmt.Fprintf(out, "%-70s (unowned)\n", path)
9196
}
9297
return nil
9398
}
@@ -109,7 +114,7 @@ func printFileOwners(ruleset codeowners.Ruleset, path string, ownerFilters []str
109114

110115
// If the owners slice is empty, no owners matched the filters so don't show anything
111116
if len(owners) > 0 {
112-
fmt.Printf("%-70s %s\n", path, strings.Join(owners, " "))
117+
fmt.Fprintf(out, "%-70s %s\n", path, strings.Join(ownersToShow, " "))
113118
}
114119
return nil
115120
}

0 commit comments

Comments
 (0)