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

Fixed --terragrunt-config flag #2703

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,14 @@ func initialSetup(opts *options.TerragruntOptions) func(ctx *cli.Context) error
opts.DownloadDir = filepath.ToSlash(downloadDir)

// --- Terragrunt ConfigPath
if opts.TerragruntConfigPath == "" {
opts.TerragruntConfigPath = config.GetDefaultConfigPath(opts.WorkingDir)
if opts.TerragruntConfigPath == options.DefaultTerragruntConfigPath {
opts.TerragruntConfigPath = config.GetConfigPath(opts.WorkingDir, opts.TerragruntConfigPath)
} else {
// `run-all` command uses the `options.DefaultTerragruntConfigPaths` slice when looking for terragrunt configuration in subfolders,
// we need to take care that the specified `TerragruntConfigPath` is included in this slice.
options.DefaultTerragruntConfigPaths = append(options.DefaultTerragruntConfigPaths, filepath.Base(opts.TerragruntConfigPath))
}

opts.TerraformPath = filepath.ToSlash(opts.TerraformPath)

opts.ExcludeDirs, err = util.GlobCanonicalPath(opts.WorkingDir, opts.ExcludeDirs...)
Expand Down
49 changes: 24 additions & 25 deletions cli/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/gruntwork-io/terragrunt/cli/commands/hclfmt"
runall "github.com/gruntwork-io/terragrunt/cli/commands/run-all"
"github.com/gruntwork-io/terragrunt/cli/commands/terraform"
"github.com/gruntwork-io/terragrunt/config"
"github.com/gruntwork-io/terragrunt/options"
"github.com/gruntwork-io/terragrunt/pkg/cli"
"github.com/gruntwork-io/terragrunt/util"
Expand Down Expand Up @@ -42,110 +41,110 @@ func TestParseTerragruntOptionsFromArgs(t *testing.T) {
}{
{
[]string{},
mockOptions(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, false, defaultLogLevel, false),
mockOptions(t, util.JoinPath(workingDir, options.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, false, defaultLogLevel, false),
nil,
},

{
[]string{"foo", "bar"},
mockOptions(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, []string{"foo", "bar"}, false, "", false, false, defaultLogLevel, false),
mockOptions(t, util.JoinPath(workingDir, options.DefaultTerragruntConfigPath), workingDir, []string{"foo", "bar"}, false, "", false, false, defaultLogLevel, false),
nil,
},

{
[]string{"--foo", "--bar"},
mockOptions(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, []string{"-foo", "-bar"}, false, "", false, false, defaultLogLevel, false),
mockOptions(t, util.JoinPath(workingDir, options.DefaultTerragruntConfigPath), workingDir, []string{"-foo", "-bar"}, false, "", false, false, defaultLogLevel, false),
nil,
},

{
[]string{"--foo", "apply", "--bar"},
mockOptions(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, []string{"-foo", "apply", "-bar"}, false, "", false, false, defaultLogLevel, false),
mockOptions(t, util.JoinPath(workingDir, options.DefaultTerragruntConfigPath), workingDir, []string{"-foo", "apply", "-bar"}, false, "", false, false, defaultLogLevel, false),
nil,
},

{
[]string{doubleDashed(commands.FlagNameTerragruntNonInteractive)},
mockOptions(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, []string{}, true, "", false, false, defaultLogLevel, false),
mockOptions(t, util.JoinPath(workingDir, options.DefaultTerragruntConfigPath), workingDir, []string{}, true, "", false, false, defaultLogLevel, false),
nil,
},

{
[]string{doubleDashed(commands.FlagNameTerragruntIncludeExternalDependencies)},
mockOptions(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, true, defaultLogLevel, false),
mockOptions(t, util.JoinPath(workingDir, options.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, true, defaultLogLevel, false),
nil,
},

{
[]string{doubleDashed(commands.FlagNameTerragruntConfig), fmt.Sprintf("/some/path/%s", config.DefaultTerragruntConfigPath)},
mockOptions(t, fmt.Sprintf("/some/path/%s", config.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, false, defaultLogLevel, false),
[]string{doubleDashed(commands.FlagNameTerragruntConfig), fmt.Sprintf("/some/path/%s", options.DefaultTerragruntConfigPath)},
mockOptions(t, fmt.Sprintf("/some/path/%s", options.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, false, defaultLogLevel, false),
nil,
},

{
[]string{doubleDashed(commands.FlagNameTerragruntWorkingDir), "/some/path"},
mockOptions(t, util.JoinPath("/some/path", config.DefaultTerragruntConfigPath), "/some/path", []string{}, false, "", false, false, defaultLogLevel, false),
mockOptions(t, util.JoinPath("/some/path", options.DefaultTerragruntConfigPath), "/some/path", []string{}, false, "", false, false, defaultLogLevel, false),
nil,
},

{
[]string{doubleDashed(commands.FlagNameTerragruntSource), "/some/path"},
mockOptions(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, []string{}, false, "/some/path", false, false, defaultLogLevel, false),
mockOptions(t, util.JoinPath(workingDir, options.DefaultTerragruntConfigPath), workingDir, []string{}, false, "/some/path", false, false, defaultLogLevel, false),
nil,
},

{
[]string{doubleDashed(commands.FlagNameTerragruntSourceMap), "git::[email protected]:one/gw-terraform-aws-vpc.git=git::[email protected]:two/test.git?ref=FEATURE"},
mockOptionsWithSourceMap(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, []string{}, map[string]string{"git::[email protected]:one/gw-terraform-aws-vpc.git": "git::[email protected]:two/test.git?ref=FEATURE"}),
mockOptionsWithSourceMap(t, util.JoinPath(workingDir, options.DefaultTerragruntConfigPath), workingDir, []string{}, map[string]string{"git::[email protected]:one/gw-terraform-aws-vpc.git": "git::[email protected]:two/test.git?ref=FEATURE"}),
nil,
},

{
[]string{doubleDashed(commands.FlagNameTerragruntIgnoreDependencyErrors)},
mockOptions(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", true, false, defaultLogLevel, false),
mockOptions(t, util.JoinPath(workingDir, options.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", true, false, defaultLogLevel, false),
nil,
},

{
[]string{doubleDashed(commands.FlagNameTerragruntIgnoreExternalDependencies)},
mockOptions(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, false, defaultLogLevel, false),
mockOptions(t, util.JoinPath(workingDir, options.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, false, defaultLogLevel, false),
nil,
},

{
[]string{doubleDashed(commands.FlagNameTerragruntIAMRole), "arn:aws:iam::ACCOUNT_ID:role/ROLE_NAME"},
mockOptionsWithIamRole(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, "arn:aws:iam::ACCOUNT_ID:role/ROLE_NAME"),
mockOptionsWithIamRole(t, util.JoinPath(workingDir, options.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, "arn:aws:iam::ACCOUNT_ID:role/ROLE_NAME"),
nil,
},

{
[]string{doubleDashed(commands.FlagNameTerragruntIAMAssumeRoleDuration), "36000"},
mockOptionsWithIamAssumeRoleDuration(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, 36000),
mockOptionsWithIamAssumeRoleDuration(t, util.JoinPath(workingDir, options.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, 36000),
nil,
},

{
[]string{doubleDashed(commands.FlagNameTerragruntIAMAssumeRoleSessionName), "terragrunt-iam-role-session-name"},
mockOptionsWithIamAssumeRoleSessionName(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, "terragrunt-iam-role-session-name"),
mockOptionsWithIamAssumeRoleSessionName(t, util.JoinPath(workingDir, options.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, "terragrunt-iam-role-session-name"),
nil,
},

{
[]string{doubleDashed(commands.FlagNameTerragruntConfig), fmt.Sprintf("/some/path/%s", config.DefaultTerragruntConfigPath), "--terragrunt-non-interactive"},
mockOptions(t, fmt.Sprintf("/some/path/%s", config.DefaultTerragruntConfigPath), workingDir, []string{}, true, "", false, false, defaultLogLevel, false),
[]string{doubleDashed(commands.FlagNameTerragruntConfig), fmt.Sprintf("/some/path/%s", options.DefaultTerragruntConfigPath), "--terragrunt-non-interactive"},
mockOptions(t, fmt.Sprintf("/some/path/%s", options.DefaultTerragruntConfigPath), workingDir, []string{}, true, "", false, false, defaultLogLevel, false),
nil,
},

{
[]string{"--foo", doubleDashed(commands.FlagNameTerragruntConfig), fmt.Sprintf("/some/path/%s", config.DefaultTerragruntConfigPath), "bar", doubleDashed(commands.FlagNameTerragruntNonInteractive), "--baz", doubleDashed(commands.FlagNameTerragruntWorkingDir), "/some/path", doubleDashed(commands.FlagNameTerragruntSource), "github.com/foo/bar//baz?ref=1.0.3"},
mockOptions(t, fmt.Sprintf("/some/path/%s", config.DefaultTerragruntConfigPath), "/some/path", []string{"-foo", "bar", "-baz"}, true, "github.com/foo/bar//baz?ref=1.0.3", false, false, defaultLogLevel, false),
[]string{"--foo", doubleDashed(commands.FlagNameTerragruntConfig), fmt.Sprintf("/some/path/%s", options.DefaultTerragruntConfigPath), "bar", doubleDashed(commands.FlagNameTerragruntNonInteractive), "--baz", doubleDashed(commands.FlagNameTerragruntWorkingDir), "/some/path", doubleDashed(commands.FlagNameTerragruntSource), "github.com/foo/bar//baz?ref=1.0.3"},
mockOptions(t, fmt.Sprintf("/some/path/%s", options.DefaultTerragruntConfigPath), "/some/path", []string{"-foo", "bar", "-baz"}, true, "github.com/foo/bar//baz?ref=1.0.3", false, false, defaultLogLevel, false),
nil,
},

// Adding the --terragrunt-log-level flag should result in DebugLevel configured
{
[]string{doubleDashed(commands.FlagNameTerragruntLogLevel), "debug"},
mockOptions(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, false, logrus.DebugLevel, false),
mockOptions(t, util.JoinPath(workingDir, options.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, false, logrus.DebugLevel, false),
nil,
},
{
Expand All @@ -167,7 +166,7 @@ func TestParseTerragruntOptionsFromArgs(t *testing.T) {
},
{
[]string{doubleDashed(commands.FlagNameTerragruntDebug)},
mockOptions(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, false, defaultLogLevel, true),
mockOptions(t, util.JoinPath(workingDir, options.DefaultTerragruntConfigPath), workingDir, []string{}, false, "", false, false, defaultLogLevel, true),
nil,
},
}
Expand Down Expand Up @@ -261,10 +260,10 @@ func TestFilterTerragruntArgs(t *testing.T) {
}{
{[]string{}, []string{}},
{[]string{"foo", "--bar"}, []string{"foo", "-bar"}},
{[]string{"foo", doubleDashed(commands.FlagNameTerragruntConfig), fmt.Sprintf("/some/path/%s", config.DefaultTerragruntConfigPath)}, []string{"foo"}},
{[]string{"foo", doubleDashed(commands.FlagNameTerragruntConfig), fmt.Sprintf("/some/path/%s", options.DefaultTerragruntConfigPath)}, []string{"foo"}},
{[]string{"foo", doubleDashed(commands.FlagNameTerragruntNonInteractive)}, []string{"foo"}},
{[]string{"foo", doubleDashed(commands.FlagNameTerragruntDebug)}, []string{"foo"}},
{[]string{"foo", doubleDashed(commands.FlagNameTerragruntNonInteractive), "-bar", doubleDashed(commands.FlagNameTerragruntWorkingDir), "/some/path", "--baz", doubleDashed(commands.FlagNameTerragruntConfig), fmt.Sprintf("/some/path/%s", config.DefaultTerragruntConfigPath)}, []string{"foo", "-bar", "-baz"}},
{[]string{"foo", doubleDashed(commands.FlagNameTerragruntNonInteractive), "-bar", doubleDashed(commands.FlagNameTerragruntWorkingDir), "/some/path", "--baz", doubleDashed(commands.FlagNameTerragruntConfig), fmt.Sprintf("/some/path/%s", options.DefaultTerragruntConfigPath)}, []string{"foo", "-bar", "-baz"}},
{[]string{CommandNameApplyAll, "foo", "bar"}, []string{terraform.CommandNameApply, "foo", "bar"}},
{[]string{CommandNameDestroyAll, "foo", "-foo", "--bar"}, []string{terraform.CommandNameDestroy, "foo", "-foo", "-bar"}},
}
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/terraform/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ func TestFilterTerraformExtraArgs(t *testing.T) {
var defaultLogLevel = util.GetDefaultLogLevel()

func mockCmdOptions(t *testing.T, workingDir string, terraformCliArgs []string) *options.TerragruntOptions {
o := mockOptions(t, util.JoinPath(workingDir, config.DefaultTerragruntConfigPath), workingDir, terraformCliArgs, true, "", false, false, defaultLogLevel, false)
o := mockOptions(t, util.JoinPath(workingDir, options.DefaultTerragruntConfigPath), workingDir, terraformCliArgs, true, "", false, false, defaultLogLevel, false)
return o
}

Expand Down
34 changes: 13 additions & 21 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ import (
"github.com/gruntwork-io/terragrunt/util"
)

const DefaultTerragruntConfigPath = "terragrunt.hcl"
const DefaultTerragruntJsonConfigPath = "terragrunt.hcl.json"

const foundInFile = "found_in_file"

const (
Expand Down Expand Up @@ -519,7 +516,7 @@ func adjustSourceWithMap(sourceMap map[string]string, source string, modulePath
// Check if there is an entry to replace the URL portion in the map. Return the source as is if there is no entry in
// the map.
sourcePath, hasKey := sourceMap[moduleUrlQuery]
if hasKey == false {
if !hasKey {
return source, nil
}

Expand All @@ -537,23 +534,18 @@ func adjustSourceWithMap(sourceMap map[string]string, source string, modulePath

}

// Return the default hcl path to use for the Terragrunt configuration file in the given directory
func DefaultConfigPath(workingDir string) string {
return util.JoinPath(workingDir, DefaultTerragruntConfigPath)
}
// GetConfigPath returns the path to use for the Terragrunt configuration that exists within the path giving preference to `terragrunt.hcl`
func GetConfigPath(workingDir string, configPaths ...string) string {
var configPath string

// Return the default path to use for the Terragrunt Json configuration file in the given directory
func DefaultJsonConfigPath(workingDir string) string {
return util.JoinPath(workingDir, DefaultTerragruntJsonConfigPath)
}

// Return the default path to use for the Terragrunt configuration that exists within the path giving preference to `terragrunt.hcl`
func GetDefaultConfigPath(workingDir string) string {
if util.FileNotExists(DefaultConfigPath(workingDir)) && util.FileExists(DefaultJsonConfigPath(workingDir)) {
return DefaultJsonConfigPath(workingDir)
for _, configPath = range configPaths {
configPath = util.JoinPath(workingDir, configPath)
if util.FileExists(configPath) {
break
}
}

return DefaultConfigPath(workingDir)
return configPath
}

// Returns a list of all Terragrunt config files in the given path or any subfolder of the path. A file is a Terragrunt
Expand All @@ -577,7 +569,7 @@ func FindConfigFilesInPath(rootPath string, terragruntOptions *options.Terragrun
}

if isTerragruntModule {
configFiles = append(configFiles, GetDefaultConfigPath(path))
configFiles = append(configFiles, GetConfigPath(path, options.DefaultTerragruntConfigPaths...))
}

return nil
Expand Down Expand Up @@ -626,7 +618,7 @@ func containsTerragruntModule(path string, info os.FileInfo, terragruntOptions *
return false, err
}

return util.FileExists(GetDefaultConfigPath(path)), nil
return util.FileExists(GetConfigPath(path, options.DefaultTerragruntConfigPaths...)), nil
}

// Read the Terragrunt config file from its default location
Expand Down Expand Up @@ -1157,7 +1149,7 @@ func (conf *TerragruntConfig) SetFieldMetadata(fieldName string, m map[string]in
// SetFieldMetadataMap set metadata on fields from map keys.
// Example usage - setting metadata on all variables from inputs.
func (conf *TerragruntConfig) SetFieldMetadataMap(field string, data map[string]interface{}, metadata map[string]interface{}) {
for name, _ := range data {
for name := range data {
conf.SetFieldMetadataWithType(field, name, metadata)
}
}
Expand Down
6 changes: 3 additions & 3 deletions config/config_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func findInParentFolders(

previousDir = filepath.ToSlash(previousDir)

fileToFindStr := DefaultTerragruntConfigPath
fileToFindStr := options.DefaultTerragruntConfigPath
if fileToFindParam != "" {
fileToFindStr = fileToFindParam
}
Expand All @@ -408,7 +408,7 @@ func findInParentFolders(
return "", errors.WithStackTrace(ParentFileNotFound{Path: terragruntOptions.TerragruntConfigPath, File: fileToFindStr, Cause: "Traversed all the way to the root"})
}

fileToFind := GetDefaultConfigPath(currentDir)
fileToFind := GetConfigPath(currentDir, options.DefaultTerragruntConfigPaths...)
if fileToFindParam != "" {
fileToFind = util.JoinPath(currentDir, fileToFindParam)
}
Expand Down Expand Up @@ -591,7 +591,7 @@ func getCleanedTargetConfigPath(configPath string, workingPath string) string {
targetConfig = util.JoinPath(cwd, targetConfig)
}
if util.IsDir(targetConfig) {
targetConfig = GetDefaultConfigPath(targetConfig)
targetConfig = GetConfigPath(targetConfig, options.DefaultTerragruntConfigPaths...)
}
return util.CleanPath(targetConfig)
}
Expand Down
Loading