Skip to content

Commit bc75e3d

Browse files
committed
fix: templating fromConfigItem with $()
use structTemplater as that supports {{}} & $()
1 parent b94b3da commit bc75e3d

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

playbook/playbook.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/flanksource/duty/rbac"
1717
"github.com/flanksource/duty/rbac/policy"
1818
"github.com/flanksource/duty/types"
19-
"github.com/flanksource/gomplate/v3"
2019
"github.com/google/uuid"
2120
"github.com/samber/lo"
2221
"github.com/samber/oops"
@@ -206,7 +205,7 @@ func Run(ctx context.Context, playbook *models.Playbook, req RunParams) (*models
206205

207206
var runSpec v1.PlaybookSpec
208207
if err := json.Unmarshal(playbook.Spec, &runSpec); err != nil {
209-
return nil, ctx.Oops().Wrap(err)
208+
return nil, ctx.Oops().Wrapf(err, "failed to unmarshal playbook spec")
210209
}
211210

212211
{
@@ -216,17 +215,15 @@ func Run(ctx context.Context, playbook *models.Playbook, req RunParams) (*models
216215
hasExplicitAgent := len(runSpec.RunsOn) != 0 || len(action.RunsOn) != 0
217216
usesFromConfigItem := action.Exec != nil && action.Exec.Connections.FromConfigItem != nil
218217
if !hasExplicitAgent && usesFromConfigItem {
219-
tpl := gomplate.Template{
220-
Template: *action.Exec.Connections.FromConfigItem,
221-
}
222-
output, err := ctx.RunTemplate(tpl, templateEnv.AsMap(ctx))
218+
templater := ctx.NewStructTemplater(templateEnv.AsMap(ctx), "", nil)
219+
output, err := templater.Template(*action.Exec.Connections.FromConfigItem)
223220
if err != nil {
224-
return nil, ctx.Oops().Wrap(err)
221+
return nil, ctx.Oops().Wrapf(err, "failed to template config item %s", *action.Exec.Connections.FromConfigItem)
225222
}
226223

227224
var fromConfigItem models.ConfigItem
228225
if err := ctx.DB().Select("id", "agent_id").Where("id = ?", output).Find(&fromConfigItem).Error; err != nil {
229-
return nil, ctx.Oops().Wrap(err)
226+
return nil, ctx.Oops().Wrapf(err, "failed to find config item %s", output)
230227
} else if fromConfigItem.AgentID != uuid.Nil {
231228
runSpec.Actions[i].RunsOn = []string{fromConfigItem.AgentID.String()}
232229
}
@@ -236,9 +233,10 @@ func Run(ctx context.Context, playbook *models.Playbook, req RunParams) (*models
236233
// Template run's spec (runsOn)
237234
var runsOn []string
238235
for _, specRunOn := range runSpec.RunsOn {
239-
output, err := ctx.RunTemplate(gomplate.Template{Template: specRunOn}, templateEnv.AsMap(ctx))
236+
templater := ctx.NewStructTemplater(templateEnv.AsMap(ctx), "", nil)
237+
output, err := templater.Template(specRunOn)
240238
if err != nil {
241-
return nil, ctx.Oops().Wrap(err)
239+
return nil, ctx.Oops().Wrapf(err, "failed to template run's spec %s", specRunOn)
242240
}
243241
runsOn = append(runsOn, output)
244242
}

0 commit comments

Comments
 (0)