Skip to content

Commit

Permalink
Merge branch 'gruntwork-io:master' into feature/copy-lock-file
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorfk committed Jan 18, 2024
2 parents 3ab235d + 419ea3a commit 488b2bf
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 35 deletions.
6 changes: 3 additions & 3 deletions cli/commands/catalog/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Modules []*Module
type Module struct {
*Doc

cloneUrl string
cloneURL string
repoPath string
moduleDir string
url string
Expand All @@ -33,7 +33,7 @@ type Module struct {
// NewModule returns a module instance if the given `moduleDir` path contains a Terraform module, otherwise returns nil.
func NewModule(repo *Repo, moduleDir string) (*Module, error) {
module := &Module{
cloneUrl: repo.cloneUrl,
cloneURL: repo.cloneURL,
repoPath: repo.path,
moduleDir: moduleDir,
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func (module *Module) URL() string {
}

func (module *Module) TerraformSourcePath() string {
return module.cloneUrl + "//" + module.moduleDir
return module.cloneURL + "//" + module.moduleDir
}

func (module *Module) isValid() (bool, error) {
Expand Down
68 changes: 42 additions & 26 deletions cli/commands/catalog/module/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,24 @@ const (
)

var (
gitHeadBranchName = regexp.MustCompile(`^.*?([^/]+)$`)
gitHeadBranchNameReg = regexp.MustCompile(`^.*?([^/]+)$`)
repoNameFromCloneURLReg = regexp.MustCompile(`(?i)^.*?([-a-z_.]+)[^/]*?(?:\.git)?$`)

modulesPaths = []string{"modules"}
)

type Repo struct {
cloneUrl string
cloneURL string
path string
tempDir string

remoteURL string
branchName string
}

func NewRepo(ctx context.Context, path, tempDir string) (*Repo, error) {
func NewRepo(ctx context.Context, cloneURL, tempDir string) (*Repo, error) {
repo := &Repo{
cloneUrl: path,
path: path,
tempDir: tempDir,
cloneURL: cloneURL,
path: tempDir,
}

if err := repo.clone(ctx); err != nil {
Expand Down Expand Up @@ -137,36 +136,52 @@ func (repo *Repo) moduleURL(moduleDir string) (string, error) {

// clone clones the repository to a temporary directory if the repoPath is URL
func (repo *Repo) clone(ctx context.Context) error {
if repo.path == "" {
if repo.cloneURL == "" {
currentDir, err := os.Getwd()
if err != nil {
return errors.WithStackTrace(err)
}

repo.path = currentDir
repo.cloneURL = currentDir
}

if files.IsDir(repo.path) {
if !filepath.IsAbs(repo.path) {
absRepoPath, err := filepath.Abs(repo.path)
if repoPath := repo.cloneURL; files.IsDir(repoPath) {
if !filepath.IsAbs(repoPath) {
absRepoPath, err := filepath.Abs(repoPath)
if err != nil {
return errors.WithStackTrace(err)
}

log.Debugf("Converting relative path %q to absolute %q", repo.path, absRepoPath)

repo.path = absRepoPath
log.Debugf("Converting relative path %q to absolute %q", repoPath, absRepoPath)
}
repo.path = repoPath

return nil
}

if err := os.MkdirAll(repo.tempDir, os.ModePerm); err != nil {
if err := os.MkdirAll(repo.path, os.ModePerm); err != nil {
return errors.WithStackTrace(err)
}
repo.tempDir = filepath.Join(repo.tempDir, "temp")

sourceUrl, err := terraform.ToSourceUrl(repo.cloneUrl, "")
repoName := "temp"
if match := repoNameFromCloneURLReg.FindStringSubmatch(repo.cloneURL); len(match) > 0 && match[1] != "" {
repoName = match[1]
}

repo.path = filepath.Join(repo.path, repoName)

// Since we are cloning the repository into a temporary directory, some operating systems such as MacOS have a service for deleting files that have not been accessed for a long time.
// For example, in MacOS the service is responsible for deleting unused files deletes only files while leaving the directory structure is untouched, which in turn misleads `go-getter`, which thinks that the repository exists but cannot update it due to the lack of files. In such cases, we simply delete the temporary directory in order to clone the one again.
// See https://github.com/gruntwork-io/terragrunt/pull/2888
if files.FileExists(repo.path) && !files.FileExists(repo.gitHeadfile()) {
log.Debugf("The repo dir exists but git file %q does not. Removing the repo dir for cloning from the remote source.", repo.gitHeadfile())

if err := os.RemoveAll(repo.path); err != nil {
return errors.WithStackTrace(err)
}
}

sourceUrl, err := terraform.ToSourceUrl(repo.cloneURL, "")
if err != nil {
return err
}
Expand All @@ -175,15 +190,14 @@ func (repo *Repo) clone(ctx context.Context) error {
if strings.HasPrefix(sourceUrl.Scheme, "http") {
sourceUrl.Scheme = "git::" + sourceUrl.Scheme
}
repo.cloneUrl = sourceUrl.String()
repo.cloneURL = sourceUrl.String()

log.Infof("Cloning repository %q to temprory directory %q", repo.cloneUrl, repo.tempDir)
log.Infof("Cloning repository %q to temprory directory %q", repo.cloneURL, repo.path)

if err := getter.Get(repo.tempDir, strings.Trim(sourceUrl.String(), "/"), getter.WithContext(ctx)); err != nil {
if err := getter.Get(repo.path, strings.Trim(sourceUrl.String(), "/"), getter.WithContext(ctx)); err != nil {
return errors.WithStackTrace(err)
}

repo.path = repo.tempDir
return nil
}

Expand Down Expand Up @@ -225,16 +239,18 @@ func (repo *Repo) parseRemoteURL() error {
return nil
}

func (repo *Repo) gitHeadfile() string {
return filepath.Join(repo.path, ".git", "HEAD")
}

// parseBranchName reads `.git/HEAD` file and parses a branch name.
func (repo *Repo) parseBranchName() error {
gitHeadFile := filepath.Join(repo.path, ".git", "HEAD")

data, err := files.ReadFileAsString(gitHeadFile)
data, err := files.ReadFileAsString(repo.gitHeadfile())
if err != nil {
return errors.Errorf("the specified path %q is not a git repository", repo.path)
}

if match := gitHeadBranchName.FindStringSubmatch(data); len(match) > 0 {
if match := gitHeadBranchNameReg.FindStringSubmatch(data); len(match) > 0 {
repo.branchName = strings.TrimSpace(match[1])
return nil
}
Expand Down
5 changes: 5 additions & 0 deletions config/config_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,11 @@ func ParseAndDecodeVarFile(varFile string, fileContents []byte, out interface{})
return err
}

if ctyVal.IsNull() {
// If the file is empty, doesn't make sense to do conversion
return nil
}

typedOut, hasType := out.(*map[string]interface{})
if hasType {
genericMap, err := parseCtyValueToMap(ctyVal)
Expand Down
Empty file.
2 changes: 2 additions & 0 deletions test/fixture-read-tf-vars/only-comments.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Line 1
# Line 2
14 changes: 8 additions & 6 deletions test/fixture-read-tf-vars/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
locals {
vars = jsondecode(read_tfvars_file("my.tfvars"))
json_vars = jsondecode(read_tfvars_file("my.tfvars.json"))
string_var = local.vars.string_var
bool_var = local.vars.bool_var
number_var = local.vars.number_var
list_var = local.vars.list_var
vars = jsondecode(read_tfvars_file("my.tfvars"))
json_vars = jsondecode(read_tfvars_file("my.tfvars.json"))
empty_vars = jsondecode(read_tfvars_file("empty.tfvars"))
empty_vars_2 = jsondecode(read_tfvars_file("only-comments.tfvars"))
string_var = local.vars.string_var
bool_var = local.vars.bool_var
number_var = local.vars.number_var
list_var = local.vars.list_var

json_string_var = local.json_vars.string_var
json_bool_var = local.json_vars.bool_var
Expand Down

0 comments on commit 488b2bf

Please sign in to comment.