Skip to content

Commit

Permalink
feat: add fallback to include all paths functionality (#2868)
Browse files Browse the repository at this point in the history
Changes autodetect functionality so that if no paths are detected from an initial
provider/backend block project detection we fallback to include all paths. This is
particularly usefull for module repositories which often do not contain provider/backend
blocks.
  • Loading branch information
hugorut committed Feb 7, 2024
1 parent f5fc595 commit 9211cd0
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: 0.1

projects:
- path: modules/bar
name: modules-bar
skip_autodetect: true
- path: modules/baz
name: modules-baz
skip_autodetect: true
- path: modules/foo
name: modules-foo
skip_autodetect: true

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.
└── modules
├── foo
│ └── test.tf
├── bar
│ └── test.tf
└── baz
└── test.tf
5 changes: 5 additions & 0 deletions internal/config/run_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ var (
outputIndent = " "
)

// IsAutoDetect returns true if the command is running with auto-detect functionality.
func (r *RunContext) IsAutoDetect() bool {
return len(r.Config.Projects) <= 1 && r.Config.ConfigFilePath == ""
}

// NewSpinner returns an ui.Spinner built from the RunContext.
func (r *RunContext) NewSpinner(msg string) *ui.Spinner {
return ui.NewSpinner(msg, ui.SpinnerOptions{
Expand Down
51 changes: 38 additions & 13 deletions internal/hcl/project_locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,23 @@ type ProjectLocator struct {
envMatcher *EnvFileMatcher
skip bool

shouldSkipDir func(string) bool
shouldIncludeDir func(string) bool
pathOverrides []pathOverride
wdContainsTerragrunt bool
shouldSkipDir func(string) bool
shouldIncludeDir func(string) bool
pathOverrides []pathOverride
wdContainsTerragrunt bool
fallbackToIncludePaths bool
}

// ProjectLocatorConfig provides configuration options on how the locator functions.
type ProjectLocatorConfig struct {
ExcludedDirs []string
ChangedObjects []string
UseAllPaths bool
SkipAutoDetection bool
IncludedDirs []string
EnvNames []string
PathOverrides []PathOverrideConfig
ExcludedDirs []string
ChangedObjects []string
UseAllPaths bool
SkipAutoDetection bool
IncludedDirs []string
EnvNames []string
PathOverrides []PathOverrideConfig
FallbackToIncludePaths bool
}

type PathOverrideConfig struct {
Expand Down Expand Up @@ -280,6 +282,7 @@ func NewProjectLocator(logger zerolog.Logger, config *ProjectLocatorConfig) *Pro
shouldIncludeDir: func(s string) bool {
return false
},
fallbackToIncludePaths: config.FallbackToIncludePaths,
}
}

Expand Down Expand Up @@ -1021,7 +1024,7 @@ func (p *ProjectLocator) FindRootModules(fullPath string) []RootPath {
var projects []RootPath
projectMap := map[string]bool{}
for _, dir := range p.discoveredProjectsWithModulesFiltered() {
if p.shouldUseProject(dir) {
if p.shouldUseProject(dir, false) {
projects = append(projects, RootPath{
RepoPath: fullPath,
Path: dir.path,
Expand All @@ -1036,6 +1039,24 @@ func (p *ProjectLocator) FindRootModules(fullPath string) []RootPath {
}
}

if len(projects) == 0 && p.fallbackToIncludePaths {
for _, dir := range p.discoveredProjectsWithModulesFiltered() {
if p.shouldUseProject(dir, true) {
projects = append(projects, RootPath{
RepoPath: fullPath,
Path: dir.path,
HasChanges: p.hasChanges(dir.path),
TerraformVarFiles: p.discoveredVarFiles[dir.path],
Matcher: p.envMatcher,
IsTerragrunt: dir.isTerragrunt,
})
projectMap[dir.path] = true

delete(p.discoveredVarFiles, dir.path)
}
}
}

node := CreateTreeNode(fullPath, projects, p.discoveredVarFiles, p.envMatcher)
node.AssociateChildVarFiles()
node.AssociateSiblingVarFiles()
Expand Down Expand Up @@ -1112,13 +1133,17 @@ func (p *ProjectLocator) discoveredProjectsWithModulesFiltered() []discoveredPro

}

func (p *ProjectLocator) shouldUseProject(dir discoveredProject) bool {
func (p *ProjectLocator) shouldUseProject(dir discoveredProject, force bool) bool {
if p.shouldSkipDir(dir.path) {
p.logger.Debug().Msgf("skipping directory %s as it is marked as excluded by --exclude-path", dir.path)

return false
}

if force {
return true
}

if p.shouldIncludeDir(dir.path) {
return true
}
Expand Down
15 changes: 8 additions & 7 deletions internal/providers/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ func Detect(ctx *config.RunContext, project *config.Project, includePastResource
}

locatorConfig := &hcl.ProjectLocatorConfig{
ExcludedDirs: append(project.ExcludePaths, ctx.Config.Autodetect.ExcludeDirs...),
IncludedDirs: ctx.Config.Autodetect.IncludeDirs,
PathOverrides: pathOverrides,
EnvNames: ctx.Config.Autodetect.EnvNames,
ChangedObjects: ctx.VCSMetadata.Commit.ChangedObjects,
UseAllPaths: project.IncludeAllPaths,
SkipAutoDetection: project.SkipAutodetect,
ExcludedDirs: append(project.ExcludePaths, ctx.Config.Autodetect.ExcludeDirs...),
IncludedDirs: ctx.Config.Autodetect.IncludeDirs,
PathOverrides: pathOverrides,
EnvNames: ctx.Config.Autodetect.EnvNames,
ChangedObjects: ctx.VCSMetadata.Commit.ChangedObjects,
UseAllPaths: project.IncludeAllPaths,
SkipAutoDetection: project.SkipAutodetect,
FallbackToIncludePaths: ctx.IsAutoDetect(),
}
pl := hcl.NewProjectLocator(logging.Logger, locatorConfig)
rootPaths := pl.FindRootModules(project.Path)
Expand Down

0 comments on commit 9211cd0

Please sign in to comment.