feat(terraform): add all contextual files

This commit is contained in:
Jan De Dobbeleer 2023-12-01 20:48:40 +01:00 committed by Jan De Dobbeleer
parent aed0c4f6d1
commit bcebcb55a2
2 changed files with 70 additions and 37 deletions

View file

@ -39,53 +39,73 @@ type TerraformBlock struct {
func (tf *Terraform) Enabled() bool { func (tf *Terraform) Enabled() bool {
cmd := "terraform" cmd := "terraform"
terraformFolder := filepath.Join(tf.env.Pwd(), ".terraform")
fetchVersion := tf.props.GetBool(properties.FetchVersion, false) fetchVersion := tf.props.GetBool(properties.FetchVersion, false)
if fetchVersion {
// known version files
files := []string{"versions.tf", "main.tf", "terraform.tfstate"}
var hasFiles bool
for _, file := range files {
if tf.env.HasFiles(file) {
hasFiles = true
break
}
}
fetchVersion = hasFiles
}
inContext := tf.env.HasFolder(terraformFolder) || fetchVersion if !tf.env.HasCommand(cmd) || !tf.inContext(fetchVersion) {
if !tf.env.HasCommand(cmd) || !inContext {
return false return false
} }
tf.WorkspaceName, _ = tf.env.RunCommand(cmd, "workspace", "show") tf.WorkspaceName, _ = tf.env.RunCommand(cmd, "workspace", "show")
if !fetchVersion { if !fetchVersion {
return true return true
} }
if err := tf.setVersionFromTfFiles(); err == nil { if err := tf.setVersionFromTfFiles(); err == nil {
return true return true
} }
tf.setVersionFromTfStateFile() tf.setVersionFromTfStateFile()
return true return true
} }
func (tf *Terraform) inContext(fetchVersion bool) bool {
terraformFolder := filepath.Join(tf.env.Pwd(), ".terraform")
if tf.env.HasFolder(terraformFolder) {
return true
}
files := []string{".tf", ".tfplan", ".tfstate"}
for _, file := range files {
if tf.env.HasFiles(file) {
return true
}
}
if !fetchVersion {
return false
}
versionFiles := []string{"versions.tf", "main.tf", "terraform.tfstate"}
for _, file := range versionFiles {
if tf.env.HasFiles(file) {
return true
}
}
return false
}
func (tf *Terraform) setVersionFromTfFiles() error { func (tf *Terraform) setVersionFromTfFiles() error {
files := []string{"versions.tf", "main.tf"} files := []string{"versions.tf", "main.tf"}
for _, file := range files { for _, file := range files {
if !tf.env.HasFiles(file) { if !tf.env.HasFiles(file) {
continue continue
} }
parser := hclparse.NewParser() parser := hclparse.NewParser()
content := tf.env.FileContent(file) content := tf.env.FileContent(file)
hclFile, diags := parser.ParseHCL([]byte(content), file) hclFile, diags := parser.ParseHCL([]byte(content), file)
if diags != nil { if diags != nil {
continue continue
} }
var config TerraFormConfig var config TerraFormConfig
diags = gohcl.DecodeBody(hclFile.Body, nil, &config) diags = gohcl.DecodeBody(hclFile.Body, nil, &config)
if diags != nil { if diags != nil {
continue continue
} }
tf.TerraformBlock = *config.Terraform tf.TerraformBlock = *config.Terraform
return nil return nil
} }
@ -97,6 +117,7 @@ func (tf *Terraform) setVersionFromTfStateFile() {
if !tf.env.HasFiles(file) { if !tf.env.HasFiles(file) {
return return
} }
content := tf.env.FileContent(file) content := tf.env.FileContent(file)
_ = json.Unmarshal([]byte(content), &tf.TerraformBlock) _ = json.Unmarshal([]byte(content), &tf.TerraformBlock)
} }

View file

@ -12,16 +12,17 @@ import (
func TestTerraform(t *testing.T) { func TestTerraform(t *testing.T) {
cases := []struct { cases := []struct {
Case string Case string
Template string Template string
HasTfCommand bool HasTfCommand bool
HasTfFolder bool HasTfFolder bool
HasTfFiles bool HasTfFiles bool
HasTfStateFile bool HasTfVersionFiles bool
FetchVersion bool HasTfStateFile bool
WorkspaceName string FetchVersion bool
ExpectedString string WorkspaceName string
ExpectedEnabled bool ExpectedString string
ExpectedEnabled bool
}{ }{
{ {
Case: "default workspace", Case: "default workspace",
@ -51,17 +52,17 @@ func TestTerraform(t *testing.T) {
FetchVersion: true, FetchVersion: true,
}, },
{ {
Case: "files", Case: "files",
ExpectedString: ">= 1.0.10", ExpectedString: ">= 1.0.10",
ExpectedEnabled: true, ExpectedEnabled: true,
WorkspaceName: "default", WorkspaceName: "default",
Template: "{{ .Version }}", Template: "{{ .Version }}",
HasTfFiles: true, HasTfVersionFiles: true,
HasTfCommand: true, HasTfCommand: true,
FetchVersion: true, FetchVersion: true,
}, },
{ {
Case: "files", Case: "version files",
ExpectedString: "0.12.24", ExpectedString: "0.12.24",
ExpectedEnabled: true, ExpectedEnabled: true,
WorkspaceName: "default", WorkspaceName: "default",
@ -70,6 +71,14 @@ func TestTerraform(t *testing.T) {
HasTfCommand: true, HasTfCommand: true,
FetchVersion: true, FetchVersion: true,
}, },
{
Case: "context files",
ExpectedString: "default",
ExpectedEnabled: true,
WorkspaceName: "default",
HasTfFiles: true,
HasTfCommand: true,
},
} }
for _, tc := range cases { for _, tc := range cases {
@ -77,12 +86,15 @@ func TestTerraform(t *testing.T) {
env.On("HasCommand", "terraform").Return(tc.HasTfCommand) env.On("HasCommand", "terraform").Return(tc.HasTfCommand)
env.On("HasFolder", ".terraform").Return(tc.HasTfFolder) env.On("HasFolder", ".terraform").Return(tc.HasTfFolder)
env.On("HasFiles", ".tf").Return(tc.HasTfFiles)
env.On("HasFiles", ".tfplan").Return(tc.HasTfFiles)
env.On("HasFiles", ".tfstate").Return(tc.HasTfFiles)
env.On("Pwd").Return("") env.On("Pwd").Return("")
env.On("RunCommand", "terraform", []string{"workspace", "show"}).Return(tc.WorkspaceName, nil) env.On("RunCommand", "terraform", []string{"workspace", "show"}).Return(tc.WorkspaceName, nil)
env.On("HasFiles", "versions.tf").Return(tc.HasTfFiles) env.On("HasFiles", "versions.tf").Return(tc.HasTfVersionFiles)
env.On("HasFiles", "main.tf").Return(tc.HasTfFiles) env.On("HasFiles", "main.tf").Return(tc.HasTfVersionFiles)
env.On("HasFiles", "terraform.tfstate").Return(tc.HasTfStateFile) env.On("HasFiles", "terraform.tfstate").Return(tc.HasTfStateFile)
if tc.HasTfFiles { if tc.HasTfVersionFiles {
content, _ := os.ReadFile("../test/versions.tf") content, _ := os.ReadFile("../test/versions.tf")
env.On("FileContent", "versions.tf").Return(string(content)) env.On("FileContent", "versions.tf").Return(string(content))
} }