From 81bf4841b8325de3490c9e3fddd8619e8d035669 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sat, 29 Oct 2022 21:00:43 +0200 Subject: [PATCH] add byg --- byg.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ examples/test.go | 34 ++++++++++++++++++++++++ go.mod | 5 ++++ go.sum | 2 ++ 4 files changed, 110 insertions(+) create mode 100644 byg.go create mode 100644 examples/test.go create mode 100644 go.mod create mode 100644 go.sum diff --git a/byg.go b/byg.go new file mode 100644 index 0000000..d13405c --- /dev/null +++ b/byg.go @@ -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 +} diff --git a/examples/test.go b/examples/test.go new file mode 100644 index 0000000..45f1461 --- /dev/null +++ b/examples/test.go @@ -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()) + +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e76267a --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.front.kjuulh.io/kjuulh/byg + +go 1.19 + +require golang.org/x/sync v0.1.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..595d314 --- /dev/null +++ b/go.sum @@ -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=