Skip to content

Commit 92cd370

Browse files
authored
docs: Documenting the new reading attribute (#5005)
* feat: Adding integration of `reading=` into `filter` * fix: Testing integration into discovery * chore: Cleanup * fix: Fixing rebase * docs: Documenting the new `reading` attribute
1 parent 9037331 commit 92cd370

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

docs-starlight/src/data/flags/filter.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,22 @@ terragrunt find --filter 'type=stack'
6161

6262
# Filter by external dependency status
6363
terragrunt find --filter 'external=false'
64+
65+
# Filter by files read
66+
terragrunt find --filter 'reading=shared.hcl'
67+
terragrunt find --filter 'reading=common/*.hcl' # Globs supported!
68+
terragrunt find --filter 'reading=config/**' # Double-wildcard globs are required filtering on files nested in subdirectories.
69+
terragrunt find --filter 'reading=config/vars.tfvars'
6470
```
6571

72+
<Aside type="tip" title="What does 'Reading' mean?">
73+
74+
The `reading` attribute filters configurations based on which files they themselves read. This is useful for finding all infrastructure that could potentially be impacted by shared configuration, and is particularly useful when a commonly included configuration (like `root.hcl`) is updated.
75+
76+
See the [mark_as_read function documentation](/docs/reference/hcl/functions/#mark_as_read) for more information on how Terragrunt tracks files that are read during parsing.
77+
78+
</Aside>
79+
6680
### Negation
6781
Exclude configurations using the `!` prefix:
6882

test/integration_docs_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ func TestFilterDocumentationExamples(t *testing.T) {
313313
generatePathBasedFixture(t, tmpDir)
314314
generateNegationFixture(t, tmpDir)
315315
generateIntersectionFixture(t, tmpDir)
316+
generateReadingFixture(t, tmpDir)
316317

317318
testCases := []struct {
318319
name string
@@ -434,6 +435,38 @@ func TestFilterDocumentationExamples(t *testing.T) {
434435
filterQuery: "./dev/**|type=unit|!name=unit1", // Our testing arg parsing is busted. Don't put whitespace between these.
435436
expectedOutput: "dev/units/unit2\n",
436437
},
438+
439+
// Reading attribute filtering
440+
{
441+
name: "reading-exact-file-match",
442+
fixtureDir: "reading",
443+
filterQuery: "reading=shared.hcl",
444+
expectedOutput: "apps/app1\napps/app2\n",
445+
},
446+
{
447+
name: "reading-glob-pattern",
448+
fixtureDir: "reading",
449+
filterQuery: "reading=shared*",
450+
expectedOutput: "apps/app1\napps/app2\n",
451+
},
452+
{
453+
name: "reading-nested-path",
454+
fixtureDir: "reading",
455+
filterQuery: "reading=common/vars.hcl",
456+
expectedOutput: "apps/app3\n",
457+
},
458+
{
459+
name: "reading-negation",
460+
fixtureDir: "reading",
461+
filterQuery: "!reading=shared.hcl",
462+
expectedOutput: "apps/app3\nlibs/lib1\n",
463+
},
464+
{
465+
name: "reading-intersection",
466+
fixtureDir: "reading",
467+
filterQuery: "./apps/**|reading=shared.hcl", // Our testing arg parsing is busted. Don't put whitespace between these.
468+
expectedOutput: "apps/app1\napps/app2\n",
469+
},
437470
}
438471

439472
// Run all test cases
@@ -650,6 +683,77 @@ func generateUnionFixture(t *testing.T, baseDir string) {
650683
createTerragruntStack(t, filepath.Join(rootDir, "dev", "stack2"))
651684
}
652685

686+
func generateReadingFixture(t *testing.T, baseDir string) {
687+
rootDir := filepath.Join(baseDir, "reading", "root")
688+
require.NoError(t, os.MkdirAll(rootDir, 0755))
689+
690+
// Create shared configuration files
691+
require.NoError(t, os.WriteFile(filepath.Join(rootDir, "shared.hcl"), []byte(`
692+
locals {
693+
common_value = "shared"
694+
}
695+
`), 0644))
696+
697+
require.NoError(t, os.WriteFile(filepath.Join(rootDir, "shared.tfvars"), []byte(`
698+
test_var = "value"
699+
`), 0644))
700+
701+
commonDir := filepath.Join(rootDir, "common")
702+
require.NoError(t, os.MkdirAll(commonDir, 0755))
703+
require.NoError(t, os.WriteFile(filepath.Join(commonDir, "vars.hcl"), []byte(`
704+
locals {
705+
vpc_cidr = "10.0.0.0/16"
706+
}
707+
`), 0644))
708+
709+
// Create apps/app1 - reads shared.hcl
710+
app1Dir := filepath.Join(rootDir, "apps", "app1")
711+
require.NoError(t, os.MkdirAll(app1Dir, 0755))
712+
require.NoError(t, os.WriteFile(filepath.Join(app1Dir, "terragrunt.hcl"), []byte(`
713+
locals {
714+
shared = read_terragrunt_config("../../shared.hcl")
715+
}
716+
717+
terraform {
718+
source = "."
719+
}
720+
`), 0644))
721+
require.NoError(t, os.WriteFile(filepath.Join(app1Dir, "main.tf"), []byte(""), 0644))
722+
723+
// Create apps/app2 - reads shared.hcl and shared.tfvars
724+
app2Dir := filepath.Join(rootDir, "apps", "app2")
725+
require.NoError(t, os.MkdirAll(app2Dir, 0755))
726+
require.NoError(t, os.WriteFile(filepath.Join(app2Dir, "terragrunt.hcl"), []byte(`
727+
locals {
728+
shared = read_terragrunt_config("../../shared.hcl")
729+
vars = read_tfvars_file("../../shared.tfvars")
730+
}
731+
732+
terraform {
733+
source = "."
734+
}
735+
`), 0644))
736+
require.NoError(t, os.WriteFile(filepath.Join(app2Dir, "main.tf"), []byte(""), 0644))
737+
738+
// Create apps/app3 - reads common/vars.hcl
739+
app3Dir := filepath.Join(rootDir, "apps", "app3")
740+
require.NoError(t, os.MkdirAll(app3Dir, 0755))
741+
require.NoError(t, os.WriteFile(filepath.Join(app3Dir, "terragrunt.hcl"), []byte(`
742+
locals {
743+
common = read_terragrunt_config("../../common/vars.hcl")
744+
}
745+
746+
terraform {
747+
source = "."
748+
}
749+
`), 0644))
750+
require.NoError(t, os.WriteFile(filepath.Join(app3Dir, "main.tf"), []byte(""), 0644))
751+
752+
// Create libs/lib1 - doesn't read any files
753+
lib1Dir := filepath.Join(rootDir, "libs", "lib1")
754+
createTerragruntUnit(t, lib1Dir)
755+
}
756+
653757
// Helper functions to create Terragrunt configuration files
654758

655759
func createTerragruntUnit(t *testing.T, dir string) {

0 commit comments

Comments
 (0)