func TfDestroy()

in internal/terraform/tf.go [165:203]


func TfDestroy(
	dir string,
	varFiles []string,
	vars []*Vars,
	targets []string,
	verbose bool,
) error {
	var tfDestroyOptions []tfexec.DestroyOption

	// find the binary and setup the client
	ctx := context.Background()

	tf, _ := initializeTerraformClient(dir, verbose)

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

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

	// an edge case, but account for specific targets if they're supplied
	for _, t := range targets {
		tfDestroyOptions = append(tfDestroyOptions, tfexec.Target(t))
	}

	// do what we came here to do
	err := tf.Destroy(ctx, tfDestroyOptions...) // TODO: tf validate before

	if err != nil {
		return err
	}

	return nil
}