This commit is contained in:
Kasper Juul Hermansen 2022-10-29 21:00:43 +02:00
commit 81bf4841b8
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
4 changed files with 110 additions and 0 deletions

69
byg.go Normal file
View File

@ -0,0 +1,69 @@
package byg
import (
"context"
"sync"
"golang.org/x/sync/errgroup"
)
type buildStep struct {
name string
tasks []StepExecuteFunc
}
type Builder struct {
steps []*buildStep
addmu sync.RWMutex
}
func New() *Builder {
return &Builder{
steps: make([]*buildStep, 0),
}
}
type Context struct{}
type StepExecuteFunc func(ctx Context) error
type Step struct {
Execute StepExecuteFunc
}
func (bb *Builder) Step(name string, steps ...Step) *Builder {
bb.addmu.Lock()
defer bb.addmu.Unlock()
bs := &buildStep{
name: name,
tasks: make([]StepExecuteFunc, 0),
}
for _, st := range steps {
bs.tasks = append(bs.tasks, st.Execute)
}
bb.steps = append(bb.steps, bs)
return bb
}
func (bb *Builder) Execute(ctx context.Context) error {
bb.addmu.Lock()
defer bb.addmu.Unlock()
for _, step := range bb.steps {
errgroup, _ := errgroup.WithContext(ctx)
for _, task := range step.tasks {
errgroup.Go(func() error {
return task(Context{})
})
}
if err := errgroup.Wait(); err != nil {
return err
}
}
return nil
}

34
examples/test.go Normal file
View File

@ -0,0 +1,34 @@
package examples
import (
"context"
"testing"
"git.front.kjuulh.io/kjuulh/byg"
)
func Test(t *testing.T) {
byg.New().
Step(
"fetch resources",
byg.Step{
Execute: func(ctx byg.Context) error {
return nil
},
},
byg.Step{
Execute: func(ctx byg.Context) error {
return nil
},
},
).
Step(
"build stuff",
byg.Step{
Execute: func(ctx byg.Context) error {
return nil
},
},
).Execute(context.Background())
}

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module git.front.kjuulh.io/kjuulh/byg
go 1.19
require golang.org/x/sync v0.1.0

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=