bust/pkg/pipelines/pipeline.go

45 lines
844 B
Go
Raw Normal View History

2022-10-30 18:13:57 +01:00
package pipelines
import (
"context"
"git.front.kjuulh.io/kjuulh/byg"
2022-10-30 18:33:59 +01:00
"git.front.kjuulh.io/kjuulh/dagger-go/pkg/builder"
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)
}