Skip to content

Commit

Permalink
Pipeline: add templare support to all arguments in exec op
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 Aug 9, 2024
1 parent 655d43b commit 4b39c45
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pipeline/exec_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func (e *ExecOp) Do(ctx ActionContext) error {
},
} {
if stream.output != nil {
s := ctx.TemplateEngine().RenderLenient(*stream.output, snapshot)
stream.output = &s
out, err := os.OpenFile(*stream.output, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
Expand All @@ -98,7 +100,10 @@ func (e *ExecOp) Do(ctx ActionContext) error {
func (e *ExecOp) CloneWith(ctx ActionContext) Action {
ss := ctx.Snapshot()
return &ExecOp{
Program: ctx.TemplateEngine().RenderLenient(e.Program, ss),
Args: safeRenderStrSlice(e.Args, ctx.TemplateEngine(), ss),
Program: ctx.TemplateEngine().RenderLenient(e.Program, ss),
Args: safeRenderStrSlice(e.Args, ctx.TemplateEngine(), ss),
Stdout: safeRenderStrPointer(e.Stdout, ctx.TemplateEngine(), ss),
Stderr: safeRenderStrPointer(e.Stderr, ctx.TemplateEngine(), ss),
ValidExitCodes: safeCopyIntSlice(e.ValidExitCodes),
}
}
17 changes: 17 additions & 0 deletions pipeline/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ func actionNames(actions ChildActions) string {
return strings.Join(sortActionNames(actions), ",")
}

func safeCopyIntSlice(in *[]int) *[]int {
if in == nil {
return nil
}
r := make([]int, len(*in))
copy(r, *in)
return &r
}

func safeRenderStrPointer(str *string, te TemplateEngine, data map[string]interface{}) *string {
if str == nil {
return nil
}
s := te.RenderLenient(*str, data)
return &s
}

func safeRenderStrSlice(args *[]string, te TemplateEngine, data map[string]interface{}) *[]string {
if args == nil {
return nil
Expand Down
21 changes: 21 additions & 0 deletions pipeline/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,24 @@ func TestSafeRegexpDeref(t *testing.T) {
assert.Equal(t, "", safeRegexpDeref(nil))
assert.Equal(t, "abc", safeRegexpDeref(regexp.MustCompile(`abc`)))
}

func TestSafeRenderStrPointerNil(t *testing.T) {
assert.Nil(t, safeRenderStrPointer(nil, mockEmptyActCtx().TemplateEngine(), nil))
}

func TestSafeRenderStrPointer(t *testing.T) {
s := "{{ .X }}"
d := b.FromMap(map[string]interface{}{
"X": "abc",
})
c := mockActCtx(d)
assert.Equal(t, "abc", *safeRenderStrPointer(&s, c.TemplateEngine(), c.Snapshot()))
}

func TestSafeCopyIntSlice(t *testing.T) {
var x *[]int
assert.Nil(t, safeCopyIntSlice(nil))
a := []int{1, 2, 5}
x = safeCopyIntSlice(&a)
assert.Equal(t, 3, len(*x))
}

0 comments on commit 4b39c45

Please sign in to comment.