Skip to content

Commit

Permalink
Merge pull request #21 from env0/fix-tf12-local-submodules-tag
Browse files Browse the repository at this point in the history
Fix: TF12 local submodules tagging
  • Loading branch information
yaronya authored May 21, 2020
2 parents 0c55681 + 2f94d95 commit ef91ca6
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 18 deletions.
23 changes: 6 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"encoding/json"
"github.com/bmatcuk/doublestar"
. "github.com/env0/terratag/cli"
"github.com/env0/terratag/convert"
. "github.com/env0/terratag/errors"
Expand All @@ -12,36 +11,26 @@ import (
. "github.com/env0/terratag/terraform"
. "github.com/env0/terratag/tfschema"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/thoas/go-funk"
"github.com/zclconf/go-cty/cty"
"log"
"path/filepath"
"strings"
)

func main() {
tags, dir, isSkipTerratagFiles, isMissingArg := InitArgs()

tfVersion := GetTeraformVersion()
tfVersion := GetTerraformVersion()

if isMissingArg {
if isMissingArg || !IsTerraformInitRun(dir) {
return
}

tagDirectoryResources(dir, tags, isSkipTerratagFiles, tfVersion)
}

func tagDirectoryResources(dir string, tags string, isSkipTerratagFiles bool, tfVersion int) {
matches, err := doublestar.Glob(dir + "/**/*.tf")
PanicOnError(err, nil)
matches := GetTerraformFilePaths(dir)

for i, match := range matches {
resolvedMatch, err := filepath.EvalSymlinks(match)
matches[i] = resolvedMatch
PanicOnError(err, nil)
}
matches = funk.UniqString(matches)
tagDirectoryResources(dir, matches, tags, isSkipTerratagFiles, tfVersion)
}

func tagDirectoryResources(dir string, matches []string, tags string, isSkipTerratagFiles bool, tfVersion int) {
for _, path := range matches {
if isSkipTerratagFiles && strings.HasSuffix(path, "terratag.tf") {
log.Print("Skipping file ", path, " as it's already tagged")
Expand Down
87 changes: 86 additions & 1 deletion terraform/terraform.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package terraform

import (
"encoding/json"
"github.com/bmatcuk/doublestar"
"github.com/env0/terratag/errors"
"github.com/thoas/go-funk"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)

func GetTeraformVersion() int {
func GetTerraformVersion() int {
output, err := exec.Command("terraform", "version").Output()
outputAsString := strings.TrimSpace(string(output))
errors.PanicOnError(err, &outputAsString)
Expand All @@ -21,3 +27,82 @@ func GetTeraformVersion() int {
log.Fatalln("Terratag only supports Terraform 0.11.x and 0.12.x - your version says ", outputAsString)
return -1
}

func IsTerraformInitRun(dir string) bool {
_, err := os.Stat(dir + "/.terraform")

if err != nil {
if os.IsNotExist(err) {
log.Fatalln("terraform init must run before running terratag")
return false
}

message := "couldn't determine if terraform init has run"
errors.PanicOnError(err, &message)
}

return true
}

func GetTerraformFilePaths(rootDir string) []string {
const tfFileMatcher = "/**/*.tf"

tfFiles, err := doublestar.Glob(rootDir + tfFileMatcher)
errors.PanicOnError(err, nil)

modulesDirs := getTerraformModulesDirPaths(rootDir)

for _, moduleDir := range modulesDirs {
matches, err := doublestar.Glob(moduleDir + tfFileMatcher)
errors.PanicOnError(err, nil)

tfFiles = append(tfFiles, matches...)
}

for i, tfFile := range tfFiles {
resolvedTfFile, err := filepath.EvalSymlinks(tfFile)
errors.PanicOnError(err, nil)

tfFiles[i] = resolvedTfFile
}

return funk.UniqString(tfFiles)
}

func getTerraformModulesDirPaths(dir string) []string {
var paths []string
var modulesJson ModulesJson

jsonFile, err := os.Open(dir + "/.terraform/modules/modules.json")

if os.IsNotExist(err) {
return paths
}
errors.PanicOnError(err, nil)

byteValue, _ := ioutil.ReadAll(jsonFile)
err = json.Unmarshal(byteValue, &modulesJson)
errors.PanicOnError(err, nil)

for _, module := range modulesJson.Modules {
modulePath, err := filepath.EvalSymlinks(dir + "/" + module.Dir)
errors.PanicOnError(err, nil)

paths = append(paths, modulePath)
}

err = jsonFile.Close()
errors.PanicOnError(err, nil)

return paths
}

type ModulesJson struct {
Modules []ModuleMetadata `json:"Modules"`
}

type ModuleMetadata struct {
Key string `json:"Key"`
Source string `json:"Source"`
Dir string `json:"Dir"`
}

0 comments on commit ef91ca6

Please sign in to comment.