Add support for configs
This commit is contained in:
34
internal/config/load.go
Normal file
34
internal/config/load.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type ConfigInterface interface {
|
||||
GetVersion() string
|
||||
GetDirectories() []string
|
||||
MeetsConditions(directory, fileID, fileName string) bool
|
||||
}
|
||||
|
||||
func LoadZurgConfig(filename string) (ConfigInterface, error) {
|
||||
content, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var initialConfig ZurgConfig
|
||||
if err := yaml.Unmarshal(content, &initialConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch initialConfig.Version {
|
||||
case "v1":
|
||||
return loadV1Config(content)
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid config version: %s", initialConfig.Version)
|
||||
}
|
||||
}
|
||||
5
internal/config/types.go
Normal file
5
internal/config/types.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package config
|
||||
|
||||
type ZurgConfig struct {
|
||||
Version string `yaml:"zurg"`
|
||||
}
|
||||
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
|
||||
}
|
||||
22
internal/config/v1types.go
Normal file
22
internal/config/v1types.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package config
|
||||
|
||||
type ZurgConfigV1 struct {
|
||||
ZurgConfig
|
||||
Directories map[string]*DirectoryFilterConditionsV1 `yaml:"directories"`
|
||||
Duplicates bool `yaml:"duplicates"`
|
||||
}
|
||||
type DirectoryFilterConditionsV1 struct {
|
||||
Group string `yaml:"group"`
|
||||
Filters []*FilterConditionsV1 `yaml:"filters"`
|
||||
}
|
||||
|
||||
type FilterConditionsV1 struct {
|
||||
RegexStr string `yaml:"regex"`
|
||||
Contains string `yaml:"contains"`
|
||||
ContainsStrict string `yaml:"contains_strict"`
|
||||
NotContains string `yaml:"not_contains"`
|
||||
NotContainsStrict string `yaml:"not_contains_strict"`
|
||||
ID string `yaml:"id"`
|
||||
And []*FilterConditionsV1 `yaml:"and"`
|
||||
Or []*FilterConditionsV1 `yaml:"or"`
|
||||
}
|
||||
Reference in New Issue
Block a user