Skip to content

Commit

Permalink
Refactored code - not to run init()
Browse files Browse the repository at this point in the history
  • Loading branch information
warrensbox committed Mar 3, 2020
1 parent ae4bbf2 commit ae66905
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
14 changes: 13 additions & 1 deletion lib/install.go
Expand Up @@ -22,7 +22,7 @@ var (
//installedBinPath = "/tmp"
)

func init() {
func initialize() {
/* get current user */
usr, errCurr := user.Current()
if errCurr != nil {
Expand Down Expand Up @@ -65,6 +65,17 @@ func Install(tfversion string, binPath string) {
os.Exit(1)
}

pathDir := Path(binPath) //get path directory from binary path
binDirExist := CheckDirExist(pathDir) //check bin path exist

if !binDirExist {
fmt.Printf("Error - Binary path does not exist: %s\n", pathDir)
fmt.Printf("Create binary path: %s for terraform installation\n", pathDir)
os.Exit(1)
}

initialize() //initialize path

goarch := runtime.GOARCH
goos := runtime.GOOS

Expand Down Expand Up @@ -113,6 +124,7 @@ func Install(tfversion string, binPath string) {
/* remove zipped file to clear clutter */
RemoveFiles(installLocation + installVersion + tfversion + "_" + goos + "_" + goarch + ".zip")

fmt.Println("rm2 symlink")
/* remove current symlink if exist*/
symlinkExist := CheckSymlink(binPath)

Expand Down
4 changes: 3 additions & 1 deletion lib/symlink.go
@@ -1,6 +1,7 @@
package lib

import (
"fmt"
"log"
"os"
)
Expand All @@ -23,7 +24,7 @@ func CreateSymlink(cwd string, dir string) {

//RemoveSymlink : remove symlink
func RemoveSymlink(symlinkPath string) {

fmt.Println(symlinkPath)
_, err := os.Lstat(symlinkPath)
if err != nil {
log.Fatalf(`
Expand All @@ -36,6 +37,7 @@ func RemoveSymlink(symlinkPath string) {
os.Exit(1)
} else {
errRemove := os.Remove(symlinkPath)
fmt.Println("ATTEMPING TO RM")
if errRemove != nil {
log.Fatalf(`
Unable to remove symlink.
Expand Down
9 changes: 1 addition & 8 deletions main.go
Expand Up @@ -105,14 +105,7 @@ func main() {
tfversion = version.(string)
}

pathDir := lib.Path(binPath) //get path directory from binary path
binDirExist := lib.CheckDirExist(pathDir) //check bin path exist

if !binDirExist {
fmt.Printf("Binary path does not exist: %s\n", pathDir)
fmt.Printf("Create binary path: %s for terraform installation\n", pathDir)
os.Exit(1)
} else if *listAllFlag { //show all terraform version including betas and RCs
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
Expand Down

0 comments on commit ae66905

Please sign in to comment.