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

Add debug logging when successfully obtaining parameter from location… #464

Merged
merged 6 commits into from
Jun 21, 2024
2 changes: 2 additions & 0 deletions lib/param_parsing/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import "os"
func GetParamsFromEnvironment(params Params) Params {
if envVersion := os.Getenv("TF_VERSION"); envVersion != "" {
params.Version = envVersion
logger.Debugf("Using version from environment variable \"TF_VERSION\": %q", envVersion)
}
if envProduct := os.Getenv("TF_PRODUCT"); envProduct != "" {
params.Product = envProduct
logger.Debugf("Using product from environment variable \"TF_PRODUCT\": %q", envProduct)
}
return params
}
4 changes: 4 additions & 0 deletions lib/param_parsing/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/param_parsing/parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down
4 changes: 3 additions & 1 deletion lib/param_parsing/terraform_version.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion lib/param_parsing/terragrunt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 %q: %q", filePath, params.Version)
}
return params, nil
}
Expand Down
4 changes: 3 additions & 1 deletion lib/param_parsing/tfswitch.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -19,6 +20,7 @@ func GetParamsFromTfSwitch(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
}
Expand Down
4 changes: 4 additions & 0 deletions lib/param_parsing/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ func getParamsTOML(params Params) (Params, error) {

if viperParser.Get("bin") != nil {
params.CustomBinaryPath = viperParser.GetString("bin")
logger.Debugf("Using \"bin\" from %q: %q", tomlPath, params.CustomBinaryPath)
}
if viperParser.Get("log-level") != nil {
params.LogLevel = viperParser.GetString("log-level")
MatthewJohn marked this conversation as resolved.
Show resolved Hide resolved
logger.Debugf("Using \"log-level\" from %q: %q", tomlPath, params.LogLevel)
}
if viperParser.Get("version") != nil {
params.Version = viperParser.GetString("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 %q from %q: %q", configKey, tomlPath, params.Product)
}
}
return params, nil
Expand Down
5 changes: 5 additions & 0 deletions lib/param_parsing/versiontf.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,18 @@ func GetVersionFromVersionsTF(params Params) (Params, error) {
return params, err2
}
params.Version = version
logger.Debugf("Using version from Terraform module at %q: %q", relPath, params.Version)
return params, nil
}

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 version constraints defined by Terraform module at %q", params.ChDirPath)
}
return err == nil && len(module.RequiredCore) > 0
}