~foosoft/goldsmith

679f2344cb521d37d67fe32285fb2f8f6c3db322 — Alex Yatskov 9 years ago 1ebb78f
Better error handling
2 files changed, 47 insertions(+), 47 deletions(-)

M goldsmith.go
M types.go
M goldsmith.go => goldsmith.go +42 -41
@@ 75,22 75,14 @@ func (gs *goldsmith) makeStage() stage {
	return s
}

func (gs *goldsmith) NewFile(relPath string) File {
	return &file{relPath: relPath}
}

func (gs *goldsmith) SetError(err error) {
	gs.err = err
}

func (gs *goldsmith) taskSingle(ts TaskerSingle) {
func (gs *goldsmith) chainSingle(ts ChainerSingle) {
	s := gs.makeStage()

	var wg sync.WaitGroup
	for file := range s.input {
		wg.Add(1)
		go func(f File) {
			s.output <- ts.TaskSingle(gs, f)
			s.output <- ts.ChainSingle(gs, f)
			wg.Done()
		}(file)
	}


@@ 101,18 93,26 @@ func (gs *goldsmith) taskSingle(ts TaskerSingle) {
	}()
}

func (gs *goldsmith) taskMultiple(tm TaskerMultiple) {
func (gs *goldsmith) chainMultiple(tm ChainerMultiple) {
	s := gs.makeStage()
	tm.TaskMultiple(gs, s.input, s.output)
	tm.ChainMultiple(gs, s.input, s.output)
}

func (gs *goldsmith) NewFile(relPath string) File {
	return &file{relPath: relPath}
}

func (gs *goldsmith) Task(task interface{}) Goldsmith {
	if gs.err == nil {
		switch t := task.(type) {
		case TaskerSingle:
			gs.taskSingle(t)
		case TaskerMultiple:
			gs.taskMultiple(t)
func (gs *goldsmith) Chain(chain interface{}, err error) Goldsmith {
	if gs.err != nil {
		return gs
	}

	if gs.err = err; gs.err != nil {
		switch t := chain.(type) {
		case ChainerSingle:
			gs.chainSingle(t)
		case ChainerMultiple:
			gs.chainMultiple(t)
		}
	}



@@ 124,28 124,29 @@ func (gs *goldsmith) Complete(dstDir string) ([]File, error) {

	var files []File
	for file := range s.output {
		data := file.Data()
		if data == nil {
			continue
		}

		absPath := filepath.Join(dstDir, file.Path())

		if err := os.MkdirAll(path.Dir(absPath), 0755); err != nil {
			file.SetError(err)
			continue
		}

		f, err := os.Create(absPath)
		if err != nil {
			file.SetError(err)
			continue
		}
		defer f.Close()

		if _, err := f.Write(data); err != nil {
			file.SetError(err)
			continue
		if file.Error() == nil {
			data := file.Data()
			if data == nil {
				continue
			}

			absPath := filepath.Join(dstDir, file.Path())
			if err := os.MkdirAll(path.Dir(absPath), 0755); err != nil {
				file.SetError(err)
				continue
			}

			f, err := os.Create(absPath)
			if err != nil {
				file.SetError(err)
				continue
			}
			defer f.Close()

			if _, err := f.Write(data); err != nil {
				file.SetError(err)
				continue
			}
		}

		files = append(files, file)

M types.go => types.go +5 -6
@@ 23,21 23,20 @@
package goldsmith

type Goldsmith interface {
	Task(task interface{}) Goldsmith
	Chain(task interface{}, err error) Goldsmith
	Complete(dstDir string) ([]File, error)
}

type TaskerSingle interface {
	TaskSingle(ctx Context, file File) File
type ChainerSingle interface {
	ChainSingle(ctx Context, file File) File
}

type TaskerMultiple interface {
	TaskMultiple(ctx Context, input, output chan File)
type ChainerMultiple interface {
	ChainMultiple(ctx Context, input, output chan File)
}

type Context interface {
	NewFile(srcDir string) File
	SetError(err error)
}

type File interface {

Do not follow this link