Skip to content

Commit

Permalink
Merge pull request #32 from warrensbox/master
Browse files Browse the repository at this point in the history
Show less output log message
  • Loading branch information
warrensbox authored Oct 24, 2018
2 parents ff9d161 + a213c90 commit e0ff704
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 21 deletions.
39 changes: 39 additions & 0 deletions lib/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -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
}
69 changes: 69 additions & 0 deletions lib/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"os/user"
"path/filepath"
"regexp"
"runtime"
"strings"
"testing"
"time"

"github.com/warrensbox/terraform-switcher/lib"
)
Expand Down Expand Up @@ -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)
}
26 changes: 6 additions & 20 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os/user"
"regexp"
"runtime"
"strings"
)

const (
Expand All @@ -16,8 +15,6 @@ const (
installVersion = "terraform_"
binLocation = "/usr/local/bin/terraform"
installPath = "/.terraform.versions/"
macOS = "_darwin_amd64.zip"
linux = "_darwin_amd64.zip"
recentFile = "RECENT"
)

Expand Down Expand Up @@ -47,12 +44,9 @@ func init() {
/* overrride installation default binary path if terraform is already installed */
/* find the last bin path */
for path := next(); len(path) > 0; path = next() {
fmt.Printf("Found installation path: %v \n", path)
installedBinPath = path
}

fmt.Printf("Terraform binary path: %v \n", installedBinPath)

/* Create local installation directory if it does not exist */
CreateDirIfNotExist(installLocation)

Expand All @@ -69,12 +63,11 @@ 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 {
RemoveSymlink(installedBinPath)
}

Expand All @@ -89,31 +82,24 @@ func Install(tfversion string) {
url := hashiURL + tfversion + "/" + installVersion + tfversion + "_" + goos + "_" + goarch + ".zip"
zipFile, _ := DownloadFromURL(installLocation, url)

fmt.Printf("Downloaded zipFile: %v \n", zipFile)

/* unzip the downloaded zipfile */
files, errUnzip := Unzip(zipFile, installLocation)
_, errUnzip := Unzip(zipFile, installLocation)
if errUnzip != nil {
fmt.Println("Unable to unzip downloaded zip file")
log.Fatal(errUnzip)
os.Exit(1)
}

fmt.Println("Unzipped: " + strings.Join(files, "\n"))

/* rename unzipped file to terraform version name - terraform_x.x.x */
RenameFile(installLocation+installFile, installLocation+installVersion+tfversion)

/* remove zipped file to clear clutter */
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 {
RemoveSymlink(installedBinPath)
}

Expand Down
22 changes: 22 additions & 0 deletions lib/symlink.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lib

import (
"fmt"
"log"
"os"
)
Expand Down Expand Up @@ -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
}
34 changes: 34 additions & 0 deletions lib/symlink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ func main() {
os.Exit(1)
}

fmt.Printf("Terraform version %q selected\n", tfversion)
lib.AddRecent(tfversion) //add to recent file for faster lookup
lib.Install(tfversion)

Expand Down

0 comments on commit e0ff704

Please sign in to comment.