This repository has been archived by the owner on Apr 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
54 lines (47 loc) · 1.48 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package tftest
import (
"context"
"fmt"
"io/ioutil"
"os"
"github.com/hashicorp/terraform-exec/tfinstall"
)
// Config is used to configure the test helper. In most normal test programs
// the configuration is discovered automatically by an Init* function using
// DiscoverConfig, but this is exposed so that more complex scenarios can be
// implemented by direct configuration.
type Config struct {
SourceDir string
TerraformExec string
execTempDir string
PreviousPluginExec string
}
// DiscoverConfig uses environment variables and other means to automatically
// discover a reasonable test helper configuration.
func DiscoverConfig(sourceDir string) (*Config, error) {
tfVersion := os.Getenv("TF_ACC_TERRAFORM_VERSION")
tfPath := os.Getenv("TF_ACC_TERRAFORM_PATH")
tempDir := os.Getenv("TF_ACC_TEMP_DIR")
tfDir, err := ioutil.TempDir(tempDir, "tftest-terraform")
if err != nil {
return nil, fmt.Errorf("failed to create temp dir: %w", err)
}
finders := []tfinstall.ExecPathFinder{}
switch {
case tfPath != "":
finders = append(finders, tfinstall.ExactPath(tfPath))
case tfVersion != "":
finders = append(finders, tfinstall.ExactVersion(tfVersion, tfDir))
default:
finders = append(finders, tfinstall.LookPath(), tfinstall.LatestVersion(tfDir, true))
}
tfExec, err := tfinstall.Find(context.Background(), finders...)
if err != nil {
return nil, err
}
return &Config{
SourceDir: sourceDir,
TerraformExec: tfExec,
execTempDir: tfDir,
}, nil
}