mirror of
https://github.com/stashapp/stash.git
synced 2025-12-11 02:42:43 +01:00
* Add a space after // comments
For consistency, the commentFormatting lint checker suggests a space
after each // comment block. This commit handles all the spots in
the code where that is needed.
* Rewrite documentation on functions
Use the Go idiom of commenting:
* First sentence declares the purpose.
* First word is the name being declared
The reason this style is preferred is such that grep is able to find
names the user might be interested in. Consider e.g.,
go doc -all pkg/ffmpeg | grep -i transcode
in which case a match will tell you the name of the function you are
interested in.
* Remove old code comment-blocks
There are some commented out old code blocks in the code base. These are
either 3 years old, or 2 years old. By now, I don't think their use is
going to come back any time soon, and Git will track old pieces of
deleted code anyway.
Opt for deletion.
* Reorder imports
Split stdlib imports from non-stdlib imports in files we are touching.
* Use a range over an iteration variable
Probably more go-idiomatic, and the code needed comment-fixing anyway.
* Use time.After rather than rolling our own
The idiom here is common enough that the stdlib contains a function for
it. Use the stdlib function over our own variant.
* Enable the commentFormatting linter
86 lines
1.7 KiB
Go
86 lines
1.7 KiB
Go
package manager
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
|
)
|
|
|
|
func excludeFiles(files []string, patterns []string) ([]string, int) {
|
|
if patterns == nil {
|
|
logger.Infof("No exclude patterns in config")
|
|
return files, 0
|
|
} else {
|
|
var results []string
|
|
var exclCount int
|
|
|
|
fileRegexps := generateRegexps(patterns)
|
|
|
|
if len(fileRegexps) == 0 {
|
|
logger.Infof("Excluded 0 files from scan")
|
|
return files, 0
|
|
}
|
|
|
|
for _, f := range files {
|
|
if matchFileSimple(f, fileRegexps) {
|
|
logger.Infof("File matched pattern. Excluding:\"%s\"", f)
|
|
exclCount++
|
|
} else {
|
|
// if pattern doesn't match add file to list
|
|
results = append(results, f)
|
|
}
|
|
}
|
|
logger.Infof("Excluded %d file(s) from scan", exclCount)
|
|
return results, exclCount
|
|
}
|
|
}
|
|
|
|
func matchFileRegex(file string, fileRegexps []*regexp.Regexp) bool {
|
|
for _, regPattern := range fileRegexps {
|
|
if regPattern.MatchString(strings.ToLower(file)) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func matchFile(file string, patterns []string) bool {
|
|
if patterns != nil {
|
|
fileRegexps := generateRegexps(patterns)
|
|
|
|
return matchFileRegex(file, fileRegexps)
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func generateRegexps(patterns []string) []*regexp.Regexp {
|
|
|
|
var fileRegexps []*regexp.Regexp
|
|
|
|
for _, pattern := range patterns {
|
|
reg, err := regexp.Compile(strings.ToLower(pattern))
|
|
if err != nil {
|
|
logger.Errorf("Exclude :%v", err)
|
|
} else {
|
|
fileRegexps = append(fileRegexps, reg)
|
|
}
|
|
}
|
|
|
|
if len(fileRegexps) == 0 {
|
|
return nil
|
|
} else {
|
|
return fileRegexps
|
|
}
|
|
|
|
}
|
|
|
|
func matchFileSimple(file string, regExps []*regexp.Regexp) bool {
|
|
for _, regPattern := range regExps {
|
|
if regPattern.MatchString(strings.ToLower(file)) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|