Add optional doublestar pattern matching

This commit is contained in:
Alex Yatskov 2016-07-03 20:08:31 -07:00
parent 7044e21201
commit 341e1c5bd1
3 changed files with 36 additions and 21 deletions

View File

@ -26,24 +26,27 @@ import (
"os"
"runtime"
"sync"
"github.com/bmatcuk/doublestar"
)
type context struct {
gs *goldsmith
plug Plugin
filters []string
input, output chan *file
}
func (ctx *context) chain() {
defer close(ctx.output)
init, _ := ctx.plug.(Initializer)
accept, _ := ctx.plug.(Accepter)
proc, _ := ctx.plug.(Processor)
fin, _ := ctx.plug.(Finalizer)
initializer, _ := ctx.plug.(Initializer)
accepter, _ := ctx.plug.(Accepter)
processor, _ := ctx.plug.(Processor)
finalizer, _ := ctx.plug.(Finalizer)
if init != nil {
if err := init.Initialize(ctx); err != nil {
if initializer != nil {
if err := initializer.Initialize(ctx); err != nil {
ctx.gs.fault(nil, err)
return
}
@ -56,15 +59,26 @@ func (ctx *context) chain() {
go func() {
defer wg.Done()
for f := range ctx.input {
if proc == nil || accept != nil && !accept.Accept(ctx, f) {
ctx.output <- f
} else {
accept := processor != nil && (accepter == nil || accepter.Accept(ctx, f))
if accept && len(ctx.filters) > 0 {
accept = false
for _, filter := range ctx.filters {
if match, _ := doublestar.PathMatch(filter, f.Path()); match {
accept = true
break
}
}
}
if accept {
if _, err := f.Seek(0, os.SEEK_SET); err != nil {
ctx.gs.fault(f, err)
}
if err := proc.Process(ctx, f); err != nil {
if err := processor.Process(ctx, f); err != nil {
ctx.gs.fault(f, err)
}
} else {
ctx.output <- f
}
}
}()
@ -72,8 +86,8 @@ func (ctx *context) chain() {
wg.Wait()
}
if fin != nil {
if err := fin.Finalize(ctx); err != nil {
if finalizer != nil {
if err := finalizer.Finalize(ctx); err != nil {
ctx.gs.fault(nil, err)
}
}

13
core.go
View File

@ -37,11 +37,12 @@ type goldsmith struct {
errorMtx sync.Mutex
}
func (gs *goldsmith) pushContext(plug Plugin) *context {
func (gs *goldsmith) pushContext(plug Plugin, filters []string) *context {
ctx := &context{
gs: gs,
plug: plug,
output: make(chan *file),
gs: gs,
plug: plug,
filters: filters,
output: make(chan *file),
}
if len(gs.contexts) > 0 {
@ -100,8 +101,8 @@ func (gs *goldsmith) fault(f *file, err error) {
// Goldsmith Implementation
//
func (gs *goldsmith) Chain(p Plugin) Goldsmith {
gs.pushContext(p)
func (gs *goldsmith) Chain(p Plugin, filters ...string) Goldsmith {
gs.pushContext(p, filters)
return gs
}

View File

@ -31,13 +31,13 @@ import (
)
type Goldsmith interface {
Chain(p Plugin) Goldsmith
Chain(p Plugin, filters ...string) Goldsmith
End(dstDir string) []error
}
func Begin(srcDir string) Goldsmith {
func Begin(srcDir string, filters ...string) Goldsmith {
gs := &goldsmith{srcDir: srcDir, refs: make(map[string]bool)}
gs.Chain(new(loader))
gs.Chain(new(loader), filters...)
return gs
}