@@ -36,13 +36,15 @@ import (
3636)
3737
3838type Options struct {
39- Path string
40- Stdout io.Writer
41- Color bool
42- Verbose bool
43- Silent bool
44- MaxFileSize int64
45- Version string
39+ Path string
40+ Stdout io.Writer
41+ Color bool
42+ Verbose bool
43+ Silent bool
44+ MaxFileSize int64
45+ Excludes []string
46+ UseDefaultExcludes bool
47+ Version string
4648}
4749
4850type Result struct {
@@ -75,7 +77,28 @@ func Run(ctx context.Context, opts Options) (Result, error) {
7577 maxFileSize = filesystem .DefaultMaxFileSize
7678 }
7779
78- discovery , err := filesystem .Discover (path , maxFileSize )
80+ useDefaultExcludes := true
81+ if opts .Excludes != nil || ! opts .UseDefaultExcludes {
82+ useDefaultExcludes = opts .UseDefaultExcludes
83+ }
84+
85+ excluder , err := filesystem .NewExcluder (opts .Excludes , useDefaultExcludes )
86+ if err != nil {
87+ return Result {}, fmt .Errorf ("configure excludes: %w" , err )
88+ }
89+ headerWritten := false
90+ if opts .Verbose {
91+ if err := report .WriteHeader (opts .Stdout , opts .Version , opts .Silent ); err != nil {
92+ return Result {}, fmt .Errorf ("write report header: %w" , err )
93+ }
94+ headerWritten = true
95+ }
96+
97+ discovery , err := filesystem .Discover (path , filesystem.DiscoverOptions {
98+ MaxFileSize : maxFileSize ,
99+ Excluder : excluder ,
100+ OnExclude : buildExcludeReporter (opts .Stdout , opts .Verbose ),
101+ })
79102 if err != nil {
80103 return Result {}, fmt .Errorf ("discover files from %q: %w" , path , err )
81104 }
@@ -96,10 +119,11 @@ func Run(ctx context.Context, opts Options) (Result, error) {
96119 finding .Sort (findings )
97120
98121 if err := report .WriteHuman (opts .Stdout , findings , report.Options {
99- Version : opts .Version ,
100- Color : opts .Color ,
101- Verbose : opts .Verbose ,
102- Silent : opts .Silent ,
122+ Version : opts .Version ,
123+ Color : opts .Color ,
124+ Verbose : opts .Verbose ,
125+ Silent : opts .Silent ,
126+ HeaderWritten : headerWritten ,
103127 Runtime : report.RuntimeStats {
104128 WalkDuration : walkDuration ,
105129 ScanDuration : scanDuration ,
@@ -121,6 +145,16 @@ func Run(ctx context.Context, opts Options) (Result, error) {
121145 }, nil
122146}
123147
148+ func buildExcludeReporter (w io.Writer , verbose bool ) func (path , pattern string ) {
149+ if ! verbose {
150+ return nil
151+ }
152+
153+ return func (path , pattern string ) {
154+ _ , _ = fmt .Fprintf (w , "SKIP %s (matched exclude: %q)\n " , path , pattern )
155+ }
156+ }
157+
124158func scanCandidates (ctx context.Context , engine * scan.Engine , paths []string ) ([]fileScanResult , []error ) {
125159 if len (paths ) == 0 {
126160 return nil , nil
0 commit comments