@@ -2,10 +2,7 @@ package terraform
2
2
3
3
import (
4
4
"context"
5
- "os/exec"
6
5
"path/filepath"
7
- "regexp"
8
- "strings"
9
6
10
7
"github.com/cli/safeexec"
11
8
"github.com/hashicorp/go-version"
@@ -19,8 +16,9 @@ import (
19
16
20
17
// This is the exact version of Terraform used internally
21
18
// when Terraform is missing on the system.
22
- const terraformVersion = "1.1.9"
23
- const versionDelimiter = "."
19
+ var terraformVersion = version .Must (version .NewVersion ("1.1.9" ))
20
+ var minTerraformVersion = version .Must (version .NewVersion ("1.1.0" ))
21
+ var maxTerraformVersion = version .Must (version .NewVersion ("1.2.0" ))
24
22
25
23
var (
26
24
// The minimum version of Terraform supported by the provisioner.
@@ -63,25 +61,10 @@ func Serve(ctx context.Context, options *ServeOptions) error {
63
61
return xerrors .Errorf ("absolute: %w" , err )
64
62
}
65
63
// Checking the installed version of Terraform.
66
- output , err := exec . Command ( absoluteBinary , "version" ). Output ( )
64
+ version , err := versionFromBinaryPath ( ctx , absoluteBinary )
67
65
if err != nil {
68
- return xerrors .Errorf ("terraform version: %w" , err )
69
- }
70
- // The output for `terraform version` is:
71
- // Terraform v1.2.1
72
- // on linux_amd64
73
- versionRegex := regexp .MustCompile ("Terraform v(.+)\n ?.*" )
74
- match := versionRegex .FindStringSubmatch (string (output ))
75
- if match != nil {
76
- // match[0] is the entire string.
77
- // match[1] is the matched substring.
78
- version := match [1 ]
79
- terraformMinorVersion := strings .Join (strings .Split (terraformVersion , versionDelimiter )[:2 ], versionDelimiter )
80
- if ! strings .HasPrefix (version , terraformMinorVersion ) {
81
- downloadTerraform = true
82
- }
83
- } else {
84
- // Download the required Terraform version when unable to determine the existing one.
66
+ downloadTerraform = true
67
+ } else if version .LessThan (minTerraformVersion ) || version .GreaterThanOrEqual (maxTerraformVersion ) {
85
68
downloadTerraform = true
86
69
}
87
70
if ! downloadTerraform {
@@ -92,7 +75,7 @@ func Serve(ctx context.Context, options *ServeOptions) error {
92
75
installer := & releases.ExactVersion {
93
76
InstallDir : options .CachePath ,
94
77
Product : product .Terraform ,
95
- Version : version . Must ( version . NewVersion ( terraformVersion )) ,
78
+ Version : terraformVersion ,
96
79
}
97
80
98
81
execPath , err := installer .Install (ctx )
0 commit comments