diff --git a/lib/files.go b/lib/files.go index 449ee2fb..657b3dea 100644 --- a/lib/files.go +++ b/lib/files.go @@ -6,6 +6,7 @@ import ( "bytes" "fmt" "io" + "io/ioutil" "log" "os" "path/filepath" @@ -162,3 +163,41 @@ func ReadLines(path string) (lines []string, err error) { } return } + +//IsDirEmpty : check if directory is empty (TODO UNIT TEST) +func IsDirEmpty(name string) bool { + + exist := false + + f, err := os.Open(name) + if err != nil { + log.Fatal(err) + } + defer f.Close() + + _, err = f.Readdirnames(1) // Or f.Readdir(1) + if err == io.EOF { + exist = true + } + return exist // Either not empty or error, suits both cases +} + +//CheckDirHasTGBin : // check binary exist (TODO UNIT TEST) +func CheckDirHasTGBin(dir, prefix string) bool { + + exist := false + + files, err := ioutil.ReadDir(dir) + if err != nil { + log.Fatal(err) + //return exist, err + } + res := []string{} + for _, f := range files { + if !f.IsDir() && strings.HasPrefix(f.Name(), prefix) { + res = append(res, filepath.Join(dir, f.Name())) + exist = true + } + } + return exist +} diff --git a/lib/files_test.go b/lib/files_test.go index b7adf24a..aa941f4f 100644 --- a/lib/files_test.go +++ b/lib/files_test.go @@ -10,8 +10,10 @@ import ( "os/user" "path/filepath" "regexp" + "runtime" "strings" "testing" + "time" "github.com/warrensbox/terraform-switcher/lib" ) @@ -289,3 +291,70 @@ func TestReadLines(t *testing.T) { cleanUp(installLocation) } + +// TestIsDirEmpty : create empty directory, check if empty +func TestIsDirEmpty(t *testing.T) { + + current := time.Now() + + installPath := "/.terraform.versions_test/" + + usr, errCurr := user.Current() + if errCurr != nil { + log.Fatal(errCurr) + } + installLocation := usr.HomeDir + installPath + + test_dir := current.Format("2006-01-02") + t.Logf("Create test dir: %v \n", test_dir) + + createDirIfNotExist(installLocation) + + createDirIfNotExist(installLocation + "/" + test_dir) + + empty := lib.IsDirEmpty(installLocation + "/" + test_dir) + + t.Logf("Expected directory to be empty %v [expected]", installLocation+"/"+test_dir) + + if empty == true { + t.Logf("Directory empty") + } else { + t.Error("Directory not empty") + } + + cleanUp(installLocation + "/" + test_dir) + + cleanUp(installLocation) + +} + +// TestCheckDirHasTGBin : create tg file in directory, check if exist +func TestCheckDirHasTFBin(t *testing.T) { + + goarch := runtime.GOARCH + goos := runtime.GOOS + installPath := "/.terraform.versions_test/" + installFile := "terraform" + + usr, errCurr := user.Current() + if errCurr != nil { + log.Fatal(errCurr) + } + installLocation := usr.HomeDir + installPath + + createDirIfNotExist(installLocation) + + createFile(installLocation + installFile + "_" + goos + "_" + goarch) + + empty := lib.CheckDirHasTGBin(installLocation, installFile) + + t.Logf("Expected directory to have tf file %v [expected]", installLocation+installFile+"_"+goos+"_"+goarch) + + if empty == true { + t.Logf("Directory empty") + } else { + t.Error("Directory not empty") + } + + cleanUp(installLocation) +} diff --git a/lib/install.go b/lib/install.go index 22f8d1e0..fd67fe46 100644 --- a/lib/install.go +++ b/lib/install.go @@ -16,8 +16,6 @@ const ( installVersion = "terraform_" binLocation = "/usr/local/bin/terraform" installPath = "/.terraform.versions/" - macOS = "_darwin_amd64.zip" - linux = "_darwin_amd64.zip" recentFile = "RECENT" ) @@ -69,19 +67,19 @@ func Install(tfversion string) { /* if selected version already exist, */ if fileExist { + /* remove current symlink if exist*/ - exist := CheckFileExist(installedBinPath) + symlinkExist := CheckSymlink(installedBinPath) - if !exist { - fmt.Println("Symlink does not exist") - } else { + if symlinkExist { + fmt.Println("Reset symlink") RemoveSymlink(installedBinPath) } /* set symlink to desired version */ CreateSymlink(installLocation+installVersion+tfversion, installedBinPath) fmt.Printf("Swicthed terraform to version %q \n", tfversion) - os.Exit(0) + return } /* if selected version already exist, */ @@ -108,12 +106,10 @@ func Install(tfversion string) { RemoveFiles(installLocation + installVersion + tfversion + "_" + goos + "_" + goarch + ".zip") /* remove current symlink if exist*/ - exist := CheckFileExist(installedBinPath) + symlinkExist := CheckSymlink(installedBinPath) - if !exist { - fmt.Println("Symlink does not exist") - } else { - fmt.Println("Symlink exist") + if symlinkExist { + fmt.Println("Reset symlink") RemoveSymlink(installedBinPath) } diff --git a/lib/symlink.go b/lib/symlink.go index efe545fc..3321b83a 100644 --- a/lib/symlink.go +++ b/lib/symlink.go @@ -1,6 +1,7 @@ package lib import ( + "fmt" "log" "os" ) @@ -30,3 +31,24 @@ func RemoveSymlink(symlinkPath string) { } } } + +// CheckSymlink : check file is symlink +func CheckSymlink(symlinkPath string) bool { + + //symlink := false + //fmt.Println("Checking symlink") + + fi, err := os.Lstat(symlinkPath) + if err != nil { + fmt.Println(err) + // symlink = false + return false + } + + if fi.Mode()&os.ModeSymlink != 0 { + //symlink = true + return true + } + + return false +} diff --git a/lib/symlink_test.go b/lib/symlink_test.go index a6db7810..328c21d3 100644 --- a/lib/symlink_test.go +++ b/lib/symlink_test.go @@ -82,3 +82,37 @@ func TestRemoveSymlink(t *testing.T) { t.Logf("Symlink was removed %v [expected]", lnCheck) } } + +// TestCheckSymlink : Create symlink, test if file is symlink +func TestCheckSymlink(t *testing.T) { + + testSymlinkSrc := "/test-tgshifter-src" + + testSymlinkDest := "/test-tgshifter-dest" + + usr, errCurr := user.Current() + if errCurr != nil { + log.Fatal(errCurr) + } + symlinkPathSrc := usr.HomeDir + testSymlinkSrc + symlinkPathDest := usr.HomeDir + testSymlinkDest + + ln, _ := os.Readlink(symlinkPathSrc) + + if ln != symlinkPathDest { + t.Log("Creating symlink") + if err := os.Symlink(symlinkPathDest, symlinkPathSrc); err != nil { + t.Error(err) + } + } + + symlinkExist := lib.CheckSymlink(symlinkPathSrc) + + if symlinkExist { + t.Logf("Symlink does exist %v [expected]", ln) + } else { + t.Logf("Symlink does not exist %v [unexpected]", ln) + } + + os.Remove(symlinkPathSrc) +}