@@ -43,35 +43,38 @@ type ServeOptions struct {
43
43
Logger slog.Logger
44
44
}
45
45
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
+
46
70
// Serve starts a dRPC server on the provided transport speaking Terraform provisioner.
47
71
func Serve (ctx context.Context , options * ServeOptions ) error {
48
72
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
53
77
} 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 {
75
78
installer := & releases.ExactVersion {
76
79
InstallDir : options .CachePath ,
77
80
Product : product .Terraform ,
0 commit comments