bust/pkg/pipelines/pipeline.go

45 lines
839 B
Go
Raw Permalink Normal View History

2022-10-30 18:13:57 +01:00
package pipelines
import (
"context"
2022-10-31 20:16:26 +01:00
"git.front.kjuulh.io/kjuulh/bust/pkg/builder"
2022-11-06 00:39:03 +01:00
"git.front.kjuulh.io/kjuulh/byg"
2022-10-30 18:13:57 +01:00
"golang.org/x/sync/errgroup"
)
type Pipeline struct {
2022-10-30 18:33:59 +01:00
builder *builder.Builder
2022-10-30 18:13:57 +01:00
pipelines []*byg.Builder
}
2022-10-30 18:33:59 +01:00
func New(builder *builder.Builder) *Pipeline {
2022-10-30 18:13:57 +01:00
return &Pipeline{builder: builder}
}
func (p *Pipeline) WithCustom(custom func(p *Pipeline) *byg.Builder) {
p.add(custom(p))
}
func (p *Pipeline) Execute(ctx context.Context) error {
errgroup, ctx := errgroup.WithContext(ctx)
for _, pipeline := range p.pipelines {
pipeline := pipeline // Allocate for closure
errgroup.Go(func() error {
return pipeline.Execute(ctx)
})
}
if err := errgroup.Wait(); err != nil {
return err
}
return nil
}
func (p *Pipeline) add(pipeline *byg.Builder) {
p.pipelines = append(p.pipelines, pipeline)
}