Skip to content

Commit 6d89b47

Browse files
authored
Merge pull request #9 from devops-kung-fu/0.0.2
fix: Fix --recursive and ignores
2 parents 73180e8 + ecdd2b1 commit 6d89b47

File tree

7 files changed

+61
-39
lines changed

7 files changed

+61
-39
lines changed

.luchaignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Test Comment
2-
lib/test.txt
32
lucha-sbom.json
43
lucha.yaml
54
lucha
65
go.sum
7-
go.mod
8-
.git
6+
go.mod

.vscode/launch.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
78

89

910

@@ -15,6 +16,14 @@
1516
"program": "${workspaceRoot}/main.go",
1617
"args": ["scan", "."]
1718
},
19+
{
20+
"name": "Debug (Don't ignore git)",
21+
"type": "go",
22+
"request": "launch",
23+
"mode": "debug",
24+
"program": "${workspaceRoot}/main.go",
25+
"args": ["scan", "--git", "."]
26+
},
1827
{
1928
"name": "Debug (Recursive)",
2029
"type": "go",

cmd/scan.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
)
1414

1515
var (
16+
includeGit bool
1617
recursive bool
1718
minSeverity int
1819
scanCmd = &cobra.Command{
@@ -38,6 +39,7 @@ var (
3839
}
3940

4041
fs.Recursive = recursive
42+
fs.IncludeGit = includeGit
4143

4244
err := initScan(fs)
4345

@@ -83,6 +85,7 @@ func init() {
8385
rootCmd.AddCommand(scanCmd)
8486
scanCmd.PersistentFlags().BoolVarP(&recursive, "recursive", "r", false, "If true, lucha will recurse subdirectories")
8587
scanCmd.PersistentFlags().IntVar(&minSeverity, "min-severity", 0, "Only report on severities higher than this value")
88+
scanCmd.PersistentFlags().BoolVarP(&includeGit, "git", "g", false, "If true, lucha not ignore the .git directory")
8689
}
8790

8891
func initScan(fs lib.FileSystem) (err error) {

lib/filesystem.go

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type FileSystem struct {
2121
fs afero.Fs
2222
SearchPath string
2323
Recursive bool
24+
IncludeGit bool
2425
}
2526

2627
//AbsoluteSearchPath returns the the absolute path for the (possibly) relative search path
@@ -54,36 +55,6 @@ func isUTF8(fs FileSystem, file afero.File) bool {
5455
return true
5556
}
5657

57-
// func canIgnore(file os.FileInfo, originalRoot string, path string, recursive bool) bool {
58-
// if !recursive && strings.Count(path, "/") > 1 {
59-
// return true
60-
// }
61-
// for _, ignore := range Ignores {
62-
// name := file.Name()
63-
// if ignore == name {
64-
// return true
65-
// }
66-
// if strings.HasPrefix(path, ignore) {
67-
// return true
68-
// }
69-
// if path != "." {
70-
// pathedIgnore := fmt.Sprintf("%s%s", originalRoot, ignore)
71-
// if strings.HasPrefix(path, pathedIgnore) {
72-
// return true
73-
// }
74-
// if strings.HasSuffix(path, ignore) {
75-
// return true
76-
// }
77-
// }
78-
79-
// }
80-
// return false
81-
// }
82-
83-
// func filterFiles(fs FileSystem, fileList []string, ignoreList []string) (filteredList []string) {
84-
85-
// }
86-
8758
func shouldIgnore(file string, ignoreList []string) (ignore bool) {
8859
var absIgnore []string
8960

@@ -107,6 +78,16 @@ func matchIgnore(s []string, str string) (matches bool) {
10778
return
10879
}
10980

81+
func shouldIgnoreDir(fs FileSystem, f os.FileInfo, path string) bool {
82+
if f.IsDir() && f.Name() == ".git" {
83+
return !fs.IncludeGit
84+
}
85+
if (f.IsDir() && !fs.Recursive) && fs.AbsoluteSearchPath() != path {
86+
return true
87+
}
88+
return false
89+
}
90+
11091
//BuildFileList gathers all of the files from the searchpath down the folder tree
11192
func BuildFileList(fs FileSystem) (fileList []string, err error) {
11293
path, err := filepath.Abs(fs.SearchPath)
@@ -115,6 +96,9 @@ func BuildFileList(fs FileSystem) (fileList []string, err error) {
11596
}
11697
ignores, _ := LoadIgnore(fs)
11798
err = fs.Afero().Walk(path, func(path string, f os.FileInfo, err error) error {
99+
if shouldIgnoreDir(fs, f, path) {
100+
return filepath.SkipDir
101+
}
118102
if shouldIgnore(path, ignores) {
119103
fileList = append(fileList, path)
120104
}

lib/filesystem_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,30 @@ func Test_NewOsFs(t *testing.T) {
8282
// _, err = f.BuildFileList("...", true)
8383
// assert.Error(t, err, "There should be an error because the folder ... shouldn't exist")
8484
// }
85+
86+
func TestFileSystem_AbsoluteSearchPath(t *testing.T) {
87+
fs := FileSystem{
88+
fs: afero.NewMemMapFs(),
89+
SearchPath: ".",
90+
}
91+
assert.Contains(t, fs.AbsoluteSearchPath(), "/lucha/lib")
92+
}
93+
94+
func Test_shouldIgnoreDir(t *testing.T) {
95+
fs := FileSystem{
96+
fs: afero.NewMemMapFs(),
97+
SearchPath: ".",
98+
}
99+
fs.Afero().Mkdir(".git", 0644)
100+
fi, _ := fs.Afero().ReadDir(fs.SearchPath)
101+
assert.Len(t, fi, 1)
102+
103+
dir := fi[0]
104+
105+
shouldIgnore := shouldIgnoreDir(fs, dir, "")
106+
assert.True(t, shouldIgnore)
107+
108+
fs.IncludeGit = true
109+
shouldIgnore = shouldIgnoreDir(fs, dir, "")
110+
assert.False(t, shouldIgnore)
111+
}

lib/issues.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func FindIssues(fs FileSystem, minSeverity int) (violations []ScanFile, violatio
4848
}
4949
}
5050

51+
// this could go into a verbose or trace flag
5152
// else {
5253
// fmt.Println("Ignoring ", file.Name())
5354
// }

lucha-sbom.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"bomFormat": "CycloneDX",
33
"specVersion": "1.2",
4-
"serialNumber": "urn:uuid:e01de6f9-1abf-4729-a0b2-916b5c0c2008",
4+
"serialNumber": "urn:uuid:47dac047-42ee-4302-9524-cfdda03d092f",
55
"version": 1,
66
"metadata": {
7-
"timestamp": "2021-07-10T14:59:17-06:00",
7+
"timestamp": "2021-07-12T12:21:46-06:00",
88
"tools": [
99
{
1010
"vendor": "CycloneDX",
@@ -31,11 +31,11 @@
3131
}
3232
],
3333
"component": {
34-
"bom-ref": "pkg:golang/github.com/devops-kung-fu/lucha@v0.0.0-20210709212051-962480554a8e",
34+
"bom-ref": "pkg:golang/github.com/devops-kung-fu/lucha@v0.0.0-20210712092326-93af2ed6aa54",
3535
"type": "application",
3636
"name": "github.com/devops-kung-fu/lucha",
37-
"version": "v0.0.0-20210709212051-962480554a8e",
38-
"purl": "pkg:golang/github.com/devops-kung-fu/lucha@v0.0.0-20210709212051-962480554a8e",
37+
"version": "v0.0.0-20210712092326-93af2ed6aa54",
38+
"purl": "pkg:golang/github.com/devops-kung-fu/lucha@v0.0.0-20210712092326-93af2ed6aa54",
3939
"externalReferences": [
4040
{
4141
"url": "https://github.com/devops-kung-fu/lucha",
@@ -520,7 +520,7 @@
520520
"ref": "pkg:golang/gopkg.in/yaml.v2@v2.4.0"
521521
},
522522
{
523-
"ref": "pkg:golang/github.com/devops-kung-fu/lucha@v0.0.0-20210709212051-962480554a8e",
523+
"ref": "pkg:golang/github.com/devops-kung-fu/lucha@v0.0.0-20210712092326-93af2ed6aa54",
524524
"dependsOn": [
525525
"pkg:golang/github.com/briandowns/spinner@v1.16.0",
526526
"pkg:golang/github.com/dustin/go-humanize@v1.0.0",

0 commit comments

Comments
 (0)