diff --git a/file.go b/file.go index ab151f6..da8d4f8 100644 --- a/file.go +++ b/file.go @@ -31,6 +31,7 @@ type file struct { path string meta map[string]interface{} buff *bytes.Buffer + err error } func (f *file) Path() string { @@ -50,6 +51,14 @@ func (f *file) SetProperty(key string, value interface{}) { f.meta[key] = value } +func (f *file) Error() error { + return f.err +} + +func (f *file) SetError(err error) { + f.err = err +} + func (f *file) Data() (*bytes.Buffer, error) { if f.buff != nil { return f.buff, nil @@ -57,12 +66,14 @@ func (f *file) Data() (*bytes.Buffer, error) { file, err := os.Open(f.path) if err != nil { + f.SetError(err) return nil, err } defer file.Close() var buff bytes.Buffer if _, err := buff.ReadFrom(file); err != nil { + f.SetError(err) return nil, err } diff --git a/goldsmith.go b/goldsmith.go index ae0fbfe..50d483f 100644 --- a/goldsmith.go +++ b/goldsmith.go @@ -23,21 +23,19 @@ package goldsmith import ( + "io/ioutil" "path/filepath" - "sync" "github.com/bmatcuk/doublestar" ) type stage struct { - input chan File - output chan File + input, output chan File } type goldsmith struct { stages []stage files chan File - wg sync.WaitGroup } func NewGoldsmith(path string) (Goldsmith, error) { @@ -84,22 +82,34 @@ func (gs *goldsmith) stage() stage { } func (gs *goldsmith) NewFile(path string) File { - return &file{path, make(map[string]interface{}), nil} + return &file{path: path} } func (gs *goldsmith) Apply(p Processor) Goldsmith { s := gs.stage() - - gs.wg.Add(1) - go func() { - p.Process(gs, s.input, s.output) - gs.wg.Done() - }() - + go p.Process(gs, s.input, s.output) return gs } -func (gs *goldsmith) Complete(path string) error { - gs.wg.Wait() - return nil +func (gs *goldsmith) Complete(path string) []File { + s := gs.stages[len(gs.stages)-1] + + var files []File + for file := range s.output { + data, err := file.Data() + if err != nil { + file.SetError(err) + continue + } + + absPath := filepath.Join(path, file.Path()) + if err := ioutil.WriteFile(absPath, data.Bytes(), 0644); err != nil { + file.SetError(err) + continue + } + + files = append(files, file) + } + + return files } diff --git a/types.go b/types.go index 8f3583b..946c3a9 100644 --- a/types.go +++ b/types.go @@ -35,6 +35,9 @@ type File interface { Property(key string) (interface{}, bool) SetProperty(key string, value interface{}) + Error() error + SetError(err error) + Data() (*bytes.Buffer, error) } @@ -44,5 +47,5 @@ type Processor interface { type Goldsmith interface { Apply(p Processor) Goldsmith - Complete(path string) error + Complete(path string) []File }