Skip to content

Commit 91fd183

Browse files
committed
use the new exectuor version method
1 parent 33f533a commit 91fd183

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

provisioner/terraform/executor.go

+15
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ func (e executor) version(ctx context.Context) (*version.Version, error) {
112112
return version.NewVersion(vj.Version)
113113
}
114114

115+
func versionFromBinaryPath(ctx context.Context, binaryPath string) (*version.Version, error) {
116+
// #nosec
117+
cmd := exec.CommandContext(ctx, binaryPath, "version", "-json")
118+
out, err := cmd.Output()
119+
if err != nil {
120+
return nil, err
121+
}
122+
vj := tfjson.VersionOutput{}
123+
err = json.Unmarshal(out, &vj)
124+
if err != nil {
125+
return nil, err
126+
}
127+
return version.NewVersion(vj.Version)
128+
}
129+
115130
func (e executor) init(ctx context.Context, logr logger) error {
116131
outWriter, doneOut := logWriter(logr, proto.LogLevel_DEBUG)
117132
errWriter, doneErr := logWriter(logr, proto.LogLevel_ERROR)

provisioner/terraform/serve.go

+7-24
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ package terraform
22

33
import (
44
"context"
5-
"os/exec"
65
"path/filepath"
7-
"regexp"
8-
"strings"
96

107
"github.com/cli/safeexec"
118
"github.com/hashicorp/go-version"
@@ -19,8 +16,9 @@ import (
1916

2017
// This is the exact version of Terraform used internally
2118
// 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"))
2422

2523
var (
2624
// The minimum version of Terraform supported by the provisioner.
@@ -63,25 +61,10 @@ func Serve(ctx context.Context, options *ServeOptions) error {
6361
return xerrors.Errorf("absolute: %w", err)
6462
}
6563
// Checking the installed version of Terraform.
66-
output, err := exec.Command(absoluteBinary, "version").Output()
64+
version, err := versionFromBinaryPath(ctx, absoluteBinary)
6765
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) {
8568
downloadTerraform = true
8669
}
8770
if !downloadTerraform {
@@ -92,7 +75,7 @@ func Serve(ctx context.Context, options *ServeOptions) error {
9275
installer := &releases.ExactVersion{
9376
InstallDir: options.CachePath,
9477
Product: product.Terraform,
95-
Version: version.Must(version.NewVersion(terraformVersion)),
78+
Version: terraformVersion,
9679
}
9780

9881
execPath, err := installer.Install(ctx)

0 commit comments

Comments
 (0)