From ea1bb975259daee85d38c676096c20ebb3065d95 Mon Sep 17 00:00:00 2001 From: "warren.veerasingam" Date: Wed, 11 Nov 2020 17:47:51 -0600 Subject: [PATCH] added home path option for toml file --- go.mod | 1 + go.sum | 2 + main.go | 86 +++++++++++++++--------------------- test.tf => test-data/test.tf | 0 4 files changed, 39 insertions(+), 50 deletions(-) rename test.tf => test-data/test.tf (100%) diff --git a/go.mod b/go.mod index 0709ef58..99932fc4 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/manifoldco/promptui v0.2.2-0.20180308161052-c0c0d3afc6a0 github.com/mattn/go-colorable v0.0.9 // indirect github.com/mattn/go-isatty v0.0.3 // indirect + github.com/mitchellh/go-homedir v1.1.0 github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30 github.com/pelletier/go-toml v1.4.0 // indirect github.com/spf13/afero v1.2.2 // indirect diff --git a/go.sum b/go.sum index 3998a86e..b7f5d836 100644 --- a/go.sum +++ b/go.sum @@ -98,6 +98,8 @@ github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaO github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4= github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= diff --git a/main.go b/main.go index a98bf069..5414330a 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,7 @@ import ( // TODO: move back to upstream "github.com/Masterminds/semver" "github.com/kiranjthomas/terraform-config-inspect/tfconfig" + "github.com/mitchellh/go-homedir" // "github.com/hashicorp/terraform-config-inspect/tfconfig" @@ -60,15 +61,22 @@ func main() { getopt.Parse() args := getopt.Args() - dir, err := os.Getwd() + dir, err := os.Getwd() //get current directory if err != nil { log.Printf("Failed to get current directory %v\n", err) os.Exit(1) } - tfvfile := dir + fmt.Sprintf("/%s", tfvFilename) //settings for .terraform-version file in current directory (tfenv compatible) - rcfile := dir + fmt.Sprintf("/%s", rcFilename) //settings for .tfswitchrc file in current directory (backward compatible purpose) - tomlconfigfile := dir + fmt.Sprintf("/%s", tomlFilename) //settings for .tfswitch.toml file in current directory (option to specify bin directory) + homedir, errHome := homedir.Dir() + if errHome != nil { + log.Printf("Failed to get home directory %v\n", errHome) + os.Exit(1) + } + + curr_tfvfile := dir + fmt.Sprintf("/%s", tfvFilename) //settings for .terraform-version file in current directory (tfenv compatible) + curr_rcfile := dir + fmt.Sprintf("/%s", rcFilename) //settings for .tfswitchrc file in current directory (backward compatible purpose) + curr_tomlconfigfile := dir + fmt.Sprintf("/%s", tomlFilename) //settings for .tfswitch.toml file in current directory (option to specify bin directory) + home_tomlconfigfile := homedir + fmt.Sprintf("/%s", tomlFilename) //settings for .tfswitch.toml file in current directory (option to specify bin directory) switch { case *versionFlag: @@ -77,19 +85,26 @@ func main() { case *helpFlag: //} else if *helpFlag { usageMessage() - /* Checks if the .tfswitch.toml file exist */ + /* Checks if the .tfswitch.toml file exist in home or current directory */ /* This block checks to see if the tfswitch toml file is provided in the current path. * If the .tfswitch.toml file exist, it has a higher precedence than the .tfswitchrc file * You can specify the custom binary path and the version you desire * If you provide a custom binary path with the -b option, this will override the bin value in the toml file * If you provide a version on the command line, this will override the version value in the toml file */ - case fileExists(tomlconfigfile): - - readingFileMsg(tomlFilename) - version, binPath := getParamsTOML(custBinPath, dir) + case fileExists(curr_tomlconfigfile) || fileExists(home_tomlconfigfile): + + version := "" + binPath := *custBinPath + if fileExists(curr_tomlconfigfile) { //read from toml from current directory + version, binPath = getParamsTOML(binPath, dir) + } else { // else read from toml from home directory + version, binPath = getParamsTOML(binPath, homedir) + } switch { + case checkTFModuleFileExist(dir): + installTFProvidedModule(dir, &binPath) case version != "": installVersion(version, &binPath) case len(args) == 1: @@ -97,42 +112,11 @@ func main() { case *listAllFlag: listAll := true //set list all true - all versions including beta and rc will be displayed installOption(listAll, &binPath) - case checkTFModuleFileExist(dir): - installTFProvidedModule(dir, &binPath) default: listAll := false //set list all false - only official release will be displayed installOption(listAll, &binPath) } - // if len(args) == 1 { //if the version is passed in the command line - // if lib.ValidVersionFormat(args[0]) { - // requestedVersion := args[0] - // listAll := true //set list all true - all versions including beta and rc will be displayed - // tflist, _ := lib.GetTFList(hashiURL, listAll) //get list of versions - // exist := lib.VersionExist(requestedVersion, tflist) //check if version exist before downloading it - - // if exist { - // tfversion = requestedVersion // set tfversion = the version needed - // } - // } else if version != "" { // if the required version in the toml file is provided (use it) - // tfversion = version - // } - // } - - // if *listAllFlag { //show all terraform version including betas and RCs - // listAll := true //set list all true - all versions including beta and rc will be displayed - // installOption(listAll, &binPath) - // } else if tfversion == "" { // if no version is provided, show a dropdown of available release versions - // listAll := false //set list all false - only official release will be displayed - // installOption(listAll, &binPath) - // } else { - //if lib.ValidVersionFormat(tfversion) { //check if version is correct - // lib.Install(tfversion, binPath) - // } else { - // printInvalidTFVersion() - // os.Exit(1) - // } - // } /* list all versions, //show all terraform version including betas and RCs*/ case *listAllFlag: installWithListAll(custBinPath) @@ -142,15 +126,15 @@ func main() { installVersion(args[0], custBinPath) /* provide an tfswitchrc file */ - case fileExists(rcfile) && len(args) == 0: - readingFileMsg(rcfile) - tfversion := retrieveFileContents(rcfile) + case fileExists(curr_rcfile) && len(args) == 0: + readingFileMsg(rcFilename) + tfversion := retrieveFileContents(curr_rcfile) installVersion(tfversion, custBinPath) /* if .terraform-version file found */ - case fileExists(tfvfile) && len(args) == 0: + case fileExists(curr_tfvfile) && len(args) == 0: readingFileMsg(tfvFilename) - tfversion := retrieveFileContents(tfvfile) + tfversion := retrieveFileContents(curr_tfvfile) installVersion(tfversion, custBinPath) /* if versions.tf file found */ @@ -212,7 +196,7 @@ func retrieveFileContents(file string) string { // Print message reading file content of : func readingFileMsg(filename string) { - fmt.Printf("Reading required terraform version %s \n", filename) + fmt.Printf("Reading file %s \n", filename) } // fileExists checks if a file exists and is not a directory before we try using it to prevent further errors. @@ -246,11 +230,10 @@ func checkTFModuleFileExist(dir string) bool { // } /* parses everything in the toml file, return required version and bin path */ -func getParamsTOML(custBinPath *string, dir string) (string, string) { +func getParamsTOML(binPath string, dir string) (string, string) { - fmt.Printf("Reading configuration from %s\n", tomlFilename) - binPath := *custBinPath //takes the default bin (defaultBin) if user does not specify bin path - configfileName := lib.GetFileName(tomlFilename) //get the config file + fmt.Printf("Reading configuration from %s\n", tomlFilename) //takes the default bin (defaultBin) if user does not specify bin path + configfileName := lib.GetFileName(tomlFilename) //get the config file viper.SetConfigType("toml") viper.SetConfigName(configfileName) viper.AddConfigPath(dir) @@ -267,6 +250,9 @@ func getParamsTOML(custBinPath *string, dir string) (string, string) { binPath = os.ExpandEnv(bin.(string)) } version := viper.Get("version") //attempt to get the version if it's provided in the toml + if version == nil { + version = "" + } return version.(string), binPath } diff --git a/test.tf b/test-data/test.tf similarity index 100% rename from test.tf rename to test-data/test.tf