Skip to content

fix: Addressing feedback on #3929 #4003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions cli/commands/catalog/module/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import (
"regexp"
"strings"

"github.com/gruntwork-io/terragrunt/util"

"github.com/gitsight/go-vcsurl"
"github.com/gruntwork-io/go-commons/files"
"github.com/gruntwork-io/terragrunt/internal/cas"
"github.com/gruntwork-io/terragrunt/internal/errors"
"github.com/gruntwork-io/terragrunt/pkg/log"
"github.com/gruntwork-io/terragrunt/tf"
"github.com/gruntwork-io/terragrunt/util"
"github.com/hashicorp/go-getter/v2"
"gopkg.in/ini.v1"
)
Expand Down Expand Up @@ -62,7 +60,7 @@ func NewRepo(ctx context.Context, l log.Logger, cloneURL, path string, walkWithS
allowCAS: allowCAS,
}

if err := repo.clone(ctx, l); err != nil {
if err := repo.clone(ctx); err != nil {
return nil, err
}

Expand Down Expand Up @@ -91,7 +89,7 @@ func (repo *Repo) FindModules(ctx context.Context) (Modules, error) {
for _, modulesPath := range modulesPaths {
modulesPath = filepath.Join(repo.path, modulesPath)

if !files.FileExists(modulesPath) {
if !util.FileExists(modulesPath) {
continue
}

Expand Down Expand Up @@ -176,14 +174,14 @@ type CloneOptions struct {
Logger log.Logger
}

func (repo *Repo) clone(ctx context.Context, l log.Logger) error {
func (repo *Repo) clone(ctx context.Context) error {
cloneURL, err := repo.resolveCloneURL()
if err != nil {
return err
}

// Handle local directory case
if files.IsDir(cloneURL) {
if util.IsDir(cloneURL) {
return repo.handleLocalDir(cloneURL)
}

Expand All @@ -205,7 +203,7 @@ func (repo *Repo) clone(ctx context.Context, l log.Logger) error {
return nil
}

return repo.performClone(ctx, l, &opts)
return repo.performClone(ctx, &opts)
}

func (repo *Repo) resolveCloneURL() (string, error) {
Expand Down Expand Up @@ -269,14 +267,14 @@ func (repo *Repo) extractRepoName() string {
}

func (repo *Repo) shouldCleanupIncompleteClone() bool {
return files.FileExists(repo.path) && !repo.cloneCompleted()
return util.FileExists(repo.path) && !repo.cloneCompleted()
}

func (repo *Repo) cloneCompleted() bool {
return files.FileExists(filepath.Join(repo.path, cloneCompleteSentinel))
return util.FileExists(filepath.Join(repo.path, cloneCompleteSentinel))
}

func (repo *Repo) performClone(ctx context.Context, l log.Logger, opts *CloneOptions) error {
func (repo *Repo) performClone(ctx context.Context, opts *CloneOptions) error {
client := getter.DefaultClient

if repo.allowCAS {
Expand All @@ -290,7 +288,7 @@ func (repo *Repo) performClone(ctx context.Context, l log.Logger, opts *CloneOpt
IncludedGitFiles: includedGitFiles,
}

client.Getters = append([]getter.Getter{cas.NewCASGetter(&l, c, &cloneOpts)}, client.Getters...)
client.Getters = append([]getter.Getter{cas.NewCASGetter(&repo.logger, c, &cloneOpts)}, client.Getters...)
}

sourceURL, err := tf.ToSourceURL(opts.SourceURL, "")
Expand Down Expand Up @@ -337,7 +335,7 @@ func (repo *Repo) performClone(ctx context.Context, l log.Logger, opts *CloneOpt
func (repo *Repo) parseRemoteURL() error {
gitConfigPath := filepath.Join(repo.path, ".git", "config")

if !files.FileExists(gitConfigPath) {
if !util.FileExists(gitConfigPath) {
return errors.Errorf("the specified path %q is not a git repository (no .git/config file found)", repo.path)
}

Expand Down Expand Up @@ -379,7 +377,7 @@ func (repo *Repo) gitHeadfile() string {

// parseBranchName reads `.git/HEAD` file and parses a branch name.
func (repo *Repo) parseBranchName() error {
data, err := files.ReadFileAsString(repo.gitHeadfile())
data, err := util.ReadFileAsString(repo.gitHeadfile())
if err != nil {
return errors.Errorf("the specified path %q is not a git repository (no .git/HEAD file found)", repo.path)
}
Expand Down
3 changes: 1 addition & 2 deletions config/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"path/filepath"
"regexp"

"github.com/gruntwork-io/go-commons/files"
"github.com/gruntwork-io/terragrunt/config/hclparse"
"github.com/gruntwork-io/terragrunt/options"
"github.com/gruntwork-io/terragrunt/util"
Expand Down Expand Up @@ -51,7 +50,7 @@ func (cfg *CatalogConfig) normalize(configPath string) {
for i, url := range cfg.URLs {
url := filepath.Join(configDir, url)

if files.FileExists(url) {
if util.FileExists(url) {
cfg.URLs[i] = url
}
}
Expand Down
5 changes: 2 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"

"github.com/gruntwork-io/go-commons/files"
"github.com/gruntwork-io/terragrunt/codegen"
"github.com/gruntwork-io/terragrunt/config/hclparse"
"github.com/gruntwork-io/terragrunt/internal/errors"
Expand Down Expand Up @@ -658,7 +657,7 @@ func adjustSourceWithMap(sourceMap map[string]string, source string, modulePath
// that exists within the path giving preference to `terragrunt.hcl`
func GetDefaultConfigPath(workingDir string) string {
// check if a configuration file was passed as `workingDir`.
if !files.IsDir(workingDir) && files.FileExists(workingDir) {
if !util.IsDir(workingDir) && util.FileExists(workingDir) {
return workingDir
}

Expand All @@ -669,7 +668,7 @@ func GetDefaultConfigPath(workingDir string) string {
configPath = util.JoinPath(workingDir, configPath)
}

if files.FileExists(configPath) {
if util.FileExists(configPath) {
break
}
}
Expand Down
10 changes: 4 additions & 6 deletions configstack/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ import (
"sort"
"strings"

"github.com/gruntwork-io/terragrunt/internal/cache"
"github.com/gruntwork-io/terragrunt/pkg/log"
"github.com/gruntwork-io/terragrunt/tf"

"github.com/gruntwork-io/go-commons/files"
"github.com/gruntwork-io/terragrunt/config"
"github.com/gruntwork-io/terragrunt/internal/cache"
"github.com/gruntwork-io/terragrunt/internal/errors"
"github.com/gruntwork-io/terragrunt/options"
"github.com/gruntwork-io/terragrunt/pkg/log"
"github.com/gruntwork-io/terragrunt/shell"
"github.com/gruntwork-io/terragrunt/tf"
"github.com/gruntwork-io/terragrunt/util"
)

Expand Down Expand Up @@ -195,7 +193,7 @@ func (module *TerraformModule) getDependenciesForModule(modulesMap TerraformModu
return dependencies, nil //nolint:nilerr
}

if files.FileExists(dependencyModulePath) && !files.IsDir(dependencyModulePath) {
if util.FileExists(dependencyModulePath) && !util.IsDir(dependencyModulePath) {
dependencyModulePath = filepath.Dir(dependencyModulePath)
}

Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.6.0
github.com/gruntwork-io/boilerplate v0.6.0
github.com/gruntwork-io/go-commons v0.17.2
github.com/gruntwork-io/terragrunt-engine-go v0.0.8
github.com/gruntwork-io/terratest v0.48.2
github.com/hashicorp/go-cleanhttp v0.5.2
Expand Down Expand Up @@ -83,7 +82,10 @@ require (
gopkg.in/ini.v1 v1.67.0
)

require github.com/aws/aws-sdk-go-v2/service/s3 v1.74.0
require (
github.com/aws/aws-sdk-go-v2/service/s3 v1.74.0
github.com/gruntwork-io/go-commons v0.17.2
)

require (
atomicgo.dev/cursor v0.2.0 // indirect
Expand Down
43 changes: 20 additions & 23 deletions internal/cas/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,36 @@ package cas
import (
"fmt"

"github.com/gruntwork-io/terragrunt/internal/errors"
// I'm intentionally not using github.com/gruntwork-io/terragrunt/internal/errors
// here. I want to construct raw errors so that they can be wrapped later with their
// relevant stack traces when returned from locations where they're used.
"errors"
)

// Error types that can be returned by the cas package
type Error string

func (e Error) Error() string {
return string(e)
}

const (
var (
// ErrTempDir is returned when failing to create or close a temporary directory
ErrTempDir Error = "failed to create or manage temporary directory"
ErrTempDir = errors.New("failed to create or manage temporary directory")
// ErrCreateDir is returned when failing to create a directory
ErrCreateDir Error = "failed to create directory"
ErrCreateDir = errors.New("failed to create directory")
// ErrReadFile is returned when failing to read a file
ErrReadFile Error = "failed to read file"
ErrReadFile = errors.New("failed to read file")
// ErrParseTree is returned when failing to parse git tree output
ErrParseTree Error = "failed to parse git tree output"
ErrParseTree = errors.New("failed to parse git tree output")
// ErrGitClone is returned when the git clone operation fails
ErrGitClone Error = "failed to complete git clone"
ErrGitClone = errors.New("failed to complete git clone")
// ErrCreateTempDir is returned when failing to create a temporary directory
ErrCreateTempDir Error = "failed to create temporary directory"
ErrCreateTempDir = errors.New("failed to create temporary directory")
// ErrCleanupTempDir is returned when failing to clean up a temporary directory
ErrCleanupTempDir Error = "failed to clean up temporary directory"
ErrCleanupTempDir = errors.New("failed to clean up temporary directory")
// ErrCommandSpawn is returned when failing to spawn a git command
ErrCommandSpawn = errors.New("failed to spawn git command")
// ErrNoMatchingReference is returned when no matching reference is found
ErrNoMatchingReference = errors.New("no matching reference")
// ErrReadTree is returned when failing to read a git tree
ErrReadTree = errors.New("failed to read tree")
// ErrNoWorkDir is returned when a working directory is not set
ErrNoWorkDir = errors.New("working directory not set")
)

// WrappedError provides additional context for errors
Expand All @@ -50,14 +55,6 @@ func (e *WrappedError) Unwrap() error {
return e.Err
}

// Git operation errors
var (
ErrCommandSpawn = errors.New("failed to spawn git command")
ErrNoMatchingReference = errors.New("no matching reference")
ErrReadTree = errors.New("failed to read tree")
ErrNoWorkDir = errors.New("working directory not set")
)

func wrapError(op, path string, err error) error {
return &WrappedError{
Op: op,
Expand Down
2 changes: 1 addition & 1 deletion internal/cas/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestErrorString(t *testing.T) {
t.Parallel()
tests := []struct {
name string
err cas.Error
err error
want string
}{
{
Expand Down
2 changes: 1 addition & 1 deletion internal/cas/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (g *CASGetter) Detect(req *getter.Request) (bool, error) {
for _, detector := range g.Detectors {
src, ok, err := detector.Detect(req.Src, req.Pwd)
if err != nil {
return ok, err
return ok, errors.New(err)
}

if ok {
Expand Down
Loading