Skip to content

Commit

Permalink
Merge branch 'master' into feature/which-flag
Browse files Browse the repository at this point in the history
  • Loading branch information
MatrixCrawler committed Apr 26, 2024
2 parents 6220da9 + 6e0b171 commit 16aec36
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
6 changes: 6 additions & 0 deletions lib/param_parsing/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func GetParameters() Params {
// Parse the command line parameters to fetch stuff like chdir
getopt.Parse()

oldLogLevel := params.LogLevel
logger = lib.InitLogger(params.LogLevel)
var err error
// Read configuration files
Expand All @@ -72,6 +73,11 @@ func GetParameters() Params {
logger.Fatalf("Error parsing configuration file: %q", err)
}

// Logger config was changed by the config files. Reinitialise.
if params.LogLevel != oldLogLevel {
logger = lib.InitLogger(params.LogLevel)
}

// Parse again to overwrite anything that might by defined on the cli AND in any config file (CLI always wins)
getopt.Parse()
args := getopt.Args()
Expand Down
5 changes: 3 additions & 2 deletions lib/param_parsing/terragrunt.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ func GetVersionFromTerragrunt(params Params) (Params, error) {
}
var versionFromTerragrunt terragruntVersionConstraints
diagnostics = gohcl.DecodeBody(hclFile.Body, nil, &versionFromTerragrunt)
if diagnostics.HasErrors() {
return params, fmt.Errorf("could not decode body of HCL file %q", filePath)
if versionFromTerragrunt.TerraformVersionConstraint == "" {
logger.Infof("No terraform version constraint in %q", filePath)
return params, nil
}
version, err := lib.GetSemver(versionFromTerragrunt.TerraformVersionConstraint, params.MirrorURL)
if err != nil {
Expand Down
20 changes: 11 additions & 9 deletions lib/param_parsing/terragrunt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package param_parsing
import (
"github.com/hashicorp/go-version"
"github.com/warrensbox/terraform-switcher/lib"
"strings"
"testing"
)

func TestGetVersionFromTerragrunt(t *testing.T) {
var params Params
logger = lib.InitLogger("DEBUG")
params = initParams(params)
params.ChDirPath = "../../test-data/integration-tests/test_terragrunt_hcl"
params, _ = GetVersionFromTerragrunt(params)
params, err := GetVersionFromTerragrunt(params)
if err != nil {
t.Fatalf("Got error '%s'", err)
}
v1, _ := version.NewVersion("0.13")
v2, _ := version.NewVersion("0.14")
actualVersion, _ := version.NewVersion(params.Version)
Expand All @@ -37,12 +40,11 @@ func TestGetVersionFromTerragrunt_erroneous_file(t *testing.T) {
params = initParams(params)
params.ChDirPath = "../../test-data/skip-integration-tests/test_terragrunt_error_hcl"
params, err := GetVersionFromTerragrunt(params)
if err == nil {
t.Error("Expected error but got none.")
} else {
expectedError := "could not decode body of HCL file"
if !strings.Contains(err.Error(), expectedError) {
t.Errorf("Expected error to contain '%q', got '%q'", expectedError, err)
}
if err != nil {
t.Error(err)
}
expected := ""
if params.Version != expected {
t.Errorf("Expected version '%s', got '%s'", expected, params.Version)
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
terragrunt_version_constraint = "= 0.36.2"
terraform_version_constraint = ">= 0.13, < 0.14"

0 comments on commit 16aec36

Please sign in to comment.