github.com/diggerhq/digger/libs@v0.0.0-20240604170430-9d61cdf01cc5/digger_config/terragrunt/atlantis/parse_tf.go (about)

     1  package atlantis
     2  
     3  import (
     4  	"errors"
     5  	"github.com/gruntwork-io/terragrunt/util"
     6  	"github.com/hashicorp/terraform-config-inspect/tfconfig"
     7  	"strings"
     8  )
     9  
    10  var localModuleSourcePrefixes = []string{
    11  	"./",
    12  	"../",
    13  	".\\",
    14  	"..\\",
    15  }
    16  
    17  func parseTerraformLocalModuleSource(path string) ([]string, error) {
    18  	module, diags := tfconfig.LoadModule(path)
    19  	// modules, diags := parser.loadConfigDir(path)
    20  	if diags.HasErrors() {
    21  		return nil, errors.New(diags.Error())
    22  	}
    23  
    24  	var sourceMap = map[string]bool{}
    25  	for _, mc := range module.ModuleCalls {
    26  		if isLocalTerraformModuleSource(mc.Source) {
    27  			modulePath := util.JoinPath(path, mc.Source)
    28  			modulePathGlob := util.JoinPath(modulePath, "*.tf*")
    29  
    30  			if _, exists := sourceMap[modulePathGlob]; exists {
    31  				continue
    32  			}
    33  			sourceMap[modulePathGlob] = true
    34  
    35  			// find local module source recursively
    36  			subSources, err := parseTerraformLocalModuleSource(modulePath)
    37  			if err != nil {
    38  				return nil, err
    39  			}
    40  
    41  			for _, subSource := range subSources {
    42  				sourceMap[subSource] = true
    43  			}
    44  		}
    45  	}
    46  
    47  	var sources = []string{}
    48  	for source := range sourceMap {
    49  		sources = append(sources, source)
    50  	}
    51  
    52  	return sources, nil
    53  }
    54  
    55  func isLocalTerraformModuleSource(raw string) bool {
    56  	for _, prefix := range localModuleSourcePrefixes {
    57  		if strings.HasPrefix(raw, prefix) {
    58  			return true
    59  		}
    60  	}
    61  
    62  	return false
    63  }