@@ -20,16 +20,16 @@ import (
2020 "path"
2121 "strings"
2222
23- "github.com/ossf/scorecard/v4/checker"
2423 "github.com/ossf/scorecard/v4/clients"
2524 sce "github.com/ossf/scorecard/v4/errors"
2625)
2726
2827// isMatchingPath uses 'pattern' to shell-match the 'path' and its filename
2928// 'caseSensitive' indicates the match should be case-sensitive. Default: no.
30- func isMatchingPath (pattern , fullpath string , caseSensitive bool ) (bool , error ) {
31- if ! caseSensitive {
32- pattern = strings .ToLower (pattern )
29+ func isMatchingPath (fullpath string , matchPathTo PathMatcher ) (bool , error ) {
30+ pattern := matchPathTo .Pattern
31+ if ! matchPathTo .CaseSensitive {
32+ pattern = strings .ToLower (matchPathTo .Pattern )
3333 fullpath = strings .ToLower (fullpath )
3434 }
3535
@@ -55,87 +55,30 @@ func isTestdataFile(fullpath string) bool {
5555 strings .Contains (fullpath , "/testdata/" )
5656}
5757
58- // FileCbData is any data the caller can act upon
59- // to keep state.
60- type FileCbData interface {}
61-
62- // FileContentCb is the callback.
63- // The bool returned indicates whether the CheckFilesContent2
64- // should continue iterating over files or not.
65- type FileContentCb func (path string , content []byte ,
66- dl checker.DetailLogger , data FileCbData ) (bool , error )
67-
68- // CheckFilesContent downloads the tar of the repository and calls the onFileContent() function
69- // shellPathFnPattern is used for https://golang.org/pkg/path/#Match
70- // Warning: the pattern is used to match (1) the entire path AND (2) the filename alone. This means:
71- // - To scope the search to a directory, use "./dirname/*". Example, for the root directory,
72- // use "./*".
73- // - A pattern such as "*mypatern*" will match files containing mypattern in *any* directory.
74- func CheckFilesContent (shellPathFnPattern string ,
75- caseSensitive bool ,
76- c * checker.CheckRequest ,
77- onFileContent FileContentCb ,
78- data FileCbData ,
79- ) error {
80- predicate := func (filepath string ) (bool , error ) {
81- // Filter out test files.
82- if isTestdataFile (filepath ) {
83- return false , nil
84- }
85- // Filter out files based on path/names using the pattern.
86- b , err := isMatchingPath (shellPathFnPattern , filepath , caseSensitive )
87- if err != nil {
88- return false , err
89- }
90- return b , nil
91- }
92-
93- matchedFiles , err := c .RepoClient .ListFiles (predicate )
94- if err != nil {
95- // nolint: wrapcheck
96- return err
97- }
98-
99- for _ , file := range matchedFiles {
100- content , err := c .RepoClient .GetFileContent (file )
101- if err != nil {
102- //nolint
103- return err
104- }
105-
106- continueIter , err := onFileContent (file , content , c .Dlogger , data )
107- if err != nil {
108- return err
109- }
110-
111- if ! continueIter {
112- break
113- }
114- }
115-
116- return nil
58+ // PathMatcher represents a query for a filepath.
59+ type PathMatcher struct {
60+ Pattern string
61+ CaseSensitive bool
11762}
11863
119- // FileContentCbV6 is the callback.
120- // The bool returned indicates whether the CheckFilesContent2
121- // should continue iterating over files or not.
122- type FileContentCbV6 func (path string , content []byte , data FileCbData ) (bool , error )
123-
124- // CheckFilesContentV6 is the same as CheckFilesContent
125- // but for use with separated check/policy code.
126- func CheckFilesContentV6 (shellPathFnPattern string ,
127- caseSensitive bool ,
128- repoClient clients.RepoClient ,
129- onFileContent FileContentCbV6 ,
130- data FileCbData ,
131- ) error {
64+ // DoWhileTrueOnFileContent takes a filepath, its content and
65+ // optional variadic args. It returns a boolean indicating whether
66+ // iterating over next files should continue.
67+ type DoWhileTrueOnFileContent func (path string , content []byte , args ... interface {}) (bool , error )
68+
69+ // OnMatchingFileContentDo matches all files listed by `repoClient` against `matchPathTo`
70+ // and on every successful match, runs onFileContent fn on the file's contents.
71+ // Continues iterating along the matched files until onFileContent returns
72+ // either a false value or an error.
73+ func OnMatchingFileContentDo (repoClient clients.RepoClient , matchPathTo PathMatcher ,
74+ onFileContent DoWhileTrueOnFileContent , args ... interface {}) error {
13275 predicate := func (filepath string ) (bool , error ) {
13376 // Filter out test files.
13477 if isTestdataFile (filepath ) {
13578 return false , nil
13679 }
13780 // Filter out files based on path/names using the pattern.
138- b , err := isMatchingPath (shellPathFnPattern , filepath , caseSensitive )
81+ b , err := isMatchingPath (filepath , matchPathTo )
13982 if err != nil {
14083 return false , err
14184 }
@@ -144,18 +87,16 @@ func CheckFilesContentV6(shellPathFnPattern string,
14487
14588 matchedFiles , err := repoClient .ListFiles (predicate )
14689 if err != nil {
147- // nolint: wrapcheck
148- return err
90+ return fmt .Errorf ("error during ListFiles: %w" , err )
14991 }
15092
15193 for _ , file := range matchedFiles {
15294 content , err := repoClient .GetFileContent (file )
15395 if err != nil {
154- //nolint
155- return err
96+ return fmt .Errorf ("error during GetFileContent: %w" , err )
15697 }
15798
158- continueIter , err := onFileContent (file , content , data )
99+ continueIter , err := onFileContent (file , content , args ... )
159100 if err != nil {
160101 return err
161102 }
0 commit comments