diff --git a/.gitignore b/.gitignore index f31b4d7e..3de84fc4 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,8 @@ docs/.bundle/** .tfswitchrc -tfswitch \ No newline at end of file +tfswitch* + +build-script.sh + +.tfswitch.toml \ No newline at end of file diff --git a/lib/files.go b/lib/files.go index 06846509..975b5dd5 100644 --- a/lib/files.go +++ b/lib/files.go @@ -203,16 +203,23 @@ func CheckDirHasTGBin(dir, prefix string) bool { } //CheckDirExist : check if directory exist -func CheckDirExist(dir string) error { +//dir=path to file +//return path to directory +func CheckDirExist(dir string) bool { if _, err := os.Stat(dir); os.IsNotExist(err) { - return err + return false } - - return nil + return true } -// Path : removes base directory +// Path : returns path of directory +// value=path to file func Path(value string) string { - return filepath.Dir(value) } + +// GetFileName : remove file exist .tfswitch.config returns .tfswitch +func GetFileName(configfile string) string { + + return strings.TrimSuffix(configfile, filepath.Ext(configfile)) +} diff --git a/main.go b/main.go index f4153fb9..bdf5752c 100644 --- a/main.go +++ b/main.go @@ -26,17 +26,20 @@ import ( "github.com/manifoldco/promptui" "github.com/pborman/getopt" + "github.com/spf13/viper" lib "github.com/warrensbox/terraform-switcher/lib" ) const ( - hashiURL = "https://releases.hashicorp.com/terraform/" + hashiURL = "https://releases.hashicorp.com/terraform/" + defaultBin = "/usr/local/bin/terraform" ) var version = "0.7.0\n" func main() { - custBinPath := getopt.StringLong("bin", 'b', "/usr/local/bin/terraform", "Custom binary path. For example: /Users/username/bin/terraform") + //The default binary path is /usr/local/bin/terraform + custBinPath := getopt.StringLong("bin", 'b', defaultBin, "Custom binary path. For example: /Users/username/bin/terraform") listAllFlag := getopt.BoolLong("list-all", 'l', "List all versions of terraform - including beta and rc") versionFlag := getopt.BoolLong("version", 'v', "Displays the version of tfswitch") helpFlag := getopt.BoolLong("help", 'h', "Displays help message") @@ -44,54 +47,100 @@ func main() { getopt.Parse() args := getopt.Args() + pathDir := lib.Path(*custBinPath) + binDirExist := lib.CheckDirExist(pathDir) + + if !binDirExist { + fmt.Printf("Binary path does not exist: %s\n", pathDir) + fmt.Printf("Please create binary path: %s for terraform installation\n", pathDir) + os.Exit(1) + } dir, err := os.Getwd() if err != nil { log.Printf("Failed to get current directory %v\n", err) os.Exit(1) } - rcfile := dir + "/.tfswitchrc" - errBinDirExist := lib.CheckDirExist(lib.Path(*custBinPath)) - - if errBinDirExist != nil { - fmt.Printf("Binary path does not exist: %s\n", lib.Path(*custBinPath)) - fmt.Printf("Please create binary path: %s for terraform installation\n", lib.Path(*custBinPath)) - os.Exit(1) - } + rcfile := dir + "/.tfswitchrc" //settings for .tfswitchrc file + configfile := dir + "/.tfswitch.toml" if *versionFlag { fmt.Printf("\nVersion: %v\n", version) } else if *helpFlag { usageMessage() - } else if *listAllFlag { - listAll := true //set list all true - all versions including beta and rc will be displayed - installOption(listAll, custBinPath) } else { - if len(args) == 1 { //if tf version is provided in command line + if _, err := os.Stat(configfile); err == nil { + fmt.Println("Reading required terraform version from .tfswitch.toml") + tfversion := "" + binPath := *custBinPath + configfileName := lib.GetFileName(".tfswitch.toml") + viper.SetConfigType("toml") + viper.SetConfigName(configfileName) + viper.AddConfigPath(dir) + + errs := viper.ReadInConfig() // Find and read the config file + if errs != nil { + fmt.Println("Unable to read .tfswitch.toml provided") // Handle errors reading the config file + fmt.Println(err) + os.Exit(1) + } - if lib.ValidVersionFormat(args[0]) { + checkDefault := strings.Compare(binPath, defaultBin) + + bin := viper.Get("bin") + if checkDefault != -1 && bin != nil { + binPath = bin.(string) + } + version := viper.Get("version") + if version != nil { + tfversion = version.(string) + } + if len(args) == 1 { + fmt.Println("ARGs and conf file - delete") 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 { - lib.Install(requestedVersion, *custBinPath) - } else { - fmt.Println("The provided terraform version does not exist. Try `tfswitch -l` to see all available versions.") + tfversion = requestedVersion } + } + + pathDir := lib.Path(binPath) + binDirExist := lib.CheckDirExist(pathDir) + if !binDirExist { + fmt.Printf("Binary path does not exist: %s\n", pathDir) + fmt.Printf("Please create binary path: %s for terraform installation\n", pathDir) + os.Exit(1) + } else if *listAllFlag { + listAll := true //set list all true - all versions including beta and rc will be displayed + installOption(listAll, &binPath) + } else if tfversion == "" { + // if *listAllFlag { + // listAll := true //set list all true - all versions including beta and rc will be displayed + // installOption(listAll, &binPath) + // } else { + listAll := false //set list all false - only official release will be displayed + installOption(listAll, &binPath) + //} + os.Exit(0) } else { - fmt.Println("Invalid terraform version format. Format should be #.#.# or #.#.#-@# where # is numbers and @ is word characters. For example, 0.11.7 and 0.11.9-beta1 are valid versions") - fmt.Println("Args must be a valid terraform version") - usageMessage() + if lib.ValidVersionFormat(tfversion) { //check if version is correct + lib.Install(tfversion, binPath) + } else { + fmt.Println("Invalid terraform version format. Format should be #.#.# or #.#.#-@# where # is numbers and @ is word characters. For example, 0.11.7 and 0.11.9-beta1 are valid versions") + os.Exit(1) + } } } else if _, err := os.Stat(rcfile); err == nil && len(args) == 0 { //if there is a .tfswitchrc file, and no commmand line arguments - fmt.Println("Reading required terraform version ...") + fmt.Println("NO ARGs but rc file - delete") + fmt.Println("Reading required terraform version .tfswitchrc ") fileContents, err := ioutil.ReadFile(rcfile) if err != nil { @@ -107,6 +156,31 @@ func main() { fmt.Println("Invalid terraform version format. Format should be #.#.# or #.#.#-@# where # is numbers and @ is word characters. For example, 0.11.7 and 0.11.9-beta1 are valid versions") os.Exit(1) } + } else if len(args) == 1 { //if tf version is provided in command line + fmt.Println("ARGs NO conf file - delete") + 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 { + lib.Install(requestedVersion, *custBinPath) + } else { + fmt.Println("The provided terraform version does not exist. Try `tfswitch -l` to see all available versions.") + } + + } else { + fmt.Println("Invalid terraform version format. Format should be #.#.# or #.#.#-@# where # is numbers and @ is word characters. For example, 0.11.7 and 0.11.9-beta1 are valid versions") + fmt.Println("Args must be a valid terraform version") + usageMessage() + } + + } else if *listAllFlag { + listAll := true //set list all true - all versions including beta and rc will be displayed + installOption(listAll, custBinPath) + } else if len(args) == 0 { //if there are no commmand line arguments listAll := false //set list all false - only official release will be displayed