Skip to content

Commit

Permalink
aded default chdir path, fixed typo
Browse files Browse the repository at this point in the history
  • Loading branch information
warrensbox committed Nov 29, 2021
1 parent f8a3785 commit df20be5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 47 deletions.
53 changes: 24 additions & 29 deletions lib/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"os"
"path/filepath"
"strings"

"github.com/mitchellh/go-homedir"
)

// RenameFile : rename file name
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 ""
}
Expand Down
20 changes: 5 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)")
Expand All @@ -67,29 +65,21 @@ 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

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)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion test-data/test_tfswitchtoml/.tfswitch.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
bin = "/Users/uveerum/bin/terraform"
bin = "/Users/warrenveerasingam/bin/terraform"
version = "0.11.3"

0 comments on commit df20be5

Please sign in to comment.