Skip to content

Commit

Permalink
Merge pull request #25 from ccremer/hooks
Browse files Browse the repository at this point in the history
Change pipeline hook type to plain func instead of interface
  • Loading branch information
ccremer authored Dec 27, 2021
2 parents 9c041d1 + c3c1f1a commit f068907
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
10 changes: 3 additions & 7 deletions examples/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@ import (
pipeline "github.com/ccremer/go-command-pipeline"
)

type PipelineLogger struct{}

func (l *PipelineLogger) Accept(step pipeline.Step) {
fmt.Println(fmt.Sprintf("Executing step: %s", step.Name))
}

func TestExample_Hooks(t *testing.T) {
p := pipeline.NewPipeline()
p.AddBeforeHook(&PipelineLogger{})
p.AddBeforeHook(func(step pipeline.Step) {
fmt.Println(fmt.Sprintf("Executing step: %s", step.Name))
})
p.WithSteps(
pipeline.NewStep("hook demo", AfterHookAction()),
)
Expand Down
11 changes: 4 additions & 7 deletions pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@ type (
}
// Context contains arbitrary data relevant for the pipeline execution.
Context interface{}
// Listener is a simple interface that listens to Pipeline events.
Listener interface {
// Accept takes the given Step.
Accept(step Step)
}
// Listener is a simple func that listens to Pipeline events.
Listener func(step Step)
// ActionFunc is the func that contains your business logic.
// The context is a user-defined arbitrary data of type interface{} that gets provided in every Step, but may be nil if not set.
ActionFunc func(ctx Context) Result
Expand Down Expand Up @@ -132,8 +129,8 @@ func (p *Pipeline) Run() Result {

func (p *Pipeline) doRun() Result {
for _, step := range p.steps {
for _, listener := range p.beforeHooks {
listener.Accept(step)
for _, hooks := range p.beforeHooks {
hooks(step)
}

result := step.F(p.context)
Expand Down
2 changes: 1 addition & 1 deletion pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestPipeline_Run(t *testing.T) {
return Result{}
}),
},
givenBeforeHook: hook,
givenBeforeHook: hook.Accept,
expectedCalls: 2,
},
"GivenPipelineWithFinalizer_WhenRunning_ThenCallHandler": {
Expand Down

0 comments on commit f068907

Please sign in to comment.