Skip to content

Commit

Permalink
feat(terraform): add all contextual files
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Dec 1, 2023
1 parent aed0c4f commit bcebcb5
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 36 deletions.
51 changes: 36 additions & 15 deletions src/segments/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,53 +39,73 @@ type TerraformBlock struct {

func (tf *Terraform) Enabled() bool {
cmd := "terraform"
terraformFolder := filepath.Join(tf.env.Pwd(), ".terraform")
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) || !inContext {
if !tf.env.HasCommand(cmd) || !tf.inContext(fetchVersion) {
return false
}

tf.WorkspaceName, _ = tf.env.RunCommand(cmd, "workspace", "show")
if !fetchVersion {
return true
}

if err := tf.setVersionFromTfFiles(); err == nil {
return true
}

tf.setVersionFromTfStateFile()
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 {
files := []string{"versions.tf", "main.tf"}
for _, file := range files {
if !tf.env.HasFiles(file) {
continue
}

parser := hclparse.NewParser()
content := tf.env.FileContent(file)
hclFile, diags := parser.ParseHCL([]byte(content), file)
if diags != nil {
continue
}

var config TerraFormConfig
diags = gohcl.DecodeBody(hclFile.Body, nil, &config)
if diags != nil {
continue
}

tf.TerraformBlock = *config.Terraform
return nil
}
Expand All @@ -97,6 +117,7 @@ func (tf *Terraform) setVersionFromTfStateFile() {
if !tf.env.HasFiles(file) {
return
}

content := tf.env.FileContent(file)
_ = json.Unmarshal([]byte(content), &tf.TerraformBlock)
}
54 changes: 33 additions & 21 deletions src/segments/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ import (

func TestTerraform(t *testing.T) {
cases := []struct {
Case string
Template string
HasTfCommand bool
HasTfFolder bool
HasTfFiles bool
HasTfStateFile bool
FetchVersion bool
WorkspaceName string
ExpectedString string
ExpectedEnabled bool
Case string
Template string
HasTfCommand bool
HasTfFolder bool
HasTfFiles bool
HasTfVersionFiles bool
HasTfStateFile bool
FetchVersion bool
WorkspaceName string
ExpectedString string
ExpectedEnabled bool
}{
{
Case: "default workspace",
Expand Down Expand Up @@ -51,24 +52,32 @@ func TestTerraform(t *testing.T) {
FetchVersion: true,
},
{
Case: "files",
ExpectedString: ">= 1.0.10",
Case: "files",
ExpectedString: ">= 1.0.10",
ExpectedEnabled: true,
WorkspaceName: "default",
Template: "{{ .Version }}",
HasTfVersionFiles: true,
HasTfCommand: true,
FetchVersion: true,
},
{
Case: "version files",
ExpectedString: "0.12.24",
ExpectedEnabled: true,
WorkspaceName: "default",
Template: "{{ .Version }}",
HasTfFiles: true,
HasTfStateFile: true,
HasTfCommand: true,
FetchVersion: true,
},
{
Case: "files",
ExpectedString: "0.12.24",
Case: "context files",
ExpectedString: "default",
ExpectedEnabled: true,
WorkspaceName: "default",
Template: "{{ .Version }}",
HasTfStateFile: true,
HasTfFiles: true,
HasTfCommand: true,
FetchVersion: true,
},
}

Expand All @@ -77,12 +86,15 @@ func TestTerraform(t *testing.T) {

env.On("HasCommand", "terraform").Return(tc.HasTfCommand)
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("RunCommand", "terraform", []string{"workspace", "show"}).Return(tc.WorkspaceName, nil)
env.On("HasFiles", "versions.tf").Return(tc.HasTfFiles)
env.On("HasFiles", "main.tf").Return(tc.HasTfFiles)
env.On("HasFiles", "versions.tf").Return(tc.HasTfVersionFiles)
env.On("HasFiles", "main.tf").Return(tc.HasTfVersionFiles)
env.On("HasFiles", "terraform.tfstate").Return(tc.HasTfStateFile)
if tc.HasTfFiles {
if tc.HasTfVersionFiles {
content, _ := os.ReadFile("../test/versions.tf")
env.On("FileContent", "versions.tf").Return(string(content))
}
Expand Down

0 comments on commit bcebcb5

Please sign in to comment.