Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor to apply a plan #1081

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,51 @@
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")
Expand Down Expand Up @@ -142,7 +187,7 @@
func updateLocalsFromPlan(locals *LocalsTFE, p *plan.Plan) {
// if there is a planned env or account that isn't in the locals, add it
for accountName, account := range p.Accounts {
if _, ok := locals.Locals.Accounts[accountName]; !ok && account.Backend.Kind == plan.BackendKindRemote {

Check failure on line 190 in apply/apply.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

locals.Locals.Accounts[accountName] = MakeTFEWorkspace(p.Global.Common.TerraformVersion)
}
Expand Down
Loading