Skip to content

Commit

Permalink
fix(toml): Fixing toml issue
Browse files Browse the repository at this point in the history
  • Loading branch information
warrensbox committed May 9, 2024
1 parent 9208450 commit 9f7fba7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
25 changes: 14 additions & 11 deletions lib/param_parsing/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,21 @@ func GetParameters() Params {
logger = lib.InitLogger(params.LogLevel)
var err error
// Read configuration files
if tomlFileExists(params) {
// TOML file can exist in the current, specified or home directory
if tomlFileExists(params.ChDirPath) || tomlFileExists(lib.GetHomeDirectory()) {
params, err = getParamsTOML(params)
} else if tfSwitchFileExists(params) {
params, err = GetParamsFromTfSwitch(params)
} else if terraformVersionFileExists(params) {
params, err = GetParamsFromTerraformVersion(params)
} else if isTerraformModule(params) {
params, err = GetVersionFromVersionsTF(params)
} else if terraGruntFileExists(params) {
params, err = GetVersionFromTerragrunt(params)
} else {
params = GetParamsFromEnvironment(params)

if tfSwitchFileExists(params) {
params, err = GetParamsFromTfSwitch(params)
} else if terraformVersionFileExists(params) {
params, err = GetParamsFromTerraformVersion(params)
} else if isTerraformModule(params) {
params, err = GetVersionFromVersionsTF(params)
} else if terraGruntFileExists(params) {
params, err = GetVersionFromTerragrunt(params)
} else {
params = GetParamsFromEnvironment(params)
}
}
if err != nil {
logger.Fatalf("Error parsing configuration file: %q", err)
Expand Down
9 changes: 5 additions & 4 deletions lib/param_parsing/toml.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package param_parsing

import (
"path/filepath"

"github.com/spf13/viper"
"github.com/warrensbox/terraform-switcher/lib"
"path/filepath"
)

const tfSwitchTOMLFileName = ".tfswitch.toml"

// getParamsTOML parses everything in the toml file, return required version and bin path
func getParamsTOML(params Params) (Params, error) {
tomlPath := filepath.Join(params.ChDirPath, tfSwitchTOMLFileName)
if tomlFileExists(params) {
if tomlFileExists(params.ChDirPath) {
logger.Infof("Reading configuration from %q", tomlPath)
configfileName := lib.GetFileName(tfSwitchTOMLFileName)
viperParser := viper.New()
Expand All @@ -38,7 +39,7 @@ func getParamsTOML(params Params) (Params, error) {
return params, nil
}

func tomlFileExists(params Params) bool {
tomlPath := filepath.Join(params.ChDirPath, tfSwitchTOMLFileName)
func tomlFileExists(dir string) bool {
tomlPath := filepath.Join(dir, tfSwitchTOMLFileName)
return lib.CheckFileExist(tomlPath)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
bin = "/usr/local/bin/terraform_from_toml"
bin = "/Users/warrenv/temp/bin/terraform"
version = "0.11.4"
log-level = "NOTICE"

0 comments on commit 9f7fba7

Please sign in to comment.