Skip to content
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

feat: allow configuration of max search depth for autodetect #2926

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions cmd/infracost/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ func (g *generateConfigCommand) run(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to unmarshal autodetect section: %w", err)
}

ctx.Config.Autodetect.ExcludeDirs = partialConfig.Autodetect.ExcludeDirs
ctx.Config.Autodetect.IncludeDirs = partialConfig.Autodetect.IncludeDirs
ctx.Config.Autodetect.EnvNames = partialConfig.Autodetect.EnvNames
ctx.Config.Autodetect.PathOverrides = partialConfig.Autodetect.PathOverrides
ctx.Config.Autodetect = partialConfig.Autodetect

_, _ = reader.Seek(0, io.SeekStart)
definedProjects = hasLineStartingWith(reader, "projects:")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 0.1
autodetect:
max_search_depth: 3

projects:
- path: infra/foo
name: infra-foo

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 0.1
autodetect:
max_search_depth: 3

projects:
{{- range $project := .DetectedProjects }}
- path: {{ $project.Path }}
name: {{ $project.Name }}
{{- end }}
7 changes: 7 additions & 0 deletions cmd/infracost/testdata/generate/max_search_depth/tree.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.
└── infra
├── foo
│ └── main.tf
└── nested
└── bar
└── main.tf
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type AutodetectConfig struct {
// PathOverrides defines paths that should be overridden with specific
// environment variable grouping.
PathOverrides []PathOverride `yaml:"path_overrides,omitempty" ignored:"true"`
// MaxSearchDepth configures the number of folders that Infracost should
// traverse to detect projects.
MaxSearchDepth int `yaml:"max_search_depth,omitempty" ignored:"true"`
}

type PathOverride struct {
Expand Down
7 changes: 7 additions & 0 deletions internal/hcl/project_locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ type ProjectLocator struct {
pathOverrides []pathOverride
wdContainsTerragrunt bool
fallbackToIncludePaths bool
maxConfiguredDepth int
}

// ProjectLocatorConfig provides configuration options on how the locator functions.
Expand All @@ -219,6 +220,7 @@ type ProjectLocatorConfig struct {
EnvNames []string
PathOverrides []PathOverrideConfig
FallbackToIncludePaths bool
MaxSearchDepth int
}

type PathOverrideConfig struct {
Expand Down Expand Up @@ -276,6 +278,7 @@ func NewProjectLocator(logger zerolog.Logger, config *ProjectLocatorConfig) *Pro
envMatcher: matcher,
useAllPaths: config.UseAllPaths,
skip: config.SkipAutoDetection,
maxConfiguredDepth: config.MaxSearchDepth,
shouldSkipDir: func(s string) bool {
return false
},
Expand Down Expand Up @@ -1198,6 +1201,10 @@ func getChildDepth(basePath string, targetPath string) (int, error) {
}

func (p *ProjectLocator) maxSearchDepth() int {
if p.maxConfiguredDepth > 0 {
return p.maxConfiguredDepth
}

if p.useAllPaths {
return 14
}
Expand Down
1 change: 1 addition & 0 deletions internal/providers/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func Detect(ctx *config.RunContext, project *config.Project, includePastResource
UseAllPaths: project.IncludeAllPaths,
SkipAutoDetection: project.SkipAutodetect,
FallbackToIncludePaths: ctx.IsAutoDetect(),
MaxSearchDepth: ctx.Config.Autodetect.MaxSearchDepth,
}
pl := hcl.NewProjectLocator(logging.Logger, locatorConfig)
rootPaths := pl.FindRootModules(project.Path)
Expand Down