Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
pkg/terraform: use JSON output while checking version
Browse files Browse the repository at this point in the history
As it is more idiomatic than using Sscanf. '-json' parameter is a new
option added in Terraform 0.13.

Signed-off-by: Mateusz Gozdek <[email protected]>
  • Loading branch information
invidian committed Oct 5, 2020
1 parent 0f837a3 commit 935b0b8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
21 changes: 9 additions & 12 deletions pkg/terraform/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,25 +518,22 @@ func (ex *Executor) logPath(id int) string {
}

func (ex *Executor) checkVersion() error {
vOutput, err := ex.executeSync("--version")
versionOutputRaw, err := ex.executeSync("--version", "-json")
if err != nil {
return fmt.Errorf("executing 'terraform --version': %w", err)
return fmt.Errorf("executing Terraform command: %w", err)
}

format := "Terraform v%s\n"
var vStr string
n, err := fmt.Sscanf(string(vOutput), format, &vStr)
if err != nil {
return fmt.Errorf("output %q does not match format %q: %w", string(vOutput), format, err)
}
versionOutput := struct {
TerraformVersion string `json:"terraform_version"`
}{}

if n != 1 {
return fmt.Errorf("error parsing Terraform version")
if err := json.Unmarshal(versionOutputRaw, &versionOutput); err != nil {
return fmt.Errorf("unmarshaling version output %q: %w", string(versionOutputRaw), err)
}

v, err := version.NewVersion(vStr)
v, err := version.NewVersion(versionOutput.TerraformVersion)
if err != nil {
return fmt.Errorf("initializing version object from %q: %w", vStr, err)
return fmt.Errorf("initializing version object from %q: %w", versionOutput.TerraformVersion, err)
}

// requiredVersion is const, so we test it in unit tests.
Expand Down
37 changes: 33 additions & 4 deletions pkg/terraform/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,50 @@ func TestVersionConstraint(t *testing.T) {
expectError bool
}{
"valid": {
output: "Terraform v0.13.10",
output: `{
"terraform_version": "0.13.10",
"terraform_revision": "",
"provider_selections": {},
"terraform_outdated": false
}
`,
},
"outdated": {
output: "Terraform v0.11.0",
output: `{
"terraform_version": "0.11.0",
"terraform_revision": "",
"provider_selections": {},
"terraform_outdated": false
}
`,
expectError: true,
},
"unsupported": {
output: "Terraform v0.14.5",
output: `{
"terraform_version": "0.14.5",
"terraform_revision": "",
"provider_selections": {},
"terraform_outdated": false
}
`,
expectError: true,
},
"with extra test": {
"not JSON": {
output: `Terraform v0.13.11
Your version of Terraform is out of date! The latest version
is 0.13.3. You can update by downloading from https://www.terraform.io/downloads.html`,
expectError: true,
},
"empty version field": {
output: `{
"terraform_version": "",
"terraform_revision": "",
"provider_selections": {},
"terraform_outdated": false
}
`,
expectError: true,
},
}

Expand Down

0 comments on commit 935b0b8

Please sign in to comment.