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

Commit 0672a10

Browse files
committed
pkg/terraform: use JSON output while checking version
As it is more idiomatic than using Sscanf. '-json' parameter is a new option added in Terraform 0.13. Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
1 parent 6c0e9a8 commit 0672a10

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

pkg/terraform/executor.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -518,25 +518,22 @@ func (ex *Executor) logPath(id int) string {
518518
}
519519

520520
func (ex *Executor) checkVersion() error {
521-
vOutput, err := ex.executeSync("--version")
521+
versionOutputRaw, err := ex.executeSync("version", "-json")
522522
if err != nil {
523-
return fmt.Errorf("executing 'terraform --version': %w", err)
523+
return fmt.Errorf("executing 'terraform version -json': %w", err)
524524
}
525525

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

533-
if n != 1 {
534-
return fmt.Errorf("version not found in 'terraform --version' output")
530+
if err := json.Unmarshal(versionOutputRaw, &versionOutput); err != nil {
531+
return fmt.Errorf("unmarshaling version command output %q: %w", string(versionOutputRaw), err)
535532
}
536533

537-
v, err := version.NewVersion(vStr)
534+
v, err := version.NewVersion(versionOutput.TerraformVersion)
538535
if err != nil {
539-
return fmt.Errorf("parsing Terraform version %q: %w", vStr, err)
536+
return fmt.Errorf("parsing Terraform version %q: %w", versionOutput.TerraformVersion, err)
540537
}
541538

542539
// requiredVersion is const, so we test it in unit tests.

pkg/terraform/executor_test.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,50 @@ func TestVersionConstraint(t *testing.T) {
3131
expectError bool
3232
}{
3333
"valid": {
34-
output: "Terraform v0.13.10",
34+
output: `{
35+
"terraform_version": "0.13.10",
36+
"terraform_revision": "",
37+
"provider_selections": {},
38+
"terraform_outdated": false
39+
}
40+
`,
3541
},
3642
"outdated": {
37-
output: "Terraform v0.11.0",
43+
output: `{
44+
"terraform_version": "0.11.0",
45+
"terraform_revision": "",
46+
"provider_selections": {},
47+
"terraform_outdated": false
48+
}
49+
`,
3850
expectError: true,
3951
},
4052
"unsupported": {
41-
output: "Terraform v0.14.5",
53+
output: `{
54+
"terraform_version": "0.14.5",
55+
"terraform_revision": "",
56+
"provider_selections": {},
57+
"terraform_outdated": false
58+
}
59+
`,
4260
expectError: true,
4361
},
44-
"with extra test": {
62+
"not JSON": {
4563
output: `Terraform v0.13.11
4664
4765
Your version of Terraform is out of date! The latest version
4866
is 0.13.3. You can update by downloading from https://www.terraform.io/downloads.html`,
67+
expectError: true,
68+
},
69+
"empty version field": {
70+
output: `{
71+
"terraform_version": "",
72+
"terraform_revision": "",
73+
"provider_selections": {},
74+
"terraform_outdated": false
75+
}
76+
`,
77+
expectError: true,
4978
},
5079
}
5180

0 commit comments

Comments
 (0)