From fb32d9064353971fd6b4c4e9ec7121005dd29889 Mon Sep 17 00:00:00 2001 From: Jake Heath Date: Thu, 29 Aug 2024 20:55:28 -0700 Subject: [PATCH] feat: refactor to apply a plan --- apply/apply.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/apply/apply.go b/apply/apply.go index 1d95c62a4..53cd60a1c 100644 --- a/apply/apply.go +++ b/apply/apply.go @@ -46,6 +46,51 @@ func Apply(fs afero.Fs, conf *v2.Config, tmpl *templates.T, upgrade bool) error if err != nil { return errs.WrapUser(err, "unable to evaluate plan") } + + return ApplyPlan(plan, WithApplyPlanFs(fs), WithApplyPlanTemplates(tmpl)) +} + +type ApplyPlanContext struct { + fs afero.Fs + tmpl *templates.T +} + +func MakeApplyPlanContext() (*ApplyPlanContext, error) { + pwd, e := os.Getwd() + if e != nil { + return nil, e + } + return &ApplyPlanContext{ + fs: afero.NewBasePathFs(afero.NewOsFs(), pwd), + tmpl: templates.Templates, + }, nil +} + +type ApplyPlanOption func(*ApplyPlanContext) + +func WithApplyPlanFs(fs afero.Fs) ApplyPlanOption { + return func(ctx *ApplyPlanContext) { + ctx.fs = fs + } +} + +func WithApplyPlanTemplates(tmpl *templates.T) ApplyPlanOption { + return func(ctx *ApplyPlanContext) { + ctx.tmpl = tmpl + } +} + +func ApplyPlan(plan *plan.Plan, opts ...ApplyPlanOption) error { + applyCtx, err := MakeApplyPlanContext() + if err != nil { + return err + } + for _, opt := range opts { + opt(applyCtx) + } + + fs := applyCtx.fs + tmpl := applyCtx.tmpl err = applyRepo(fs, plan, tmpl.Repo, tmpl.Common) if err != nil { return errs.WrapUser(err, "unable to apply repo")