1
1
package main
2
2
3
3
import (
4
+ "bufio"
4
5
"fmt"
6
+ "io"
5
7
"os"
6
8
"path/filepath"
7
9
"strings"
8
10
9
11
"github.com/hmarr/codeowners"
10
- "github.com/karrick/godirwalk"
11
12
flag "github.com/spf13/pflag"
12
13
)
13
14
@@ -50,29 +51,29 @@ func main() {
50
51
ownerFilters [i ] = strings .TrimLeft (ownerFilters [i ], "@" )
51
52
}
52
53
54
+ out := bufio .NewWriter (os .Stdout )
55
+ defer out .Flush ()
56
+
53
57
for _ , startPath := range paths {
54
58
// godirwalk only accepts directories, so we need to handle files separately
55
59
if ! isDir (startPath ) {
56
- if err := printFileOwners (ruleset , startPath , ownerFilters , showUnowned ); err != nil {
60
+ if err := printFileOwners (out , ruleset , startPath , ownerFilters , showUnowned ); err != nil {
57
61
fmt .Fprintf (os .Stderr , "error: %v" , err )
58
62
os .Exit (1 )
59
63
}
60
64
continue
61
65
}
62
66
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
76
77
})
77
78
78
79
if err != nil {
@@ -82,7 +83,7 @@ func main() {
82
83
}
83
84
}
84
85
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 {
86
87
rule , err := ruleset .Match (path )
87
88
if err != nil {
88
89
return err
@@ -91,13 +92,13 @@ func printFileOwners(ruleset codeowners.Ruleset, path string, ownerFilters []str
91
92
if rule == nil || rule .Owners == nil {
92
93
// Unless explicitly requested, don't show unowned files if we're filtering by owner
93
94
if len (ownerFilters ) == 0 || showUnowned {
94
- fmt .Printf ( "%-70s (unowned)\n " , path )
95
+ fmt .Fprintf ( out , "%-70s (unowned)\n " , path )
95
96
}
96
97
return nil
97
98
}
98
99
99
100
// 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 ))
101
102
for _ , o := range rule .Owners {
102
103
// If there are no filters, show all owners
103
104
filterMatch := len (ownerFilters ) == 0 && ! showUnowned
@@ -107,13 +108,13 @@ func printFileOwners(ruleset codeowners.Ruleset, path string, ownerFilters []str
107
108
}
108
109
}
109
110
if filterMatch {
110
- owners = append (owners , o .String ())
111
+ ownersToShow = append (ownersToShow , o .String ())
111
112
}
112
113
}
113
114
114
115
// 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 , " " ))
117
118
}
118
119
return nil
119
120
}
0 commit comments