From ce668ab5add1c073b11da84a64577b0e6836a99b Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 7 Nov 2015 13:36:20 +0900 Subject: [PATCH] Simplify API --- goldsmith.go | 47 +++++++++++------------------------------------ types.go | 16 +++++----------- util.go | 30 ------------------------------ 3 files changed, 16 insertions(+), 77 deletions(-) diff --git a/goldsmith.go b/goldsmith.go index a21b3ce..1da9bc7 100644 --- a/goldsmith.go +++ b/goldsmith.go @@ -28,7 +28,6 @@ import ( "os" "path" "path/filepath" - "sync" ) type stage struct { @@ -124,11 +123,10 @@ func (gs *goldsmith) exportFile(file *File) { var f *os.File if f, file.Err = os.Create(absPath); file.Err == nil { + defer f.Close() if _, file.Err = f.Write(file.Buff.Bytes()); file.Err == nil { gs.refs[file.Path] = true } - - f.Close() } } @@ -142,36 +140,19 @@ func (gs *goldsmith) makeStage() stage { return s } -func (gs *goldsmith) chainSingle(s stage, cs ChainerSingle, globs []string) { - defer close(s.output) +func (gs *goldsmith) chain(s stage, c Chainer) { + f, _ := c.(Filterer) - var wg sync.WaitGroup - for file := range s.input { - wg.Add(1) - go func(f *File) { - defer wg.Done() - if skipFile(f, globs) { - s.output <- f - } else { - s.output <- cs.ChainSingle(gs, f) - } - }(file) - } + allowed := make(chan *File) + defer close(allowed) - wg.Wait() -} - -func (gs *goldsmith) chainMultiple(s stage, cm ChainerMultiple, globs []string) { - filtered := make(chan *File) - defer close(filtered) - - go cm.ChainMultiple(gs, filtered, s.output) + go c.Chain(gs, allowed, s.output) for file := range s.input { - if skipFile(file, globs) { + if f.Filter(file.Path) { s.output <- file } else { - filtered <- file + allowed <- file } } } @@ -207,18 +188,13 @@ func (gs *goldsmith) DstDir() string { return gs.dstDir } -func (gs *goldsmith) Chain(ctx Config) Goldsmith { +func (gs *goldsmith) Chain(c Chainer, err error) Goldsmith { if gs.err != nil { return gs } - if gs.err = ctx.Err; gs.err == nil { - switch c := ctx.Chainer.(type) { - case ChainerSingle: - go gs.chainSingle(gs.makeStage(), c, ctx.Globs) - case ChainerMultiple: - go gs.chainMultiple(gs.makeStage(), c, ctx.Globs) - } + if gs.err = err; gs.err == nil { + go gs.chain(gs.makeStage(), c) } return gs @@ -234,6 +210,5 @@ func (gs *goldsmith) Complete() ([]*File, error) { } gs.cleanupFiles() - return files, gs.err } diff --git a/types.go b/types.go index d4df79c..8cec53a 100644 --- a/types.go +++ b/types.go @@ -25,16 +25,16 @@ package goldsmith import "bytes" type Goldsmith interface { - Chain(cfg Config) Goldsmith + Chain(c Chainer, err error) Goldsmith Complete() ([]*File, error) } -type ChainerSingle interface { - ChainSingle(ctx Context, file *File) *File +type Chainer interface { + Chain(ctx Context, input, output chan *File) } -type ChainerMultiple interface { - ChainMultiple(ctx Context, input, output chan *File) +type Filterer interface { + Filter(path string) bool } type File struct { @@ -51,9 +51,3 @@ type Context interface { SrcDir() string DstDir() string } - -type Config struct { - Chainer interface{} - Globs []string - Err error -} diff --git a/util.go b/util.go index 718482c..6f73d6c 100644 --- a/util.go +++ b/util.go @@ -27,36 +27,6 @@ import ( "path/filepath" ) -func globMatch(globs []string, name string) (bool, error) { - var ( - match bool - err error - ) - - base := filepath.Base(name) - for _, glob := range globs { - match, err = filepath.Match(glob, base) - if err != nil || match { - break - } - } - - return match, err -} - -func skipFile(file *File, globs []string) bool { - if file.Err != nil { - return true - } - - matched := true - if len(globs) > 0 { - matched, file.Err = globMatch(globs, file.Path) - } - - return !matched -} - func scanDir(root string) (files, dirs []string, err error) { err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err != nil {