support exclude filter
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Arnas Udovic 2024-08-09 12:20:34 +03:00
parent e361e38b1e
commit aae1ca7456
2 changed files with 23 additions and 4 deletions

View file

@ -67,5 +67,5 @@ There are two main commands: `PopulateTree` to prepare source (format, add metad
Also both commands use `Config` object that can be prepared by `NewConfig` command. With it can be configurable readable formats, attachment directory, custom meta and excludes (not movable files by regexp).
`Tree` object has methods: `FileById` to get `File` by Id, `Slice` to get sub-tree of given path and `Filter` to filter tree by filter.
Filter contains array of meta key and searching value. `tag` key is searched as equal and other meta values of keys can contain part.
Filter contains array of meta key and searching value. `tag` key is searched as equal and other meta values of keys can contain part. If `-` is before key - filter is filter out.

25
tree.go
View file

@ -79,9 +79,16 @@ func (t Tree) Filter(filter map[string][]string) (Tree, bool) {
for _, f := range t.Files {
addFile := false
exclude := false
for option, values := range filter {
negative := false
if string(option[0]) == "-" {
negative = true
option = option[1:]
}
for _, value := range values {
if option == "tag" {
if !negative && option == "tag" {
for _, tag := range f.Tags {
if tag == value {
addFile = true
@ -89,12 +96,24 @@ func (t Tree) Filter(filter map[string][]string) (Tree, bool) {
}
continue
}
if strings.Contains(f.Meta[option], value) {
if negative && option == "tag" {
for _, tag := range f.Tags {
if tag == value {
exclude = true
}
}
continue
}
if !negative && strings.Contains(f.Meta[option], value) {
addFile = true
continue
}
if negative && strings.Contains(f.Meta[option], value) {
exclude = true
}
}
}
if addFile {
if addFile && !exclude {
found = true
filtered.Files = append(filtered.Files, f)
}