Skip to content

Commit

Permalink
Merge pull request #9 from devops-kung-fu/0.0.2
Browse files Browse the repository at this point in the history
fix: Fix --recursive and ignores
  • Loading branch information
djschleen authored Jul 12, 2021
2 parents 73180e8 + ecdd2b1 commit 6d89b47
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 39 deletions.
4 changes: 1 addition & 3 deletions .luchaignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Test Comment
lib/test.txt
lucha-sbom.json
lucha.yaml
lucha
go.sum
go.mod
.git
go.mod
9 changes: 9 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [




Expand All @@ -15,6 +16,14 @@
"program": "${workspaceRoot}/main.go",
"args": ["scan", "."]
},
{
"name": "Debug (Don't ignore git)",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}/main.go",
"args": ["scan", "--git", "."]
},
{
"name": "Debug (Recursive)",
"type": "go",
Expand Down
3 changes: 3 additions & 0 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

var (
includeGit bool
recursive bool
minSeverity int
scanCmd = &cobra.Command{
Expand All @@ -38,6 +39,7 @@ var (
}

fs.Recursive = recursive
fs.IncludeGit = includeGit

err := initScan(fs)

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

func initScan(fs lib.FileSystem) (err error) {
Expand Down
44 changes: 14 additions & 30 deletions lib/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type FileSystem struct {
fs afero.Fs
SearchPath string
Recursive bool
IncludeGit bool
}

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

// func canIgnore(file os.FileInfo, originalRoot string, path string, recursive bool) bool {
// if !recursive && strings.Count(path, "/") > 1 {
// return true
// }
// for _, ignore := range Ignores {
// name := file.Name()
// if ignore == name {
// return true
// }
// if strings.HasPrefix(path, ignore) {
// return true
// }
// if path != "." {
// pathedIgnore := fmt.Sprintf("%s%s", originalRoot, ignore)
// if strings.HasPrefix(path, pathedIgnore) {
// return true
// }
// if strings.HasSuffix(path, ignore) {
// return true
// }
// }

// }
// return false
// }

// func filterFiles(fs FileSystem, fileList []string, ignoreList []string) (filteredList []string) {

// }

func shouldIgnore(file string, ignoreList []string) (ignore bool) {
var absIgnore []string

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

func shouldIgnoreDir(fs FileSystem, f os.FileInfo, path string) bool {
if f.IsDir() && f.Name() == ".git" {
return !fs.IncludeGit
}
if (f.IsDir() && !fs.Recursive) && fs.AbsoluteSearchPath() != path {
return true
}
return false
}

//BuildFileList gathers all of the files from the searchpath down the folder tree
func BuildFileList(fs FileSystem) (fileList []string, err error) {
path, err := filepath.Abs(fs.SearchPath)
Expand All @@ -115,6 +96,9 @@ func BuildFileList(fs FileSystem) (fileList []string, err error) {
}
ignores, _ := LoadIgnore(fs)
err = fs.Afero().Walk(path, func(path string, f os.FileInfo, err error) error {
if shouldIgnoreDir(fs, f, path) {
return filepath.SkipDir
}
if shouldIgnore(path, ignores) {
fileList = append(fileList, path)
}
Expand Down
27 changes: 27 additions & 0 deletions lib/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,30 @@ func Test_NewOsFs(t *testing.T) {
// _, err = f.BuildFileList("...", true)
// assert.Error(t, err, "There should be an error because the folder ... shouldn't exist")
// }

func TestFileSystem_AbsoluteSearchPath(t *testing.T) {
fs := FileSystem{
fs: afero.NewMemMapFs(),
SearchPath: ".",
}
assert.Contains(t, fs.AbsoluteSearchPath(), "/lucha/lib")
}

func Test_shouldIgnoreDir(t *testing.T) {
fs := FileSystem{
fs: afero.NewMemMapFs(),
SearchPath: ".",
}
fs.Afero().Mkdir(".git", 0644)
fi, _ := fs.Afero().ReadDir(fs.SearchPath)
assert.Len(t, fi, 1)

dir := fi[0]

shouldIgnore := shouldIgnoreDir(fs, dir, "")
assert.True(t, shouldIgnore)

fs.IncludeGit = true
shouldIgnore = shouldIgnoreDir(fs, dir, "")
assert.False(t, shouldIgnore)
}
1 change: 1 addition & 0 deletions lib/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func FindIssues(fs FileSystem, minSeverity int) (violations []ScanFile, violatio
}
}

// this could go into a verbose or trace flag
// else {
// fmt.Println("Ignoring ", file.Name())
// }
Expand Down
12 changes: 6 additions & 6 deletions lucha-sbom.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"bomFormat": "CycloneDX",
"specVersion": "1.2",
"serialNumber": "urn:uuid:e01de6f9-1abf-4729-a0b2-916b5c0c2008",
"serialNumber": "urn:uuid:47dac047-42ee-4302-9524-cfdda03d092f",
"version": 1,
"metadata": {
"timestamp": "2021-07-10T14:59:17-06:00",
"timestamp": "2021-07-12T12:21:46-06:00",
"tools": [
{
"vendor": "CycloneDX",
Expand All @@ -31,11 +31,11 @@
}
],
"component": {
"bom-ref": "pkg:golang/github.com/devops-kung-fu/[email protected]20210709212051-962480554a8e",
"bom-ref": "pkg:golang/github.com/devops-kung-fu/[email protected]20210712092326-93af2ed6aa54",
"type": "application",
"name": "github.com/devops-kung-fu/lucha",
"version": "v0.0.0-20210709212051-962480554a8e",
"purl": "pkg:golang/github.com/devops-kung-fu/[email protected]20210709212051-962480554a8e",
"version": "v0.0.0-20210712092326-93af2ed6aa54",
"purl": "pkg:golang/github.com/devops-kung-fu/[email protected]20210712092326-93af2ed6aa54",
"externalReferences": [
{
"url": "https://github.com/devops-kung-fu/lucha",
Expand Down Expand Up @@ -520,7 +520,7 @@
"ref": "pkg:golang/gopkg.in/[email protected]"
},
{
"ref": "pkg:golang/github.com/devops-kung-fu/[email protected]20210709212051-962480554a8e",
"ref": "pkg:golang/github.com/devops-kung-fu/[email protected]20210712092326-93af2ed6aa54",
"dependsOn": [
"pkg:golang/github.com/briandowns/[email protected]",
"pkg:golang/github.com/dustin/[email protected]",
Expand Down

0 comments on commit 6d89b47

Please sign in to comment.