Finish config mapping

This commit is contained in:
Ben Sarmiento
2023-10-19 18:02:30 +02:00
parent ce0b2445b2
commit faba4e53ab
14 changed files with 397 additions and 89 deletions

View File

@@ -12,6 +12,7 @@ func loadV1Config(content []byte) (*ZurgConfigV1, error) {
if err := yaml.Unmarshal(content, &configV1); err != nil {
return nil, err
}
return &configV1, nil
}
@@ -52,7 +53,7 @@ func (z *ZurgConfigV1) matchFilter(fileID, torrentName string, filter *FilterCon
return true
}
if filter.RegexStr != "" {
regex := regexp.MustCompile(filter.RegexStr)
regex := compilePattern(filter.RegexStr)
if regex.MatchString(torrentName) {
return true
}
@@ -88,3 +89,39 @@ func (z *ZurgConfigV1) matchFilter(fileID, torrentName string, filter *FilterCon
}
return false
}
func compilePattern(pattern string) *regexp.Regexp {
flags := map[rune]string{
'i': "(?i)",
'm': "(?m)",
's': "(?s)",
'x': "(?x)",
}
lastSlash := strings.LastIndex(pattern, "/")
secondLastSlash := strings.LastIndex(pattern[:lastSlash], "/")
// Extract the core pattern
corePattern := pattern[secondLastSlash+1 : lastSlash]
// Extract and process flags
flagSection := pattern[lastSlash+1:]
flagString := ""
processedFlags := make(map[rune]bool)
for _, flag := range flagSection {
if replacement, ok := flags[flag]; ok && !processedFlags[flag] {
flagString += replacement
processedFlags[flag] = true
}
}
// Combine the processed flags with the core pattern
finalPattern := flagString + corePattern
// Validate pattern
if finalPattern == "" || finalPattern == flagString {
return nil
}
return regexp.MustCompile(finalPattern)
}