Add support for configs
This commit is contained in:
100
internal/config/v1.go
Normal file
100
internal/config/v1.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func loadV1Config(content []byte) (*ZurgConfigV1, error) {
|
||||
var configV1 ZurgConfigV1
|
||||
if err := yaml.Unmarshal(content, &configV1); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &configV1, nil
|
||||
}
|
||||
|
||||
func (z *ZurgConfigV1) GetVersion() string {
|
||||
return z.Version
|
||||
}
|
||||
|
||||
func (z *ZurgConfigV1) GetDirectories() []string {
|
||||
var rootDirectories []string
|
||||
for directory := range z.Directories {
|
||||
rootDirectories = append(rootDirectories, directory)
|
||||
}
|
||||
return rootDirectories
|
||||
}
|
||||
|
||||
func (z *ZurgConfigV1) GetGroupMap() map[string][]string {
|
||||
var groupMap = make(map[string][]string)
|
||||
for directory, val := range z.Directories {
|
||||
groupMap[val.Group] = append(groupMap[val.Group], directory)
|
||||
}
|
||||
return groupMap
|
||||
}
|
||||
|
||||
func (z *ZurgConfigV1) GetDirectoriesByGroup(group string) []string {
|
||||
var groupDirs []string
|
||||
for directory := range z.Directories {
|
||||
if z.Directories[directory].Group == group {
|
||||
groupDirs = append(groupDirs, directory)
|
||||
}
|
||||
}
|
||||
return groupDirs
|
||||
}
|
||||
|
||||
func (z *ZurgConfigV1) MeetsConditions(directory, fileID, torrentName string) bool {
|
||||
if _, ok := z.Directories[directory]; !ok {
|
||||
return false
|
||||
}
|
||||
for _, filter := range z.Directories[directory].Filters {
|
||||
if z.matchFilter(fileID, torrentName, filter) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (z *ZurgConfigV1) matchFilter(fileID, torrentName string, filter *FilterConditionsV1) bool {
|
||||
if filter.ID != "" && fileID == filter.ID {
|
||||
return true
|
||||
}
|
||||
if filter.RegexStr != "" {
|
||||
regex := regexp.MustCompile(filter.RegexStr)
|
||||
if regex.MatchString(torrentName) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if filter.ContainsStrict != "" && strings.Contains(torrentName, filter.ContainsStrict) {
|
||||
return true
|
||||
}
|
||||
if filter.Contains != "" && strings.Contains(strings.ToLower(torrentName), strings.ToLower(filter.Contains)) {
|
||||
return true
|
||||
}
|
||||
if filter.NotContainsStrict != "" && !strings.Contains(torrentName, filter.NotContainsStrict) {
|
||||
return true
|
||||
}
|
||||
if filter.NotContains != "" && !strings.Contains(strings.ToLower(torrentName), strings.ToLower(filter.NotContains)) {
|
||||
return true
|
||||
}
|
||||
if len(filter.And) > 0 {
|
||||
andResult := true
|
||||
for _, andFilter := range filter.And {
|
||||
andResult = andResult && z.matchFilter(fileID, torrentName, andFilter)
|
||||
if !andResult {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
if len(filter.Or) > 0 {
|
||||
for _, orFilter := range filter.Or {
|
||||
if z.matchFilter(fileID, torrentName, orFilter) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user