Skip to content

Commit

Permalink
increase code health
Browse files Browse the repository at this point in the history
  • Loading branch information
emilwareus committed Oct 7, 2023
1 parent f924c3f commit aa27a55
Showing 1 changed file with 42 additions and 27 deletions.
69 changes: 42 additions & 27 deletions internal/file/fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,38 @@ const (
)

func isExcludedFile(filename string) bool {
return isExcludedByExtension(filename) ||
isExcludedByFilename(filename) ||
isExcludedByEnding(filename)
}

func isExcludedByExtension(filename string) bool {
filenameLower := strings.ToLower(filename)
for _, format := range EXCLUDED_EXT {
if filepath.Ext(filenameLower) == format {
return true
}
}
return false

Check failure on line 57 in internal/file/fingerprint.go

View workflow job for this annotation

GitHub Actions / Lint

return with no blank line before (nlreturn)
}

func isExcludedByFilename(filename string) bool {
filenameLower := strings.ToLower(filename)
for _, file := range ECLUDED_FILES {
if filenameLower == file {
return true
}
}
return false

Check failure on line 67 in internal/file/fingerprint.go

View workflow job for this annotation

GitHub Actions / Lint

return with no blank line before (nlreturn)
}

func isExcludedByEnding(filename string) bool {
filenameLower := strings.ToLower(filename)
for _, ending := range EXCLUDED_FILE_ENDINGS {
if strings.HasSuffix(filenameLower, ending) {
return true
}
}

return false

Check failure on line 77 in internal/file/fingerprint.go

View workflow job for this annotation

GitHub Actions / Lint

return with no blank line before (nlreturn)
}

Expand All @@ -88,46 +100,49 @@ func (f FileFingerprint) ToString() string {

return fmt.Sprintf("file=%x,%d,%s", f.fingerprint, f.contentLength, path)
}

func (f *Fingerprinter) FingerprintFiles(rootPath string, exclusions []string) (Fingerprints, error) {

if len(rootPath) == 0 {
rootPath = filepath.Base("")
}

fingerprints := Fingerprints{}

// Traverse files to find dependency file groups
err := filepath.Walk(
rootPath,
func(path string, fileInfo os.FileInfo, err error) error {
if err != nil {
return err
}
if !fileInfo.IsDir() && !excluded(exclusions, path) {

if isExcludedFile(path) {
return nil
}

fingerprint, err := computeMD5(path)
err := filepath.Walk(rootPath, func(path string, fileInfo os.FileInfo, err error) error {
if err != nil {
return err
}

// Skip directories, fileInfo.IsDir() is not reliable enough
if err != nil && !strings.Contains(err.Error(), "is a directory") {
return err
} else if err == nil {
fingerprints.Append(fingerprint)
}
if !shouldProcessFile(fileInfo, exclusions, path) {
return nil
}

}
fingerprint, err := computeMD5(path)
if err != nil {
return err
}

return nil
},
)
fingerprints.Append(fingerprint)
return nil
})

return fingerprints, err
}

func shouldProcessFile(fileInfo os.FileInfo, exclusions []string, path string) bool {
if fileInfo.IsDir() {
return false
}

if excluded(exclusions, path) {
return false
}

if isExcludedFile(path) {
return false
}

return true
}
func computeMD5(filename string) (FileFingerprint, error) {
file, err := os.Open(filename)
if err != nil {
Expand Down

0 comments on commit aa27a55

Please sign in to comment.