Skip to content

Commit

Permalink
Pipeline: Expose reference to current action in ActionContext
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
  • Loading branch information
rkosegi committed Jul 15, 2024
1 parent e054561 commit be339ee
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
6 changes: 4 additions & 2 deletions pipeline/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type actContext struct {
t TemplateEngine
}

func (ac actContext) Action() Action { return ac.c }
func (ac actContext) Data() dom.ContainerBuilder { return ac.d }
func (ac actContext) Factory() dom.ContainerFactory { return ac.f }
func (ac actContext) Executor() Executor { return ac.e }
Expand All @@ -47,8 +48,9 @@ func (ac actContext) Snapshot() map[string]interface{} {
return dom.DefaultNodeMappingFn(ac.Data()).(map[string]interface{})
}

func (p *exec) newCtx() *actContext {
func (p *exec) newCtx(a Action) *actContext {
return &actContext{
c: a,
d: p.gd,
e: p,
f: b,
Expand All @@ -57,7 +59,7 @@ func (p *exec) newCtx() *actContext {
}

func (p *exec) Execute(act Action) (err error) {
ctx := p.newCtx()
ctx := p.newCtx(act)
p.l.OnBefore(ctx)
defer p.l.OnAfter(ctx, err)
err = act.Do(ctx)
Expand Down
2 changes: 2 additions & 0 deletions pipeline/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type ActionContext interface {
Executor() Executor
// TemplateEngine return reference to TemplateEngine
TemplateEngine() TemplateEngine
// Action return reference to actual Action
Action() Action
}

// Action is implemented by actions within ActionSpec
Expand Down
8 changes: 7 additions & 1 deletion pipeline/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ func mockEmptyActCtx() ActionContext {
}

func mockActCtx(data dom.ContainerBuilder) ActionContext {
return New(WithData(data)).(*exec).newCtx()
return New(WithData(data)).(*exec).newCtx(nil)
}

func TestGetActionFromContext(t *testing.T) {
ac := mockEmptyActCtx().(*actContext)
ac.c = &ExportOp{}
assert.NotNil(t, ac.Action())
}

func TestNonEmpty(t *testing.T) {
Expand Down

0 comments on commit be339ee

Please sign in to comment.