Skip to content

Commit e645ecf

Browse files
authored
Exclusion filters (#22)
* inital implementation - README and e2e scripts need updating still * update e2e script and update CI * gitlab-ci yml format * format e2e script * add example to README
1 parent 0e6f8c3 commit e645ecf

6 files changed

Lines changed: 164 additions & 9 deletions

File tree

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ jobs:
1818
- name: Build Bins
1919
run: ./scripts/bins.sh
2020
- name: E2E
21-
run: ./scripts/e2e.sh
21+
run: ETE_DIR=$HOME ./scripts/e2e.sh
2222
- name: Print SHA
2323
run: cat scnnr_bins.zip.sha256

.gitlab-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ release:
1616
- go run main.go -m fsf -s 1MB -d $HOME
1717
- go run main.go -m fnf -f main,DEFCON -p $HOME
1818
- go run main.go -k main,const,let,var,for -p $HOME
19+
- ETE_DIR=$HOME ./scripts/e2e.sh
1920
- go run cmd/pack/main.go
2021
- go run main.go -m fff -k $(go run cmd/checksum/main.go) -p .
2122
- cat scnnr_bins.zip.sha256
@@ -26,4 +27,3 @@ release:
2627
- scnnr_bins.zip.sha256
2728
- LICENSE
2829
- README.md
29-

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ $ scnnr -h
9696
-s string
9797
REQUIRED SizeFinder MODE
9898
size: 1MB,10MB,100MB,1GB,10GB,100GB,1TB
99+
-xd string
100+
OPTIONAL Scnnr MODE
101+
comma-delimited list of directory names to exclude from scanning
102+
ex: -xd ".git,node_modules,.venv"
103+
-xe string
104+
OPTIONAL Scnnr MODE
105+
comma-delimited list of file extensions to exclude from scanning
106+
ex: -xe ".log,.tmp,.json"
99107
```
100108

101109
### No Keywords
@@ -187,6 +195,37 @@ README.md:383:36:cache
187195
README.md:393:40:cache
188196
```
189197

198+
### Keywords while excluding dirs (-xd) and file extensions (-xe)
199+
200+
```bash
201+
go run main.go -k main,let,for -p . -c -xd ".git,scnnr_bins,pkg,cmd,.zip" -xe ".yml,.sh,.md"
202+
.editorconfig:11:21:let
203+
.gitignore:3:1:main
204+
.gitignore:4:1:main
205+
.dockerignore:3:1:main
206+
.dockerignore:4:1:main
207+
Dockerfile:14:21:main
208+
Dockerfile:15:25:main
209+
main.go:25:9:main
210+
main.go:36:6:main
211+
main.go:46:8:for
212+
main.go:47:8:for
213+
main.go:48:8:for
214+
main.go:49:8:for
215+
main.go:70:65:for
216+
main.go:78:57:for
217+
main.go:84:23:for
218+
main.go:89:34:for
219+
main.go:125:3:for
220+
main.go:129:3:for
221+
main.go:172:3:for
222+
main.go:179:42:for
223+
main.go:186:3:for
224+
scnnr_bins.zip:2053:93:for
225+
scnnr_bins.zip:26325:8:let
226+
scnnr_bins.zip:32123:53:let
227+
```
228+
190229
# File Name Finder (NameFinder) (fnf)
191230

192231
```

main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ func main() {
101101
flag.StringVar(&size, "s", "", `REQUIRED SizeFinder MODE
102102
size: 1MB,10MB,100MB,1GB,10GB,100GB,1TB`)
103103

104+
var excludeDirs string
105+
flag.StringVar(&excludeDirs, "xd", "", `OPTIONAL Scnnr MODE
106+
comma-delimited list of directory names to exclude from scanning
107+
ex: -xd ".git,node_modules,.venv"`)
108+
109+
var excludeExts string
110+
flag.StringVar(&excludeExts, "xe", "", `OPTIONAL Scnnr MODE
111+
comma-delimited list of file extensions to exclude from scanning
112+
ex: -xe ".log,.tmp,.json"`)
113+
104114
flag.Parse()
105115

106116
directory = dir
@@ -127,13 +137,26 @@ func main() {
127137
log.Fatal("Position tracking flags (-l, -c) require keywords (-k)")
128138
}
129139

140+
var excludeDirsList []string
141+
var excludeExtsList []string
142+
143+
if excludeDirs != "" {
144+
excludeDirsList = strings.Split(excludeDirs, ",")
145+
}
146+
147+
if excludeExts != "" {
148+
excludeExtsList = strings.Split(excludeExts, ",")
149+
}
150+
130151
scanner := scnnr.Scanner{
131152
Regex: rgx,
132153
Keywords: keywords,
133154
Directory: directory,
134155
FileExtensions: extensions,
135156
ShowLines: showLines,
136157
ShowCols: showCols,
158+
ExcludeDirs: excludeDirsList,
159+
ExcludeExts: excludeExtsList,
137160
}
138161

139162
err := scanner.Scan()

pkg/scanner.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type Scanner struct {
2121
FileExtensions []string
2222
Keywords []string
2323
KeywordMatches []string
24+
ExcludeDirs []string
25+
ExcludeExts []string
2426
AllMatches []Match
2527
MatchedFilePaths []FileData
2628
}
@@ -112,13 +114,23 @@ func (s *Scanner) scan(path string, info os.FileInfo, err error) error {
112114
return err
113115
}
114116

117+
// Check if directory should be excluded
118+
if info.IsDir() && s.shouldExcludeDir(filepath.Base(path)) {
119+
return filepath.SkipDir
120+
}
121+
115122
if !info.IsDir() {
123+
fileExtension := filepath.Ext(path)
124+
125+
// Check if file extension should be excluded
126+
if s.shouldExcludeExt(fileExtension) {
127+
return nil
128+
}
129+
116130
if s.FileExtensions[0] == "" {
117131
s.MatchedFilePaths = append(s.MatchedFilePaths, FileData{path, info})
118132
} else {
119133
for _, pattern := range s.FileExtensions {
120-
fileExtension := filepath.Ext(path)
121-
122134
if fileExtension == pattern {
123135
s.MatchedFilePaths = append(s.MatchedFilePaths, FileData{path, info})
124136
}
@@ -208,6 +220,26 @@ func (s *Scanner) parse(match FileData) {
208220
file.Close()
209221
}
210222

223+
// shouldExcludeDir checks if a directory name should be excluded
224+
func (s *Scanner) shouldExcludeDir(dirName string) bool {
225+
for _, excludeDir := range s.ExcludeDirs {
226+
if dirName == excludeDir {
227+
return true
228+
}
229+
}
230+
return false
231+
}
232+
233+
// shouldExcludeExt checks if a file extension should be excluded
234+
func (s *Scanner) shouldExcludeExt(ext string) bool {
235+
for _, excludeExt := range s.ExcludeExts {
236+
if ext == excludeExt {
237+
return true
238+
}
239+
}
240+
return false
241+
}
242+
211243
func eachSlice(files []FileData) [][]FileData {
212244
var chunks [][]FileData
213245
var chunk []FileData

scripts/e2e.sh

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22

33
set -eou pipefail
44

5+
e2e_dir=$HOME
6+
7+
if [[ $ETE_DIR != "" ]]
8+
then
9+
e2e_dir=$ETE_DIR
10+
fi
11+
12+
##################################################
13+
##################################################
14+
##################################################
15+
516
echo "--- FILE SIZE FINDER DRY RUN: BEGIN ---"
617

7-
go run main.go -m fsf -s 1MB -d $HOME
18+
go run main.go -m fsf -s 1MB -d $e2e_dir
819

920
echo "--- FILE SIZE FINDER DRY RUN: DONE ---"
1021

@@ -16,7 +27,7 @@ sleep 2
1627

1728
echo "--- FILE NAME FINDER DRY RUN: BEGIN---"
1829

19-
go run main.go -m fnf -f main,DEFCON -p $HOME
30+
go run main.go -m fnf -f main,DEFCON -p $e2e_dir
2031

2132
echo "--- FILE NAME FINDER DRY RUN: DONE ---"
2233

@@ -28,12 +39,16 @@ sleep 2
2839

2940
echo "--- SCANNER DRY RUN: BEGIN---"
3041

31-
go run main.go -k main,const,let,var,for -p $HOME
42+
go run main.go -k main,const,let,var,for -p $e2e_dir
3243

3344
echo "--- SCANNER DRY RUN: DONE ---"
3445

3546
sleep 2
3647

48+
##################################################
49+
##################################################
50+
##################################################
51+
3752
echo "--- FILE FINGERPRINT FINDER DRY RUN: BEGIN---"
3853

3954
go run main.go -m fff -k $(go run cmd/checksum/main.go) -d .
@@ -42,16 +57,62 @@ echo "--- FILE FINGERPRINT FINDER DRY RUN: DONE---"
4257

4358
sleep 2
4459

60+
##################################################
61+
##################################################
62+
##################################################
63+
4564
echo "--- SCANNER LINE RUN: BEGIN---"
4665

47-
go run main.go -k main,const,let,var,for -p $HOME -l
66+
go run main.go -k main,const,let,var,for -p $e2e_dir -l
4867

4968
echo "--- SCANNER LINE RUN: DONE ---"
5069

5170
sleep 2
5271

72+
##################################################
73+
##################################################
74+
##################################################
75+
5376
echo "--- SCANNER LINE AND COL RUN: BEGIN---"
5477

55-
go run main.go -k main,const,let,var,for -p $HOME -c
78+
go run main.go -k main,const,let,var,for -p $e2e_dir -c
5679

5780
echo "--- SCANNER LINE AND COL RUN: DONE ---"
81+
82+
sleep 2
83+
84+
##################################################
85+
##################################################
86+
##################################################
87+
88+
echo "--- SCANNER DIR EXCLUDE RUN: BEGIN---"
89+
90+
sleep 2
91+
92+
go run main.go -k main,const,let,var,for -p $e2e_dir -c -xd ".git"
93+
94+
echo "--- SCANNER DIR EXCLUDE RUN: DONE ---"
95+
96+
sleep 2
97+
98+
##################################################
99+
##################################################
100+
##################################################
101+
102+
echo "--- SCANNER EXTENSION EXCLUDE RUN: BEGIN---"
103+
104+
go run main.go -k main,const,let,var,for -p $e2e_dir -c -xe ".yml,.sh,.md"
105+
106+
echo "--- SCANNER EXTENSION RUN: DONE ---"
107+
108+
sleep 2
109+
110+
##################################################
111+
##################################################
112+
##################################################
113+
114+
echo "--- SCANNER DIRECTORY AND EXTENSION EXCLUDE RUN: BEGIN---"
115+
116+
go run main.go -k main,const,let,var,for -p $e2e_dir -c -xd ".git" -xe ".yml,.sh,.md"
117+
118+
echo "--- SCANNER DIRECTORY AND EXTENSION RUN: DONE ---"

0 commit comments

Comments
 (0)