commit | 769254b3386cdd6c7d50a688ee1d981811c744b2 | [log] [tgz] |
---|---|---|
author | Lorna Song <lorna@hashicorp.com> | Mon Aug 31 21:45:35 2020 -0400 |
committer | Paul Tyng <paul@paultyng.net> | Tue Sep 01 08:35:14 2020 -0400 |
tree | d1d198e05a71daba6c76a496286cc255a0024aa7 | |
parent | 05e87e9ca6d326df44fae1a04988ca22eacd1b1b [diff] |
Add parsing for workspace already exists error When calling `terraform workspace new <name>` to create a workspace that already exists, an error is returned.
A Go module for constructing and running Terraform CLI commands. Structured return values use the data types defined in terraform-json.
The Terraform Plugin SDK is the canonical Go interface for Terraform plugins using the gRPC protocol. This library is intended for use in Go programs that make use of Terraform's other interface, the CLI. Importing this library is preferable to importing github.com/hashicorp/terraform/command
, because the latter is not intended for use outside Terraform Core.
The Terraform
struct must be initialised with NewTerraform(workingDir, execPath)
.
Top-level Terraform commands each have their own function, which will return either error
or (T, error)
, where T
is a terraform-json
type.
package main import ( "context" "fmt" "io/ioutil" "os" "github.com/hashicorp/terraform-exec/tfexec" "github.com/hashicorp/terraform-exec/tfinstall" ) func main() { tmpDir, err := ioutil.TempDir("", "tfinstall") if err != nil { panic(err) } defer os.RemoveAll(tmpDir) execPath, err := tfinstall.Find(tfinstall.LatestVersion(tmpDir, false)) if err != nil { panic(err) } workingDir := "/path/to/working/dir" tf, err := tfexec.NewTerraform(workingDir, execPath) if err != nil { panic(err) } err = tf.Init(context.Background(), tfexec.Upgrade(true), tfexec.LockTimeout("60s")) if err != nil { panic(err) } state, err := tf.Show(context.Background()) if err != nil { panic(err) } fmt.Println(state.FormatVersion) // "0.1" }