diff --git a/tfexec/metadata_functions.go b/tfexec/metadata_functions.go new file mode 100644 index 00000000..eee7809f --- /dev/null +++ b/tfexec/metadata_functions.go @@ -0,0 +1,39 @@ +package tfexec + +import ( + "context" + "fmt" + "os/exec" + + tfjson "github.com/hashicorp/terraform-json" +) + +// MetadataFunctions represents the terraform metadata functions -json subcommand. +func (tf *Terraform) MetadataFunctions(ctx context.Context) (*tfjson.MetadataFunctions, error) { + err := tf.compatible(ctx, tf1_4_0, nil) + if err != nil { + return nil, fmt.Errorf("terraform metadata functions was added in 1.4.0: %w", err) + } + + functionsCmd := tf.metadataFunctionsCmd(ctx) + + var ret tfjson.MetadataFunctions + err = tf.runTerraformCmdJSON(ctx, functionsCmd, &ret) + if err != nil { + return nil, err + } + + err = ret.Validate() + if err != nil { + return nil, err + } + + return &ret, nil +} + +func (tf *Terraform) metadataFunctionsCmd(ctx context.Context, args ...string) *exec.Cmd { + allArgs := []string{"metadata", "functions", "-json"} + allArgs = append(allArgs, args...) + + return tf.buildTerraformCmd(ctx, nil, allArgs...) +} diff --git a/tfexec/version.go b/tfexec/version.go index 37825b53..a2e97368 100644 --- a/tfexec/version.go +++ b/tfexec/version.go @@ -27,6 +27,7 @@ var ( tf0_15_2 = version.Must(version.NewVersion("0.15.2")) tf0_15_3 = version.Must(version.NewVersion("0.15.3")) tf1_1_0 = version.Must(version.NewVersion("1.1.0")) + tf1_4_0 = version.Must(version.NewVersion("1.4.0")) ) // Version returns structured output from the terraform version command including both the Terraform CLI version