Skip to content

Commit 0ee78f2

Browse files
committed
refactor into methods
1 parent e020594 commit 0ee78f2

File tree

2 files changed

+29
-37
lines changed

2 files changed

+29
-37
lines changed

provisioner/terraform/executor.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,7 @@ func (e executor) checkMinVersion(ctx context.Context) error {
9898
}
9999

100100
func (e executor) version(ctx context.Context) (*version.Version, error) {
101-
// #nosec
102-
cmd := exec.CommandContext(ctx, e.binaryPath, "version", "-json")
103-
out, err := cmd.Output()
104-
if err != nil {
105-
return nil, err
106-
}
107-
vj := tfjson.VersionOutput{}
108-
err = json.Unmarshal(out, &vj)
109-
if err != nil {
110-
return nil, err
111-
}
112-
return version.NewVersion(vj.Version)
101+
return versionFromBinaryPath(ctx, e.binaryPath)
113102
}
114103

115104
func versionFromBinaryPath(ctx context.Context, binaryPath string) (*version.Version, error) {

provisioner/terraform/serve.go

+28-25
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,38 @@ type ServeOptions struct {
4343
Logger slog.Logger
4444
}
4545

46+
func getAbsoluteBinaryPath(ctx context.Context) (string, bool) {
47+
binaryPath, err := safeexec.LookPath("terraform")
48+
if err != nil {
49+
return "", false
50+
}
51+
// If the "coder" binary is in the same directory as
52+
// the "terraform" binary, "terraform" is returned.
53+
//
54+
// We must resolve the absolute path for other processes
55+
// to execute this properly!
56+
absoluteBinary, err := filepath.Abs(binaryPath)
57+
if err != nil {
58+
return "", false
59+
}
60+
// Checking the installed version of Terraform.
61+
version, err := versionFromBinaryPath(ctx, absoluteBinary)
62+
if err != nil {
63+
return "", false
64+
} else if version.LessThan(minTerraformVersion) || version.GreaterThanOrEqual(maxTerraformVersion) {
65+
return "", false
66+
}
67+
return absoluteBinary, true
68+
}
69+
4670
// Serve starts a dRPC server on the provided transport speaking Terraform provisioner.
4771
func Serve(ctx context.Context, options *ServeOptions) error {
4872
if options.BinaryPath == "" {
49-
binaryPath, err := safeexec.LookPath("terraform")
50-
var downloadTerraform bool
51-
if err != nil {
52-
downloadTerraform = true
73+
absoluteBinary, ok := getAbsoluteBinaryPath(ctx)
74+
75+
if ok {
76+
options.BinaryPath = absoluteBinary
5377
} else {
54-
// If the "coder" binary is in the same directory as
55-
// the "terraform" binary, "terraform" is returned.
56-
//
57-
// We must resolve the absolute path for other processes
58-
// to execute this properly!
59-
absoluteBinary, err := filepath.Abs(binaryPath)
60-
if err != nil {
61-
return xerrors.Errorf("absolute: %w", err)
62-
}
63-
// Checking the installed version of Terraform.
64-
version, err := versionFromBinaryPath(ctx, absoluteBinary)
65-
if err != nil {
66-
downloadTerraform = true
67-
} else if version.LessThan(minTerraformVersion) || version.GreaterThanOrEqual(maxTerraformVersion) {
68-
downloadTerraform = true
69-
}
70-
if !downloadTerraform {
71-
options.BinaryPath = absoluteBinary
72-
}
73-
}
74-
if downloadTerraform {
7578
installer := &releases.ExactVersion{
7679
InstallDir: options.CachePath,
7780
Product: product.Terraform,

0 commit comments

Comments
 (0)