Skip to content

Commit

Permalink
feat: add TerragruntOptions.IncludeModulePrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
maciasello committed Mar 29, 2023
1 parent 776a20a commit 677f935
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cli/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ func parseTerragruntOptionsFromArgs(terragruntVersion string, args []string, wri
opts.Debug = true
}

includeModulePrefix := parseBooleanArg(args, optTerragruntIncludeModulePrefix, os.Getenv("TERRAGRUNT_INCLUDE_MODULE_PREFIX") == "true" || os.Getenv("TERRAGRUNT_INCLUDE_MODULE_PREFIX") == "1")
if includeModulePrefix {
opts.IncludeModulePrefix = true
}

opts.RunAllAutoApprove = !parseBooleanArg(args, optTerragruntNoAutoApprove, os.Getenv("TERRAGRUNT_AUTO_APPROVE") == "false")

var parallelism int
Expand Down
3 changes: 3 additions & 0 deletions cli/cli_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const (
optTerragruntModulesThatInclude = "terragrunt-modules-that-include"
optTerragruntFetchDependencyOutputFromState = "terragrunt-fetch-dependency-output-from-state"
optTerragruntUsePartialParseConfigCache = "terragrunt-use-partial-parse-config-cache"
optTerragruntIncludeModulePrefix = "terragrunt-include-module-prefix"
optTerragruntOutputWithMetadata = "with-metadata"
)

Expand All @@ -81,6 +82,7 @@ var allTerragruntBooleanOpts = []string{
optTerragruntFetchDependencyOutputFromState,
optTerragruntUsePartialParseConfigCache,
optTerragruntOutputWithMetadata,
optTerragruntIncludeModulePrefix,
}
var allTerragruntStringOpts = []string{
optTerragruntConfig,
Expand Down Expand Up @@ -263,6 +265,7 @@ GLOBAL OPTIONS:
terragrunt-strict-validate Sets strict mode for the validate-inputs command. By default, strict mode is off. When this flag is passed, strict mode is turned on. When strict mode is turned off, the validate-inputs command will only return an error if required inputs are missing from all input sources (env vars, var files, etc). When strict mode is turned on, an error will be returned if required inputs are missing OR if unused variables are passed to Terragrunt.
terragrunt-json-out The file path that terragrunt should use when rendering the terragrunt.hcl config as json. Only used in the render-json command. Defaults to terragrunt_rendered.json.
terragrunt-use-partial-parse-config-cache Enables caching of includes during partial parsing operations. Will also be used for the --terragrunt-iam-role option if provided.
terragrunt-include-module-prefix When this flag is set output from Terraform sub-commands is prefixed with module path.
VERSION:
{{.Version}}{{if len .Authors}}
Expand Down
4 changes: 4 additions & 0 deletions configstack/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ func resolveTerraformModule(terragruntConfigPath string, terragruntOptions *opti
return nil, nil
}

if opts.IncludeModulePrefix {
opts.OutputPrefix = fmt.Sprintf("[%v] ", modulePath)
}

return &TerraformModule{Path: modulePath, Config: *terragruntConfig, TerragruntOptions: opts}, nil
}

Expand Down
8 changes: 8 additions & 0 deletions docs/_docs/04_reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ prefix `--terragrunt-` (e.g., `--terragrunt-config`). The currently available op
- [terragrunt-modules-that-include](#terragrunt-modules-that-include)
- [terragrunt-fetch-dependency-output-from-state](#terragrunt-fetch-dependency-output-from-state)
- [terragrunt-use-partial-parse-config-cache](#terragrunt-use-partial-parse-config-cache)
- [terragrunt-include-module-prefix](#terragrunt-include-module-prefix)

### terragrunt-config

Expand Down Expand Up @@ -912,3 +913,10 @@ Currently only AWS S3 backend is supported.

This flag can be used to drastically decrease time required for parsing Terragrunt files. The effect will only show if a lot of similar includes are expected such as the root terragrunt.hcl include.
NOTE: This is an experimental feature, use with caution.

### terragrunt-include-module-prefix

**CLI Arg**: `--terragrunt-include-module-prefix`
**Environment Variable**: `TERRAGRUNT_INCLUDE_MODULE_PREFIX` (set to `true`)

When this flag is set output from Terraform sub-commands is prefixed with module path.
5 changes: 5 additions & 0 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ type TerragruntOptions struct {

// Prefix for shell commands' outputs
OutputPrefix string

// Controls if a module prefix will be prepended to TF outputs
IncludeModulePrefix bool
}

// IAMOptions represents options that are used by Terragrunt to assume an IAM role.
Expand Down Expand Up @@ -281,6 +284,7 @@ func NewTerragruntOptions(terragruntConfigPath string) (*TerragruntOptions, erro
FetchDependencyOutputFromState: false,
UsePartialParseConfigCache: false,
OutputPrefix: "",
IncludeModulePrefix: false,
RunTerragrunt: func(terragruntOptions *TerragruntOptions) error {
return errors.WithStackTrace(RunTerragruntCommandNotSet)
},
Expand Down Expand Up @@ -378,6 +382,7 @@ func (terragruntOptions *TerragruntOptions) Clone(terragruntConfigPath string) *
FetchDependencyOutputFromState: terragruntOptions.FetchDependencyOutputFromState,
UsePartialParseConfigCache: terragruntOptions.UsePartialParseConfigCache,
OutputPrefix: terragruntOptions.OutputPrefix,
IncludeModulePrefix: terragruntOptions.IncludeModulePrefix,
}
}

Expand Down
37 changes: 37 additions & 0 deletions test/integration_include_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,43 @@ func TestTerragruntRunAllModulesThatIncludeRestrictsSet(t *testing.T) {
assert.NotContains(t, planOutput, "charlie")
}

func TestTerragruntRunAllModulesWithPrefix(t *testing.T) {
t.Parallel()

stdout := bytes.Buffer{}
stderr := bytes.Buffer{}
err := runTerragruntCommand(
t,
fmt.Sprintf(
"terragrunt run-all plan --terragrunt-non-interactive --terragrunt-include-module-prefix --terragrunt-working-dir %s",
includeRunAllFixturePath,
),
&stdout,
&stderr,
)
require.NoError(t, err)
logBufferContentsLineByLine(t, stdout, "stdout")
logBufferContentsLineByLine(t, stderr, "stderr")

planOutput := stdout.String()
assert.Contains(t, planOutput, "alpha")
assert.Contains(t, planOutput, "beta")
assert.Contains(t, planOutput, "charlie")

stdoutLines := strings.Split(planOutput, "\n")
for _, line := range stdoutLines {
if strings.Contains(line, "alpha") {
assert.Contains(t, line, includeRunAllFixturePath+"a")
}
if strings.Contains(line, "beta") {
assert.Contains(t, line, includeRunAllFixturePath+"b")
}
if strings.Contains(line, "charlie") {
assert.Contains(t, line, includeRunAllFixturePath+"c")
}
}
}

func TestTerragruntWorksWithIncludeDeepMerge(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 677f935

Please sign in to comment.