11package main
22
33import (
4+ "bufio"
45 "fmt"
6+ "io"
57 "os"
68 "path/filepath"
79 "strings"
810
911 "github.com/hmarr/codeowners"
10- "github.com/karrick/godirwalk"
1112 flag "github.com/spf13/pflag"
1213)
1314
@@ -50,29 +51,29 @@ func main() {
5051 ownerFilters [i ] = strings .TrimLeft (ownerFilters [i ], "@" )
5152 }
5253
54+ out := bufio .NewWriter (os .Stdout )
55+ defer out .Flush ()
56+
5357 for _ , startPath := range paths {
5458 // godirwalk only accepts directories, so we need to handle files separately
5559 if ! isDir (startPath ) {
56- if err := printFileOwners (ruleset , startPath , ownerFilters , showUnowned ); err != nil {
60+ if err := printFileOwners (out , ruleset , startPath , ownerFilters , showUnowned ); err != nil {
5761 fmt .Fprintf (os .Stderr , "error: %v" , err )
5862 os .Exit (1 )
5963 }
6064 continue
6165 }
6266
63- err = godirwalk .Walk (startPath , & godirwalk.Options {
64- Callback : func (path string , dirent * godirwalk.Dirent ) error {
65- if path == ".git" {
66- return filepath .SkipDir
67- }
68-
69- // Only show code owners for files, not directories
70- if ! dirent .IsDir () {
71- return printFileOwners (ruleset , path , ownerFilters , showUnowned )
72- }
73- return nil
74- },
75- Unsorted : true ,
67+ err = filepath .WalkDir (startPath , func (path string , d os.DirEntry , err error ) error {
68+ if path == ".git" {
69+ return filepath .SkipDir
70+ }
71+
72+ // Only show code owners for files, not directories
73+ if ! d .IsDir () {
74+ return printFileOwners (out , ruleset , path , ownerFilters , showUnowned )
75+ }
76+ return nil
7677 })
7778
7879 if err != nil {
@@ -82,7 +83,7 @@ func main() {
8283 }
8384}
8485
85- 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 {
8687 rule , err := ruleset .Match (path )
8788 if err != nil {
8889 return err
@@ -91,13 +92,13 @@ func printFileOwners(ruleset codeowners.Ruleset, path string, ownerFilters []str
9192 if rule == nil || rule .Owners == nil {
9293 // Unless explicitly requested, don't show unowned files if we're filtering by owner
9394 if len (ownerFilters ) == 0 || showUnowned {
94- fmt .Printf ( "%-70s (unowned)\n " , path )
95+ fmt .Fprintf ( out , "%-70s (unowned)\n " , path )
9596 }
9697 return nil
9798 }
9899
99100 // Figure out which of the owners we need to show according to the --owner filters
100- owners := []string {}
101+ ownersToShow := make ( []string , 0 , len ( rule . Owners ))
101102 for _ , o := range rule .Owners {
102103 // If there are no filters, show all owners
103104 filterMatch := len (ownerFilters ) == 0 && ! showUnowned
@@ -107,13 +108,13 @@ func printFileOwners(ruleset codeowners.Ruleset, path string, ownerFilters []str
107108 }
108109 }
109110 if filterMatch {
110- owners = append (owners , o .String ())
111+ ownersToShow = append (ownersToShow , o .String ())
111112 }
112113 }
113114
114115 // If the owners slice is empty, no owners matched the filters so don't show anything
115- if len (owners ) > 0 {
116- fmt .Printf ( "%-70s %s\n " , path , strings .Join (owners , " " ))
116+ if len (ownersToShow ) > 0 {
117+ fmt .Fprintf ( out , "%-70s %s\n " , path , strings .Join (ownersToShow , " " ))
117118 }
118119 return nil
119120}
0 commit comments