Skip to content

Commit

Permalink
Added configuration file for custom install
Browse files Browse the repository at this point in the history
  • Loading branch information
warrensbox committed Jun 5, 2019
1 parent 0cfc907 commit bf23b9b
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 29 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ docs/.bundle/**

.tfswitchrc

tfswitch
tfswitch*

build-script.sh

.tfswitch.toml
19 changes: 13 additions & 6 deletions lib/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
118 changes: 96 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,72 +26,121 @@ 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")
_ = versionFlag

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 {
Expand All @@ -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
Expand Down

0 comments on commit bf23b9b

Please sign in to comment.