Skip to content

Commit

Permalink
add glob filter (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhennecke committed Apr 22, 2021
1 parent 701a954 commit 67fb81c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ One way to customize the behavior of this module is through CLI flag values pass
| `--root` | Path to the root directory of the git repo you want to build config for. | current directory |
| `--terraform-version` | Default terraform version to specify for all modules. Can be overriden by locals | "" |
| `--ignore-dependency-blocks` | When true, dependencies found in `dependency` and `dependencies` blocks will be ignored | false |
| `--filter` | Path to the directory you want scope down the config for. Default is all files in root | "" |
| `--filter` | Path or glob expression to the directory you want scope down the config for. Default is all files in root | "" |

## All Locals

Expand Down
35 changes: 28 additions & 7 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,20 +322,41 @@ func getAllTerragruntFiles() ([]string, error) {
// If filterPath is provided, override workingPath instead of gitRoot
// We do this here because we want to keep the relative path structure of Terragrunt files
// to root and just ignore the ConfigFiles
workingPath := gitRoot
workingPaths := []string{gitRoot}
if filterPath != "" {
workingPath, err = filepath.Abs(filterPath)
// get all matching folders
workingPaths, err = filepath.Glob(filterPath)
if err != nil {
return nil, err
}
}

paths, err := config.FindConfigFilesInPath(workingPath, options)
if err != nil {
return nil, err
uniqueConfigFilePaths := make(map[string]bool)
orderedConfigFilePaths := []string{}
for _, workingPath := range workingPaths {
paths, err := config.FindConfigFilesInPath(workingPath, options)
if err != nil {
return nil, err
}
for _, p := range paths {
// if path not yet seen, insert once
if !uniqueConfigFilePaths[p] {
orderedConfigFilePaths = append(orderedConfigFilePaths, p)
uniqueConfigFilePaths[p] = true
}
}
}

uniqueConfigFileAbsPaths := []string{}
for _, uniquePath := range orderedConfigFilePaths {
uniqueAbsPath, err := filepath.Abs(uniquePath)
if err != nil {
return nil, err
}
uniqueConfigFileAbsPaths = append(uniqueConfigFileAbsPaths, uniqueAbsPath)
}

return paths, nil
return uniqueConfigFileAbsPaths, nil
}

func main(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -466,7 +487,7 @@ func init() {
generateCmd.PersistentFlags().StringVar(&defaultWorkflow, "workflow", "", "Name of the workflow to be customized in the atlantis server. Default is to not set")
generateCmd.PersistentFlags().StringSliceVar(&defaultApplyRequirements, "apply-requirements", []string{}, "Requirements that must be satisfied before `atlantis apply` can be run. Currently the only supported requirements are `approved` and `mergeable`. Can be overridden by locals")
generateCmd.PersistentFlags().StringVar(&outputPath, "output", "", "Path of the file where configuration will be generated. Default is not to write to file")
generateCmd.PersistentFlags().StringVar(&filterPath, "filter", "", "Path to the directory you want scope down the config for. Default is all files in root")
generateCmd.PersistentFlags().StringVar(&filterPath, "filter", "", "Path or glob expression to the directory you want scope down the config for. Default is all files in root")
generateCmd.PersistentFlags().StringVar(&gitRoot, "root", pwd, "Path to the root directory of the git repo you want to build config for. Default is current dir")
generateCmd.PersistentFlags().StringVar(&defaultTerraformVersion, "terraform-version", "", "Default terraform version to specify for all modules. Can be overriden by locals")
}
Expand Down
9 changes: 9 additions & 0 deletions cmd/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,12 @@ func TestFilterFlagWithInfraLiveNonProd(t *testing.T) {
filepath.Join("..", "test_examples", "terragrunt-infrastructure-live-example", "non-prod"),
})
}

func TestFilterGlobFlagWithInfraLiveMySql(t *testing.T) {
runTest(t, filepath.Join("golden", "filterGlobInfraLiveMySQL.yaml"), []string{
"--root",
filepath.Join("..", "test_examples", "terragrunt-infrastructure-live-example"),
"--filter",
filepath.Join("..", "test_examples", "terragrunt-infrastructure-live-example", "*", "*", "*", "mysql"),
})
}
32 changes: 32 additions & 0 deletions cmd/golden/filterGlobInfraLiveMySQL.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
automerge: false
parallel_apply: true
parallel_plan: true
projects:
- autoplan:
enabled: false
when_modified:
- '*.hcl'
- '*.tf*'
- ../../../account.hcl
- ../../region.hcl
- ../env.hcl
dir: non-prod/us-east-1/qa/mysql
- autoplan:
enabled: false
when_modified:
- '*.hcl'
- '*.tf*'
- ../../../account.hcl
- ../../region.hcl
- ../env.hcl
dir: non-prod/us-east-1/stage/mysql
- autoplan:
enabled: false
when_modified:
- '*.hcl'
- '*.tf*'
- ../../../account.hcl
- ../../region.hcl
- ../env.hcl
dir: prod/us-east-1/prod/mysql
version: 3

0 comments on commit 67fb81c

Please sign in to comment.