From 912ea6d7c70e0cb4ad2b9bd45a57ab6be2f746cb Mon Sep 17 00:00:00 2001 From: Matthew John Date: Thu, 13 Jun 2024 21:48:04 +0100 Subject: [PATCH 1/5] Add debug logging when successfully obtaining parameter from location or when terraform exists but does not provide a version --- lib/param_parsing/environment.go | 2 ++ lib/param_parsing/terraform_version.go | 4 +++- lib/param_parsing/terragrunt.go | 4 +++- lib/param_parsing/tfswitch.go | 4 +++- lib/param_parsing/toml.go | 3 +++ lib/param_parsing/versiontf.go | 5 +++++ 6 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/param_parsing/environment.go b/lib/param_parsing/environment.go index 37267721..b4f01d2a 100644 --- a/lib/param_parsing/environment.go +++ b/lib/param_parsing/environment.go @@ -5,9 +5,11 @@ import "os" func GetParamsFromEnvironment(params Params) Params { if envVersion := os.Getenv("TF_VERSION"); envVersion != "" { params.Version = envVersion + logger.Debugf("Found environment variable TF_VERSION: %q", envVersion) } if envProduct := os.Getenv("TF_PRODUCT"); envProduct != "" { params.Product = envProduct + logger.Debugf("Found environment variable TF_PRODUCT: %q", envProduct) } return params } diff --git a/lib/param_parsing/terraform_version.go b/lib/param_parsing/terraform_version.go index eed498ef..e9b08ad4 100644 --- a/lib/param_parsing/terraform_version.go +++ b/lib/param_parsing/terraform_version.go @@ -1,10 +1,11 @@ package param_parsing import ( - "github.com/warrensbox/terraform-switcher/lib" "os" "path/filepath" "strings" + + "github.com/warrensbox/terraform-switcher/lib" ) const terraformVersionFileName = ".terraform-version" @@ -19,6 +20,7 @@ func GetParamsFromTerraformVersion(params Params) (Params, error) { return params, err } params.Version = strings.TrimSpace(string(content)) + logger.Debugf("Using version from %q: %q", filePath, params.Version) } return params, nil } diff --git a/lib/param_parsing/terragrunt.go b/lib/param_parsing/terragrunt.go index db6d5806..56911e94 100644 --- a/lib/param_parsing/terragrunt.go +++ b/lib/param_parsing/terragrunt.go @@ -2,10 +2,11 @@ package param_parsing import ( "fmt" + "path/filepath" + "github.com/hashicorp/hcl/v2/gohcl" "github.com/hashicorp/hcl/v2/hclparse" "github.com/warrensbox/terraform-switcher/lib" - "path/filepath" ) const terraGruntFileName = "terragrunt.hcl" @@ -34,6 +35,7 @@ func GetVersionFromTerragrunt(params Params) (Params, error) { return params, fmt.Errorf("no version found matching %q", versionFromTerragrunt.TerraformVersionConstraint) } params.Version = version + logger.Debugf("Using version from Terragrunt file %q: %q", filePath, params.Version) } return params, nil } diff --git a/lib/param_parsing/tfswitch.go b/lib/param_parsing/tfswitch.go index 89e78a24..24edf5bf 100644 --- a/lib/param_parsing/tfswitch.go +++ b/lib/param_parsing/tfswitch.go @@ -1,10 +1,11 @@ package param_parsing import ( - "github.com/warrensbox/terraform-switcher/lib" "os" "path/filepath" "strings" + + "github.com/warrensbox/terraform-switcher/lib" ) const tfSwitchFileName = ".tfswitchrc" @@ -19,6 +20,7 @@ func GetParamsFromTfSwitch(params Params) (Params, error) { return params, err } params.Version = strings.TrimSpace(string(content)) + logger.Debugf("Found version .tfswitchrc file %q: %q", filePath, params.Version) } return params, nil } diff --git a/lib/param_parsing/toml.go b/lib/param_parsing/toml.go index 544b20d9..6f191d84 100644 --- a/lib/param_parsing/toml.go +++ b/lib/param_parsing/toml.go @@ -28,15 +28,18 @@ func getParamsTOML(params Params) (Params, error) { if viperParser.Get("bin") != nil { params.CustomBinaryPath = viperParser.GetString("bin") + logger.Debugf("Using bin from toml file %q: %q", tomlPath, params.CustomBinaryPath) } if viperParser.Get("log-level") != nil { params.LogLevel = viperParser.GetString("log-level") } if viperParser.Get("version") != nil { params.Version = viperParser.GetString("version") + logger.Debugf("Using version from toml file %q: %q", tomlPath, params.Version) } if configKey := "product"; viperParser.Get(configKey) != nil { params.Product = viperParser.GetString(configKey) + logger.Debugf("Using product from toml file %q: %q", tomlPath, params.Product) } } return params, nil diff --git a/lib/param_parsing/versiontf.go b/lib/param_parsing/versiontf.go index d95b2f26..7fedbb64 100644 --- a/lib/param_parsing/versiontf.go +++ b/lib/param_parsing/versiontf.go @@ -58,6 +58,7 @@ func GetVersionFromVersionsTF(params Params) (Params, error) { logger.Errorf("No version found matching %q", tfConstraint) return params, err2 } + logger.Debugf("Using version from Terraform in %q: %q", relPath, params.Version) params.Version = version return params, nil } @@ -66,6 +67,10 @@ func isTerraformModule(params Params) bool { module, err := tfconfig.LoadModule(params.ChDirPath) if err != nil { logger.Warnf("Error parsing Terraform module: %v", err) + return false + } + if len(module.RequiredCore) == 0 { + logger.Debugf("No required versions specified by Terraform module: %q", params.ChDirPath) } return err == nil && len(module.RequiredCore) > 0 } From f8eb79d9bf9201e642868162b7586b1f2a0ff7d1 Mon Sep 17 00:00:00 2001 From: Matthew John Date: Thu, 13 Jun 2024 22:01:08 +0100 Subject: [PATCH 2/5] Add missing logger initialisation, fixing panic throw during tests --- lib/param_parsing/environment_test.go | 4 ++++ lib/param_parsing/parameters_test.go | 1 + 2 files changed, 5 insertions(+) diff --git a/lib/param_parsing/environment_test.go b/lib/param_parsing/environment_test.go index a7071c17..d0b98daf 100644 --- a/lib/param_parsing/environment_test.go +++ b/lib/param_parsing/environment_test.go @@ -3,9 +3,12 @@ package param_parsing import ( "os" "testing" + + "github.com/warrensbox/terraform-switcher/lib" ) func TestGetParamsFromEnvironment_version_from_env(t *testing.T) { + logger = lib.InitLogger("DEBUG") var params Params expected := "1.0.0_from_env" _ = os.Setenv("TF_VERSION", expected) @@ -18,6 +21,7 @@ func TestGetParamsFromEnvironment_version_from_env(t *testing.T) { } func TestGetParamsFromEnvironment_product_from_env(t *testing.T) { + logger = lib.InitLogger("DEBUG") var params Params expected := "opentofu" _ = os.Setenv("TF_PRODUCT", expected) diff --git a/lib/param_parsing/parameters_test.go b/lib/param_parsing/parameters_test.go index 2c03331e..b0fbfabe 100644 --- a/lib/param_parsing/parameters_test.go +++ b/lib/param_parsing/parameters_test.go @@ -169,6 +169,7 @@ func checkExpectedPrecedenceVersion(t *testing.T, expectedVersion string) { } func TestGetParameters_check_config_precedence(t *testing.T) { + logger = lib.InitLogger("DEBUG") t.Cleanup(func() { getopt.CommandLine = getopt.New() }) From 72b3f1545aba043dfce8d4b3b3c4d9d9cc0d1ff8 Mon Sep 17 00:00:00 2001 From: Matt John Date: Fri, 14 Jun 2024 05:48:17 +0100 Subject: [PATCH 3/5] Improve debug logging in param parsing Co-authored-by: George L. Yermulnik --- lib/param_parsing/environment.go | 4 ++-- lib/param_parsing/terragrunt.go | 2 +- lib/param_parsing/tfswitch.go | 2 +- lib/param_parsing/toml.go | 6 +++--- lib/param_parsing/versiontf.go | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/param_parsing/environment.go b/lib/param_parsing/environment.go index b4f01d2a..be2efd3d 100644 --- a/lib/param_parsing/environment.go +++ b/lib/param_parsing/environment.go @@ -5,11 +5,11 @@ import "os" func GetParamsFromEnvironment(params Params) Params { if envVersion := os.Getenv("TF_VERSION"); envVersion != "" { params.Version = envVersion - logger.Debugf("Found environment variable TF_VERSION: %q", envVersion) + logger.Debugf("Using version from environment variable \"TF_VERSION\": %q", envVersion) } if envProduct := os.Getenv("TF_PRODUCT"); envProduct != "" { params.Product = envProduct - logger.Debugf("Found environment variable TF_PRODUCT: %q", envProduct) + logger.Debugf("Using product from environment variable \"TF_PRODUCT\": %q", envProduct) } return params } diff --git a/lib/param_parsing/terragrunt.go b/lib/param_parsing/terragrunt.go index 56911e94..0e20d796 100644 --- a/lib/param_parsing/terragrunt.go +++ b/lib/param_parsing/terragrunt.go @@ -35,7 +35,7 @@ func GetVersionFromTerragrunt(params Params) (Params, error) { return params, fmt.Errorf("no version found matching %q", versionFromTerragrunt.TerraformVersionConstraint) } params.Version = version - logger.Debugf("Using version from Terragrunt file %q: %q", filePath, params.Version) + logger.Debugf("Using version from %q: %q", filePath, params.Version) } return params, nil } diff --git a/lib/param_parsing/tfswitch.go b/lib/param_parsing/tfswitch.go index 24edf5bf..1c5d3b38 100644 --- a/lib/param_parsing/tfswitch.go +++ b/lib/param_parsing/tfswitch.go @@ -20,7 +20,7 @@ func GetParamsFromTfSwitch(params Params) (Params, error) { return params, err } params.Version = strings.TrimSpace(string(content)) - logger.Debugf("Found version .tfswitchrc file %q: %q", filePath, params.Version) + logger.Debugf("Using version from %q: %q", filePath, params.Version) } return params, nil } diff --git a/lib/param_parsing/toml.go b/lib/param_parsing/toml.go index 6f191d84..96940f3d 100644 --- a/lib/param_parsing/toml.go +++ b/lib/param_parsing/toml.go @@ -28,18 +28,18 @@ func getParamsTOML(params Params) (Params, error) { if viperParser.Get("bin") != nil { params.CustomBinaryPath = viperParser.GetString("bin") - logger.Debugf("Using bin from toml file %q: %q", tomlPath, params.CustomBinaryPath) + logger.Debugf("Using \"bin\" from %q: %q", tomlPath, params.CustomBinaryPath) } if viperParser.Get("log-level") != nil { params.LogLevel = viperParser.GetString("log-level") } if viperParser.Get("version") != nil { params.Version = viperParser.GetString("version") - logger.Debugf("Using version from toml file %q: %q", tomlPath, params.Version) + logger.Debugf("Using \"version\" from %q: %q", tomlPath, params.Version) } if configKey := "product"; viperParser.Get(configKey) != nil { params.Product = viperParser.GetString(configKey) - logger.Debugf("Using product from toml file %q: %q", tomlPath, params.Product) + logger.Debugf("Using %q from %q: %q", configKey, tomlPath, params.Product) } } return params, nil diff --git a/lib/param_parsing/versiontf.go b/lib/param_parsing/versiontf.go index 7fedbb64..f7ae98ec 100644 --- a/lib/param_parsing/versiontf.go +++ b/lib/param_parsing/versiontf.go @@ -58,8 +58,8 @@ func GetVersionFromVersionsTF(params Params) (Params, error) { logger.Errorf("No version found matching %q", tfConstraint) return params, err2 } - logger.Debugf("Using version from Terraform in %q: %q", relPath, params.Version) params.Version = version + logger.Debugf("Using version from Terraform module at %q: %q", relPath, params.Version) return params, nil } @@ -70,7 +70,7 @@ func isTerraformModule(params Params) bool { return false } if len(module.RequiredCore) == 0 { - logger.Debugf("No required versions specified by Terraform module: %q", params.ChDirPath) + logger.Debugf("No required version constraints defined by Terraform module at %q", params.ChDirPath) } return err == nil && len(module.RequiredCore) > 0 } From a7b9cee8c8e521569b74e3962dce1e1af3949e3f Mon Sep 17 00:00:00 2001 From: Matt John Date: Fri, 14 Jun 2024 05:48:51 +0100 Subject: [PATCH 4/5] Add debug logging when reading log-level from TOML file Co-authored-by: George L. Yermulnik --- lib/param_parsing/toml.go | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/param_parsing/toml.go b/lib/param_parsing/toml.go index 96940f3d..94f11b75 100644 --- a/lib/param_parsing/toml.go +++ b/lib/param_parsing/toml.go @@ -32,6 +32,7 @@ func getParamsTOML(params Params) (Params, error) { } if viperParser.Get("log-level") != nil { params.LogLevel = viperParser.GetString("log-level") + logger.Debugf("Using \"log-level\" from %q: %q", tomlPath, params.LogLevel) } if viperParser.Get("version") != nil { params.Version = viperParser.GetString("version") From e84766a275b4cd959bb20515ee7ccf878e030cec Mon Sep 17 00:00:00 2001 From: Matthew John Date: Fri, 21 Jun 2024 07:28:46 +0100 Subject: [PATCH 5/5] Add debug when setting default-version from TOML file and default-version from envrionment variable --- lib/param_parsing/environment.go | 1 + lib/param_parsing/toml.go | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/param_parsing/environment.go b/lib/param_parsing/environment.go index 17187a5e..1d94e7e7 100644 --- a/lib/param_parsing/environment.go +++ b/lib/param_parsing/environment.go @@ -9,6 +9,7 @@ func GetParamsFromEnvironment(params Params) Params { } if envDefaultVersion := os.Getenv("TF_DEFAULT_VERSION"); envDefaultVersion != "" { params.DefaultVersion = envDefaultVersion + logger.Debugf("Using default-version from environment variable \"TF_DEFAULT_VERSION\": %q", envDefaultVersion) } if envProduct := os.Getenv("TF_PRODUCT"); envProduct != "" { params.Product = envProduct diff --git a/lib/param_parsing/toml.go b/lib/param_parsing/toml.go index 52b08907..2ac6ba8c 100644 --- a/lib/param_parsing/toml.go +++ b/lib/param_parsing/toml.go @@ -41,6 +41,7 @@ func getParamsTOML(params Params) (Params, error) { } if viperParser.Get("default-version") != nil { params.DefaultVersion = viperParser.GetString("default-version") + logger.Debugf("Using \"default-version\" from %q: %q", tomlPath, params.DefaultVersion) } if configKey := "product"; viperParser.Get(configKey) != nil { params.Product = viperParser.GetString(configKey)