Skip to content

Commit d765172

Browse files
committed
refactor into methods
1 parent 17b1e14 commit d765172

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
@@ -104,18 +104,7 @@ func (e executor) checkMinVersion(ctx context.Context) error {
104104
}
105105

106106
func (e executor) version(ctx context.Context) (*version.Version, error) {
107-
// #nosec
108-
cmd := exec.CommandContext(ctx, e.binaryPath, "version", "-json")
109-
out, err := cmd.Output()
110-
if err != nil {
111-
return nil, err
112-
}
113-
vj := tfjson.VersionOutput{}
114-
err = json.Unmarshal(out, &vj)
115-
if err != nil {
116-
return nil, err
117-
}
118-
return version.NewVersion(vj.Version)
107+
return versionFromBinaryPath(ctx, e.binaryPath)
119108
}
120109

121110
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)