func TfPlan()

in internal/terraform/tf.go [50:121]


func TfPlan(
	dir string,
	varFiles []string,
	vars []Vars,
	verbose bool,
) PlanResult {
	var tfPlanOptions []tfexec.PlanOption
	var result PlanResult

	// Find the binary and setup the client
	ctx := context.Background()
	p, err := findBinary()

	if err != nil {
		result.Err = err
		return result
	}

	tf, err := buildClient(dir, p, verbose)

	if err != nil {
		result.Err = err
		return result
	}

	// Create the plan file coordinates
	tmpDir, err := os.MkdirTemp(dir, "pastures")
	if err != nil {
		result.Err = err
		return result
	}

	defer os.RemoveAll(tmpDir)

	planPath := fmt.Sprintf(
		"%s/%s-%v",
		tmpDir,
		"pastureplan",
		time.Now().Unix(),
	)

	// Set the plan out target
	tfPlanOptions = append(tfPlanOptions, tfexec.Out(planPath))

	// include var files if they're provided
	for _, v := range varFiles {
		tfPlanOptions = append(tfPlanOptions, tfexec.VarFile(v))
	}

	// load up vars if they're supplied
	for _, v := range vars {
		assignment := v.Key + "=" + v.Value
		tfPlanOptions = append(tfPlanOptions, tfexec.Var(assignment))
	}

	// Run the plan
	_, err = tf.Plan(ctx, tfPlanOptions...) // TODO: tf validate before
	if err != nil {
		result.Err = err
		return result
	}

	// Return the plan, or an error
	plan, err := tf.ShowPlanFileRaw(ctx, planPath)
	if err != nil {
		result.Err = err
		return result
	}

	result.Plan = plan
	return result
}