Skip to content

Commit 4d044b0

Browse files
committed
docs: Documenting --filter for source= attribute
1 parent 4708b2f commit 4d044b0

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

docs-starlight/src/content/docs/03-features/18-filter.mdx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,43 @@ terragrunt find --filter 'name=stack*'
172172
- terragrunt.hcl
173173
</FileTree>
174174

175+
### Source-Based Filtering
176+
177+
Match units and stacks by their Terraform source URL or path specified in the `terraform` block of `terragrunt.hcl` files.
178+
179+
```bash
180+
# Filter by exact source match
181+
terragrunt find --filter 'source=github.com/acme/foo'
182+
terragrunt find --filter 'source=gitlab.com/example/baz'
183+
terragrunt find --filter 'source=./module'
184+
185+
# Filter by source using glob patterns
186+
terragrunt find --filter 'source=*github.com**acme/*'
187+
terragrunt find --filter 'source=git::[email protected]:acme/**'
188+
terragrunt find --filter 'source=**github.com**'
189+
terragrunt find --filter 'source=gitlab.com/**'
190+
```
191+
192+
<FileTree>
193+
- .
194+
- **github-acme-foo** \<-- Matched by source=github.com/acme/foo and source=*github.com**acme/*
195+
- terragrunt.hcl (source: github.com/acme/foo)
196+
- **github-acme-bar** \<-- Matched by source=*github.com**acme/* and source=git::[email protected]:acme/**
197+
- terragrunt.hcl (source: git::[email protected]:acme/bar)
198+
- **gitlab-example-baz** \<-- Matched by source=gitlab.com/example/baz and source=gitlab.com/**
199+
- terragrunt.hcl (source: gitlab.com/example/baz)
200+
- **local-module** \<-- Matched by source=./module
201+
- terragrunt.hcl (source: ./module)
202+
- module
203+
- main.tf
204+
- other-unit
205+
- terragrunt.hcl (source: s3://bucket/module)
206+
</FileTree>
207+
208+
<Aside type="note">
209+
The `source=` filter matches against the Terraform source URL or path specified in the `terraform` block of `terragrunt.hcl` files in units. It supports glob patterns, allowing you to match multiple sources with patterns like `*github.com**` or `gitlab.com/**`. This is useful for filtering units that use specific module sources, such as all units using a particular GitHub organization's modules or all local modules. This attribute may be supported on stacks in the future.
210+
</Aside>
211+
175212
### Negation
176213

177214
Exclude units and stacks using the `!` prefix.

test/integration_docs_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ func TestFilterDocumentationExamples(t *testing.T) {
315315
generateIntersectionFixture(t, tmpDir)
316316
generateReadingFixture(t, tmpDir)
317317
generateGraphBasedFixture(t, tmpDir)
318+
generateSourceBasedFixture(t, tmpDir)
318319

319320
testCases := []struct {
320321
name string
@@ -512,6 +513,50 @@ func TestFilterDocumentationExamples(t *testing.T) {
512513
filterQuery: "service... | !^db...",
513514
expectedOutput: "cache\ndb\nservice\n",
514515
},
516+
517+
// Source-based filtering
518+
{
519+
name: "source-exact-match-github",
520+
fixtureDir: "source-based",
521+
filterQuery: "source=github.com/acme/foo",
522+
expectedOutput: "github-acme-foo\n",
523+
},
524+
{
525+
name: "source-exact-match-gitlab",
526+
fixtureDir: "source-based",
527+
filterQuery: "source=gitlab.com/example/baz",
528+
expectedOutput: "gitlab-example-baz\n",
529+
},
530+
{
531+
name: "source-exact-match-local",
532+
fixtureDir: "source-based",
533+
filterQuery: "source=./module",
534+
expectedOutput: "local-module\n",
535+
},
536+
{
537+
name: "source-glob-github-org",
538+
fixtureDir: "source-based",
539+
filterQuery: "source=*github.com**acme/*",
540+
expectedOutput: "github-acme-bar\ngithub-acme-foo\n",
541+
},
542+
{
543+
name: "source-glob-github-ssh",
544+
fixtureDir: "source-based",
545+
filterQuery: "source=git::[email protected]:acme/**",
546+
expectedOutput: "github-acme-bar\n",
547+
},
548+
{
549+
name: "source-glob-all-github",
550+
fixtureDir: "source-based",
551+
filterQuery: "source=**github.com**",
552+
expectedOutput: "github-acme-bar\ngithub-acme-foo\n",
553+
},
554+
{
555+
name: "source-glob-gitlab",
556+
fixtureDir: "source-based",
557+
filterQuery: "source=gitlab.com/**",
558+
expectedOutput: "gitlab-example-baz\n",
559+
},
515560
}
516561

517562
// Run all test cases
@@ -833,6 +878,59 @@ dependency "cache" {
833878
require.NoError(t, os.WriteFile(filepath.Join(serviceDir, "main.tf"), []byte(""), 0644))
834879
}
835880

881+
func generateSourceBasedFixture(t *testing.T, baseDir string) {
882+
rootDir := filepath.Join(baseDir, "source-based", "root")
883+
require.NoError(t, os.MkdirAll(rootDir, 0755))
884+
885+
// Create github-acme-foo with source github.com/acme/foo
886+
githubAcmeFooDir := filepath.Join(rootDir, "github-acme-foo")
887+
require.NoError(t, os.MkdirAll(githubAcmeFooDir, 0755))
888+
require.NoError(t, os.WriteFile(filepath.Join(githubAcmeFooDir, "terragrunt.hcl"), []byte(`terraform {
889+
source = "github.com/acme/foo"
890+
}
891+
`), 0644))
892+
require.NoError(t, os.WriteFile(filepath.Join(githubAcmeFooDir, "main.tf"), []byte(""), 0644))
893+
894+
// Create github-acme-bar with source git::[email protected]:acme/bar
895+
githubAcmeBarDir := filepath.Join(rootDir, "github-acme-bar")
896+
require.NoError(t, os.MkdirAll(githubAcmeBarDir, 0755))
897+
require.NoError(t, os.WriteFile(filepath.Join(githubAcmeBarDir, "terragrunt.hcl"), []byte(`terraform {
898+
source = "git::[email protected]:acme/bar"
899+
}
900+
`), 0644))
901+
require.NoError(t, os.WriteFile(filepath.Join(githubAcmeBarDir, "main.tf"), []byte(""), 0644))
902+
903+
// Create gitlab-example-baz with source gitlab.com/example/baz
904+
gitlabExampleBazDir := filepath.Join(rootDir, "gitlab-example-baz")
905+
require.NoError(t, os.MkdirAll(gitlabExampleBazDir, 0755))
906+
require.NoError(t, os.WriteFile(filepath.Join(gitlabExampleBazDir, "terragrunt.hcl"), []byte(`terraform {
907+
source = "gitlab.com/example/baz"
908+
}
909+
`), 0644))
910+
require.NoError(t, os.WriteFile(filepath.Join(gitlabExampleBazDir, "main.tf"), []byte(""), 0644))
911+
912+
// Create local-module with source ./module
913+
localModuleDir := filepath.Join(rootDir, "local-module")
914+
require.NoError(t, os.MkdirAll(localModuleDir, 0755))
915+
require.NoError(t, os.WriteFile(filepath.Join(localModuleDir, "terragrunt.hcl"), []byte(`terraform {
916+
source = "./module"
917+
}
918+
`), 0644))
919+
// Create the module directory with main.tf
920+
moduleDir := filepath.Join(localModuleDir, "module")
921+
require.NoError(t, os.MkdirAll(moduleDir, 0755))
922+
require.NoError(t, os.WriteFile(filepath.Join(moduleDir, "main.tf"), []byte(""), 0644))
923+
924+
// Create other-unit with source s3://bucket/module (for non-matching examples)
925+
otherUnitDir := filepath.Join(rootDir, "other-unit")
926+
require.NoError(t, os.MkdirAll(otherUnitDir, 0755))
927+
require.NoError(t, os.WriteFile(filepath.Join(otherUnitDir, "terragrunt.hcl"), []byte(`terraform {
928+
source = "s3://bucket/module"
929+
}
930+
`), 0644))
931+
require.NoError(t, os.WriteFile(filepath.Join(otherUnitDir, "main.tf"), []byte(""), 0644))
932+
}
933+
836934
// Helper functions to create Terragrunt configuration files
837935

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

0 commit comments

Comments
 (0)