Skip to content

Commit

Permalink
Merge pull request #464 from warrensbox/add-additional-logging-in-par…
Browse files Browse the repository at this point in the history
…am-parsing

Add debug logging when successfully obtaining parameter from location…
  • Loading branch information
warrensbox authored Jun 21, 2024
2 parents 0f60b4f + e84766a commit bf5b9f1
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lib/param_parsing/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ 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 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
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 @@ -30,6 +33,7 @@ func TestGetParamsFromEnvironment_default_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, expect
}

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
5 changes: 5 additions & 0 deletions lib/param_parsing/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,23 @@ func getParamsTOML(params Params) (Params, error) {

if viperParser.Get("bin") != nil {
params.CustomBinaryPath = os.ExpandEnv(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")
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 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)
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
}

0 comments on commit bf5b9f1

Please sign in to comment.