I am trying to add a target to terraform apply command using golang sdk for terraform in hashicorp/terraform-exec
Ideally, the equivalent command for CLI is terraform apply --auto-approve --target 'module.example'
But I am getting the following error when I pass the targets
in ApplyOptions{}
to the Apply()
function.
Can anybody point me what I am doing wring here?
package main
import (
"context"
"github.com/hashicorp/terraform-exec/tfexec"
)
func main() {
// Create a new tfexec.Executor instance
tf, err := tfexec.NewTerraform("/path/to/terraform/binary")
if err != nil {
panic(err)
}
err = tf.Init(context.Background(), tfexec.Upgrade(true))
if err != nil {
panic(err)
}
// Define the targets you want to apply
targets := []string{"module.example", "module.another_example"}
// Create an ApplyOption with the targets
applyOption := tfexec.ApplyOption{
Targets: targets,
}
// Apply the Terraform configuration with the defined targets
err = tf.Apply(context.Background(), applyOption)
if err != nil {
panic(err)
}
}
错误显示为,invalid composite literal type tfexec.ApplyOptioncompiler
go run test.go # command-line-arguments ./test.go:23:17: invalid composite literal type tfexec.ApplyOption