From 341e1c5bd1332877a599381977d01c2fa0db708e Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sun, 3 Jul 2016 20:08:31 -0700 Subject: [PATCH] Add optional doublestar pattern matching --- context.go | 38 ++++++++++++++++++++++++++------------ core.go | 13 +++++++------ goldsmith.go | 6 +++--- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/context.go b/context.go index 2e7879c..3bdbdfa 100644 --- a/context.go +++ b/context.go @@ -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) } } diff --git a/core.go b/core.go index 7dad67f..4a47e5d 100644 --- a/core.go +++ b/core.go @@ -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 } diff --git a/goldsmith.go b/goldsmith.go index fa32316..897752e 100644 --- a/goldsmith.go +++ b/goldsmith.go @@ -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 }