-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffe534b
commit 5f25c38
Showing
9 changed files
with
322 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package filters | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/permafrost-dev/eget/lib/assets" | ||
) | ||
|
||
type FilterHandler func(assets.Asset, []string) bool | ||
|
||
type FilterAction byte | ||
|
||
const ( | ||
FilterActionInclude FilterAction = iota | ||
FilterActionExclude FilterAction = iota | ||
) | ||
|
||
type Filter struct { | ||
Name string | ||
Handler FilterHandler | ||
Action FilterAction | ||
Args []string | ||
Definition string | ||
} | ||
|
||
func NewFilter(name string, handler FilterHandler, action FilterAction, args ...string) *Filter { | ||
return &Filter{ | ||
Name: name, | ||
Handler: handler, | ||
Action: action, | ||
Args: args, | ||
Definition: name + "(" + strings.Join(args, ",") + ")", | ||
} | ||
} | ||
|
||
func (f *Filter) Apply(item assets.Asset) bool { | ||
return f.Handler(item, f.Args) | ||
} | ||
|
||
func (f *Filter) WithArgs(args ...string) *Filter { | ||
f.Args = args | ||
return f | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package filters | ||
|
||
import ( | ||
"path" | ||
"strings" | ||
|
||
"github.com/permafrost-dev/eget/lib/assets" | ||
) | ||
|
||
var FilterMap = map[string]*Filter{ | ||
"all": NewFilter("all", allHandler, FilterActionInclude), | ||
"any": NewFilter("any", anyHandler, FilterActionInclude), | ||
"ext": NewFilter("ext", extensionHandler, FilterActionInclude), | ||
"none": NewFilter("none", noneHandler, FilterActionExclude), | ||
"has": NewFilter("has", hasHandler, FilterActionInclude), | ||
} | ||
|
||
var anyHandler FilterHandler = func(item assets.Asset, args []string) bool { | ||
for _, arg := range args { | ||
if strings.EqualFold(item.Name, arg) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
var allHandler FilterHandler = func(item assets.Asset, args []string) bool { | ||
for _, arg := range args { | ||
if !strings.EqualFold(item.Name, arg) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
var hasHandler FilterHandler = func(item assets.Asset, args []string) bool { | ||
for _, arg := range args { | ||
if !strings.EqualFold(item.Name, arg) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
var noneHandler FilterHandler = func(item assets.Asset, args []string) bool { | ||
for _, arg := range args { | ||
if strings.EqualFold(item.Name, arg) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
var extensionHandler FilterHandler = func(item assets.Asset, args []string) bool { | ||
extension := path.Ext(item.Name) | ||
|
||
for _, arg := range args { | ||
if strings.EqualFold(extension, arg) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package filters_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/permafrost-dev/eget/lib/assets" | ||
. "github.com/permafrost-dev/eget/lib/filters" | ||
) | ||
|
||
var anyHandler = FilterMap["any"].Handler | ||
var allHandler = FilterMap["all"].Handler | ||
var hasHandler = FilterMap["has"].Handler | ||
var noneHandler = FilterMap["none"].Handler | ||
var extensionHandler = FilterMap["ext"].Handler | ||
|
||
var _ = Describe("Filters", func() { | ||
Context("Handlers", func() { | ||
asset1 := assets.Asset{Name: "file1.txt"} | ||
asset2 := assets.Asset{Name: "file2.exe"} | ||
|
||
It("anyHandler should return true if any argument matches the asset name", func() { | ||
Expect(anyHandler(asset1, []string{"file1.txt", "file2.exe"})).To(BeTrue()) | ||
Expect(anyHandler(asset2, []string{"file1.txt", "file2.exe"})).To(BeTrue()) | ||
Expect(anyHandler(asset1, []string{"file2.exe"})).To(BeFalse()) | ||
}) | ||
|
||
It("allHandler should return true if all arguments match the asset name", func() { | ||
Expect(allHandler(asset1, []string{"file1.txt", "file1.txt"})).To(BeTrue()) | ||
Expect(allHandler(asset2, []string{"file2.exe", "file2.exe"})).To(BeTrue()) | ||
Expect(allHandler(asset1, []string{"file1.txt", "file2.exe"})).To(BeFalse()) | ||
}) | ||
|
||
It("hasHandler should return true if any argument matches the asset name", func() { | ||
Expect(hasHandler(asset1, []string{"file1.txt"})).To(BeTrue()) | ||
Expect(hasHandler(asset2, []string{"file2.exe"})).To(BeTrue()) | ||
Expect(hasHandler(asset1, []string{"file2.exe"})).To(BeFalse()) | ||
}) | ||
|
||
It("noneHandler should return true if no arguments match the asset name", func() { | ||
Expect(noneHandler(asset1, []string{"file2.exe"})).To(BeTrue()) | ||
Expect(noneHandler(asset2, []string{"file1.txt"})).To(BeTrue()) | ||
Expect(noneHandler(asset1, []string{"file1.txt"})).To(BeFalse()) | ||
}) | ||
|
||
It("extensionHandler should return true if the extension matches the asset name", func() { | ||
Expect(extensionHandler(asset1, []string{".txt"})).To(BeTrue()) | ||
Expect(extensionHandler(asset2, []string{".exe"})).To(BeTrue()) | ||
Expect(extensionHandler(asset1, []string{".exe"})).To(BeFalse()) | ||
}) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package filters | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type Parser struct { | ||
} | ||
|
||
func NewParser() *Parser { | ||
return &Parser{} | ||
} | ||
|
||
func (p *Parser) ParseDefinitions(definitions string) []*Filter { | ||
defs := strings.Split(definitions, ";") | ||
|
||
filters := make([]*Filter, 0) | ||
|
||
for _, def := range defs { | ||
filter := p.ParseDefinition(def) | ||
if filter != nil { | ||
filters = append(filters, filter) | ||
} | ||
} | ||
|
||
return filters | ||
} | ||
|
||
// ParseDefinition parses a string definition of a filter, like "always(abc,def)" or "never(.deb)" | ||
// and returns a Filter struct with the appropriate values set. | ||
func (p *Parser) ParseDefinition(definition string) *Filter { | ||
re := regexp.MustCompile(`([a-zA-Z]+)\((.*)\)`) | ||
matches := re.FindStringSubmatch(definition) | ||
|
||
if len(matches) < 3 { | ||
return nil | ||
} | ||
|
||
name := matches[1] | ||
argStr := matches[2] | ||
args := strings.Split(argStr, ",") | ||
|
||
if FilterMap[name] == nil { | ||
return nil | ||
} | ||
|
||
return &Filter{ | ||
Name: FilterMap[name].Name, | ||
Handler: FilterMap[name].Handler, | ||
Action: FilterMap[name].Action, | ||
Args: args, | ||
Definition: definition, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package filters_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/permafrost-dev/eget/lib/assets" | ||
. "github.com/permafrost-dev/eget/lib/filters" | ||
) | ||
|
||
var _ = Describe("Filters", func() { | ||
Context("NewFilter", func() { | ||
It("should create a new filter with the given parameters", func() { | ||
handler := func(a assets.Asset, args []string) bool { return true } | ||
filter := NewFilter("test", handler, FilterActionInclude, "arg1", "arg2") | ||
|
||
Expect(filter.Name).To(Equal("test")) | ||
Expect(filter.Action).To(Equal(FilterActionInclude)) | ||
Expect(filter.Args).To(Equal([]string{"arg1", "arg2"})) | ||
Expect(filter.Definition).To(Equal("test(arg1,arg2)")) | ||
}) | ||
}) | ||
|
||
Context("Filter Apply", func() { | ||
It("should apply the handler to the asset", func() { | ||
handler := func(a assets.Asset, args []string) bool { return a.Name == "test" } | ||
filter := NewFilter("test", handler, FilterActionInclude) | ||
asset := assets.Asset{Name: "test"} | ||
|
||
Expect(filter.Apply(asset)).To(BeTrue()) | ||
}) | ||
}) | ||
|
||
Context("Filter WithArgs", func() { | ||
It("should set the arguments and return the filter", func() { | ||
handler := func(a assets.Asset, args []string) bool { return true } | ||
filter := NewFilter("test", handler, FilterActionInclude) | ||
filter.WithArgs("newArg1", "newArg2") | ||
|
||
Expect(filter.Args).To(Equal([]string{"newArg1", "newArg2"})) | ||
}) | ||
}) | ||
|
||
Context("Parser", func() { | ||
parser := NewParser() | ||
|
||
It("should parse multiple definitions", func() { | ||
definitions := "all(file1.txt);none(file2.exe)" | ||
filters := parser.ParseDefinitions(definitions) | ||
|
||
Expect(filters).To(HaveLen(2)) | ||
Expect(filters[0].Name).To(Equal("all")) | ||
Expect(filters[1].Name).To(Equal("none")) | ||
}) | ||
|
||
It("should parse a single definition", func() { | ||
definition := "all(file1.txt)" | ||
filter := parser.ParseDefinition(definition) | ||
|
||
Expect(filter).NotTo(BeNil()) | ||
Expect(filter.Name).To(Equal("all")) | ||
Expect(filter.Args).To(Equal([]string{"file1.txt"})) | ||
}) | ||
|
||
It("should return nil for invalid definitions", func() { | ||
definition := "invalid()" | ||
filter := parser.ParseDefinition(definition) | ||
|
||
Expect(filter).To(BeNil()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package filters | ||
package utilities | ||
|
||
import ( | ||
"regexp" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters