Skip to content

Commit

Permalink
Merge pull request #308 from guilhem/relative_module
Browse files Browse the repository at this point in the history
Manage relative module path
  • Loading branch information
kanchwala-yusuf authored Sep 2, 2020
2 parents b698de5 + 0a73752 commit f98373b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 20 deletions.
27 changes: 9 additions & 18 deletions pkg/iac-providers/terraform/v12/load-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package tfv12

import (
"bytes"
"fmt"
"path/filepath"
"strings"
Expand Down Expand Up @@ -69,13 +68,11 @@ func (*TfV12) LoadIacDir(absRootDir string) (allResourcesConfig output.AllResour

// determine the absolute path from root module to the sub module
// using *configs.ModuleRequest.Path field
var (
pathArr = strings.Split(req.Path.String(), ".")
pathToModule = absRootDir
)
for _, subPath := range pathArr {
pathToModule = filepath.Join(pathToModule, subPath)
}

pathArr := strings.Split(req.Path.String(), ".")
pathArr = pathArr[:len(pathArr)-1]

pathToModule := filepath.Join(absRootDir, filepath.Join(pathArr...), req.SourceAddr)

// load sub module directory
subMod, diags := parser.LoadConfigDir(pathToModule)
Expand Down Expand Up @@ -122,8 +119,10 @@ func (*TfV12) LoadIacDir(absRootDir string) (allResourcesConfig output.AllResour
return allResourcesConfig, fmt.Errorf("failed to create ResourceConfig")
}

// trimFilePath
resourceConfig.Source = trimFilePath(resourceConfig.Source, absRootDir)
resourceConfig.Source, err = filepath.Rel(absRootDir, resourceConfig.Source)
if err != nil {
return allResourcesConfig, fmt.Errorf("failed to get resource: %s", err)
}

// append to normalized output
if _, present := allResourcesConfig[resourceConfig.Type]; !present {
Expand All @@ -142,11 +141,3 @@ func (*TfV12) LoadIacDir(absRootDir string) (allResourcesConfig output.AllResour
// successful
return allResourcesConfig, nil
}

// trimFilePath returns relative file path wrt to the base path
func trimFilePath(fullPath, basePath string) string {
basePath = strings.Trim(basePath, ".")
basePath = strings.Trim(basePath, "/")
splits := bytes.Split([]byte(fullPath), []byte(basePath))
return strings.TrimPrefix(string(splits[1]), "/")
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ module "sqs" {
}

module "elasticcache" {
source = "./elasticcache"
source = "../relative-moduleconfigs/elasticcache"
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
{
"id": "aws_elasticache_cluster.noMemcachedInElastiCache",
"name": "noMemcachedInElastiCache",
"source": "elasticcache/main.tf",
"source": "../relative-moduleconfigs/elasticcache/main.tf",
"line": 1,
"type": "aws_elasticache_cluster",
"config": {
Expand Down

0 comments on commit f98373b

Please sign in to comment.