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

推荐答案

我认为以下措施应该会奏效:

    targets := []tfexec.ApplyOption{
        tfexec.Target("module.example"),
        tfexec.Target("module.another_example"),
    }

    // Apply the Terraform configuration with the defined targets
    err = tf.Apply(context.Background(), targets...)
    if err != nil {
        panic(err)
    }

Go相关问答推荐

Go Fiber和HTMX—HX—Trigger header被更改为HX—Trigger,这不是HTMX监听的内容

错误.如果它包含切片,则返回FALSE

如何在S汇编器中更高效地将全局数据加载到霓虹灯寄存器?

如何在链接中写入链接

Golang Gorm Fiber - 如何将定义为别名的名称发送到索引模板?

运行 Docker 容器 - X11 无法打开显示:0

golang / urfave.cli:无法手动设置标志

GoLang: gocui 边框 colored颜色

判断不同 go map 类型中的重复键

如何使用带有方法的字符串枚举作为通用参数?

获取切片元素的地址是否意味着 Go 中元素的副本?

Golang 构建多平台问题

自定义指标未显示在 prometheus web ui 中,grafana 中也是如此

如何在 Windows 中使用 github.com/AllenDang/giu 和 github.com/gordonklaus/portaudio 构建 GO 程序

如何在循环中旋转图像以便在 golang 中创建 GIF?

手动将 OpenTelemetry 上下文从 golang 提取到字符串中?

行之间的模板交替设计

如何扩充 ResponseWriter 的 Header() 返回的 map

即使没有竞争条件也没有得到任何输出

如何访问Go 1.18泛型 struct (structs)中的共享字段