From df20be5a9cda6668f5ade933f9e70305d5ab2d82 Mon Sep 17 00:00:00 2001 From: "warren.veerasingam@gmail.com" Date: Sun, 28 Nov 2021 20:44:31 -0600 Subject: [PATCH] aded default chdir path, fixed typo --- lib/files.go | 53 ++++++++++------------ lib/install.go | 4 +- main.go | 20 ++------ test-data/test_tfswitchtoml/.tfswitch.toml | 2 +- 4 files changed, 32 insertions(+), 47 deletions(-) diff --git a/lib/files.go b/lib/files.go index 2922eb44..52a2c24a 100644 --- a/lib/files.go +++ b/lib/files.go @@ -11,6 +11,8 @@ import ( "os" "path/filepath" "strings" + + "github.com/mitchellh/go-homedir" ) // RenameFile : rename file name @@ -223,32 +225,25 @@ func GetFileName(configfile string) string { return strings.TrimSuffix(configfile, filepath.Ext(configfile)) } -//Check if user has permission to directory : -//dir=path to file -//return bool -// func CheckDirWritable(dir string) bool { -// return unix.Access(dir, unix.W_OK) == nil -// } - -// func WindowsCheckDirWritable(path string) bool { - -// info, err := os.Stat(path) -// if err != nil { -// fmt.Println("Path doesn't exist") -// return false -// } - -// err = nil -// if !info.IsDir() { -// fmt.Println("Path isn't a directory") -// return false -// } - -// // Check if the user bit is enabled in file permission -// if info.Mode().Perm()&(1<<(uint(7))) == 0 { -// fmt.Println("Write permission bit is not set on this file for user") -// return false -// } - -// return true -// } +// GetCurrentDirectory : return the current directory +func GetCurrentDirectory() string { + + dir, err := os.Getwd() //get current directory + if err != nil { + log.Printf("Failed to get current directory %v\n", err) + os.Exit(1) + } + return dir +} + +// GetHomeDirectory : return the home directory +func GetHomeDirectory() string { + + homedir, errHome := homedir.Dir() + if errHome != nil { + log.Printf("Failed to get home directory %v\n", errHome) + os.Exit(1) + } + + return homedir +} diff --git a/lib/install.go b/lib/install.go index 884addeb..1525aef8 100644 --- a/lib/install.go +++ b/lib/install.go @@ -119,7 +119,7 @@ func Install(tfversion string, binPath string, mirrorURL string) { /* set symlink to desired version */ CreateSymlink(installFileVersionPath, binPath) - fmt.Printf("Switched1 terraform to version %q \n", tfversion) + fmt.Printf("Switched terraform to version %q \n", tfversion) AddRecent(tfversion) //add to recent file for faster lookup os.Exit(0) } @@ -311,7 +311,7 @@ func InstallableBinLocation(userBinPath string) string { } } fmt.Printf("[Error] : Binary path does not exist: %s\n", userBinPath) - fmt.Printf("[Error] : Manually create bin directory at: %s and try again.\n", userBinPath) + fmt.Printf("[Error] : Manually create bin directory at: %s and try again.\n", binDir) os.Exit(1) return "" } diff --git a/main.go b/main.go index ba2dfb97..1a4fe73c 100644 --- a/main.go +++ b/main.go @@ -32,7 +32,6 @@ import ( "github.com/hashicorp/hcl2/gohcl" "github.com/hashicorp/hcl2/hclparse" "github.com/kiranjthomas/terraform-config-inspect/tfconfig" - "github.com/mitchellh/go-homedir" // "github.com/hashicorp/terraform-config-inspect/tfconfig" @@ -57,8 +56,7 @@ const ( var version = "0.12.0\n" func main() { - custBinPath := getopt.StringLong("bin", 'b', lib.ConvertExecutableExt(defaultBin), "Custom binary path. Ex: "+lib.ConvertExecutableExt("/Users/username/bin/terraform")) - chDirPath := getopt.StringLong("chdir", 'c', "Switch to a different working directory before executing the given command.") + custBinPath := getopt.StringLong("bin", 'b', lib.ConvertExecutableExt(defaultBin), "Custom binary path. Ex: tfswitch -b "+lib.ConvertExecutableExt("/Users/username/bin/terraform")) listAllFlag := getopt.BoolLong("list-all", 'l', "List all versions of terraform - including beta and rc") latestPre := getopt.StringLong("latest-pre", 'p', defaultLatest, "Latest pre-release implicit version. Ex: tfswitch --latest-pre 0.13 downloads 0.13.0-rc1 (latest)") showLatestPre := getopt.StringLong("show-latest-pre", 'P', defaultLatest, "Show latest pre-release implicit version. Ex: tfswitch --show-latest-pre 0.13 prints 0.13.0-rc1 (latest)") @@ -67,6 +65,7 @@ func main() { latestFlag := getopt.BoolLong("latest", 'u', "Get latest stable version") showLatestFlag := getopt.BoolLong("show-latest", 'U', "Show latest stable version") mirrorURL := getopt.StringLong("mirror", 'm', defaultMirror, "Install from a remote other than the default. Default: https://releases.hashicorp.com/terraform") + chDirPath := getopt.StringLong("chdir", 'c', "", "Switch to a different working directory before executing the given command. Ex: tfswitch --chdir terraform_project will run tfswitch in the terraform_project directory") versionFlag := getopt.BoolLong("version", 'v', "Displays the version of tfswitch") helpFlag := getopt.BoolLong("help", 'h', "Displays help message") _ = versionFlag @@ -74,22 +73,13 @@ func main() { getopt.Parse() args := getopt.Args() - dir, err := os.Getwd() //get current directory - if err != nil { - log.Printf("Failed to get current directory %v\n", err) - os.Exit(1) - } + dir := lib.GetCurrentDirectory() + homedir := lib.GetHomeDirectory() if *chDirPath != "" { dir = dir + "/" + *chDirPath } - homedir, errHome := homedir.Dir() - if errHome != nil { - log.Printf("Failed to get home directory %v\n", errHome) - os.Exit(1) - } - TFVersionFile := filepath.Join(dir, tfvFilename) //settings for .terraform-version file in current directory (tfenv compatible) RCFile := filepath.Join(dir, rcFilename) //settings for .tfswitchrc file in current directory (backward compatible purpose) TOMLConfigFile := filepath.Join(dir, tomlFilename) //settings for .tfswitch.toml file in current directory (option to specify bin directory) @@ -376,7 +366,7 @@ func checkTFEnvExist() bool { /* parses everything in the toml file, return required version and bin path */ func getParamsTOML(binPath string, dir string) (string, string) { - path, _ := homedir.Dir() + path := lib.GetHomeDirectory() if dir == path { path = "home directory" } else { diff --git a/test-data/test_tfswitchtoml/.tfswitch.toml b/test-data/test_tfswitchtoml/.tfswitch.toml index 63df82e7..fa6f6c00 100644 --- a/test-data/test_tfswitchtoml/.tfswitch.toml +++ b/test-data/test_tfswitchtoml/.tfswitch.toml @@ -1,2 +1,2 @@ -bin = "/Users/uveerum/bin/terraform" +bin = "/Users/warrenveerasingam/bin/terraform" version = "0.11.3"