1
0
mirror of https://github.com/drone/drone-cli.git synced 2024-11-23 17:22:09 +01:00
drone-cli/builder/builder.go

39 lines
683 B
Go
Raw Normal View History

package builder
2015-03-11 00:05:32 +01:00
import "sync"
2015-03-10 07:25:16 +01:00
// Builder executes a set of build step handlers.
2015-03-10 04:15:52 +01:00
type Builder struct {
2015-03-11 00:05:32 +01:00
sync.Mutex
2015-03-10 04:15:52 +01:00
handlers []Handler
}
2015-03-10 07:25:16 +01:00
// Handle registers the build step handler.
2015-03-10 04:15:52 +01:00
func (b *Builder) Handle(h Handler) {
2015-03-11 00:05:32 +01:00
b.Lock()
2015-03-10 04:15:52 +01:00
b.handlers = append(b.handlers, h)
2015-03-11 06:23:23 +01:00
b.Unlock()
2015-03-10 04:15:52 +01:00
}
2015-03-10 04:15:52 +01:00
// Build runs all build step handlers.
2015-03-10 07:25:16 +01:00
func (b *Builder) Build(rw *ResultWriter) (err error) {
2015-03-10 04:15:52 +01:00
for _, h := range b.handlers {
2015-03-10 07:25:16 +01:00
err = h.Build(rw)
if err != nil || rw.exitCode != 0 {
2015-03-10 04:15:52 +01:00
break
}
}
2015-03-10 04:15:52 +01:00
return nil
}
2015-03-10 07:25:16 +01:00
// Cancel cancels all running build steps.
2015-03-10 04:15:52 +01:00
func (b *Builder) Cancel() {
2015-03-11 00:05:32 +01:00
b.Lock()
defer b.Unlock()
2015-03-11 06:23:23 +01:00
for i := len(b.handlers) - 1; i >= 0; i-- {
h := b.handlers[i]
2015-03-11 04:52:35 +01:00
h.Cancel()
}
2015-03-10 04:15:52 +01:00
}