Skip to content

Commit a213c90

Browse files
Warren VeerasingamWarren Veerasingam
authored andcommitted
resolve merge
2 parents 3bce394 + c2b4856 commit a213c90

File tree

6 files changed

+170
-21
lines changed

6 files changed

+170
-21
lines changed

lib/files.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"bytes"
77
"fmt"
88
"io"
9+
"io/ioutil"
910
"log"
1011
"os"
1112
"path/filepath"
@@ -162,3 +163,41 @@ func ReadLines(path string) (lines []string, err error) {
162163
}
163164
return
164165
}
166+
167+
//IsDirEmpty : check if directory is empty (TODO UNIT TEST)
168+
func IsDirEmpty(name string) bool {
169+
170+
exist := false
171+
172+
f, err := os.Open(name)
173+
if err != nil {
174+
log.Fatal(err)
175+
}
176+
defer f.Close()
177+
178+
_, err = f.Readdirnames(1) // Or f.Readdir(1)
179+
if err == io.EOF {
180+
exist = true
181+
}
182+
return exist // Either not empty or error, suits both cases
183+
}
184+
185+
//CheckDirHasTGBin : // check binary exist (TODO UNIT TEST)
186+
func CheckDirHasTGBin(dir, prefix string) bool {
187+
188+
exist := false
189+
190+
files, err := ioutil.ReadDir(dir)
191+
if err != nil {
192+
log.Fatal(err)
193+
//return exist, err
194+
}
195+
res := []string{}
196+
for _, f := range files {
197+
if !f.IsDir() && strings.HasPrefix(f.Name(), prefix) {
198+
res = append(res, filepath.Join(dir, f.Name()))
199+
exist = true
200+
}
201+
}
202+
return exist
203+
}

lib/files_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import (
1010
"os/user"
1111
"path/filepath"
1212
"regexp"
13+
"runtime"
1314
"strings"
1415
"testing"
16+
"time"
1517

1618
"github.com/warrensbox/terraform-switcher/lib"
1719
)
@@ -289,3 +291,70 @@ func TestReadLines(t *testing.T) {
289291
cleanUp(installLocation)
290292

291293
}
294+
295+
// TestIsDirEmpty : create empty directory, check if empty
296+
func TestIsDirEmpty(t *testing.T) {
297+
298+
current := time.Now()
299+
300+
installPath := "/.terraform.versions_test/"
301+
302+
usr, errCurr := user.Current()
303+
if errCurr != nil {
304+
log.Fatal(errCurr)
305+
}
306+
installLocation := usr.HomeDir + installPath
307+
308+
test_dir := current.Format("2006-01-02")
309+
t.Logf("Create test dir: %v \n", test_dir)
310+
311+
createDirIfNotExist(installLocation)
312+
313+
createDirIfNotExist(installLocation + "/" + test_dir)
314+
315+
empty := lib.IsDirEmpty(installLocation + "/" + test_dir)
316+
317+
t.Logf("Expected directory to be empty %v [expected]", installLocation+"/"+test_dir)
318+
319+
if empty == true {
320+
t.Logf("Directory empty")
321+
} else {
322+
t.Error("Directory not empty")
323+
}
324+
325+
cleanUp(installLocation + "/" + test_dir)
326+
327+
cleanUp(installLocation)
328+
329+
}
330+
331+
// TestCheckDirHasTGBin : create tg file in directory, check if exist
332+
func TestCheckDirHasTFBin(t *testing.T) {
333+
334+
goarch := runtime.GOARCH
335+
goos := runtime.GOOS
336+
installPath := "/.terraform.versions_test/"
337+
installFile := "terraform"
338+
339+
usr, errCurr := user.Current()
340+
if errCurr != nil {
341+
log.Fatal(errCurr)
342+
}
343+
installLocation := usr.HomeDir + installPath
344+
345+
createDirIfNotExist(installLocation)
346+
347+
createFile(installLocation + installFile + "_" + goos + "_" + goarch)
348+
349+
empty := lib.CheckDirHasTGBin(installLocation, installFile)
350+
351+
t.Logf("Expected directory to have tf file %v [expected]", installLocation+installFile+"_"+goos+"_"+goarch)
352+
353+
if empty == true {
354+
t.Logf("Directory empty")
355+
} else {
356+
t.Error("Directory not empty")
357+
}
358+
359+
cleanUp(installLocation)
360+
}

lib/install.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os/user"
88
"regexp"
99
"runtime"
10-
"strings"
1110
)
1211

1312
const (
@@ -16,8 +15,6 @@ const (
1615
installVersion = "terraform_"
1716
binLocation = "/usr/local/bin/terraform"
1817
installPath = "/.terraform.versions/"
19-
macOS = "_darwin_amd64.zip"
20-
linux = "_darwin_amd64.zip"
2118
recentFile = "RECENT"
2219
)
2320

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

54-
fmt.Printf("Terraform binary path: %v \n", installedBinPath)
55-
5650
/* Create local installation directory if it does not exist */
5751
CreateDirIfNotExist(installLocation)
5852

@@ -69,12 +63,11 @@ func Install(tfversion string) {
6963

7064
/* if selected version already exist, */
7165
if fileExist {
66+
7267
/* remove current symlink if exist*/
73-
exist := CheckFileExist(installedBinPath)
68+
symlinkExist := CheckSymlink(installedBinPath)
7469

75-
if !exist {
76-
fmt.Println("Symlink does not exist")
77-
} else {
70+
if symlinkExist {
7871
RemoveSymlink(installedBinPath)
7972
}
8073

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

92-
fmt.Printf("Downloaded zipFile: %v \n", zipFile)
93-
9485
/* unzip the downloaded zipfile */
95-
files, errUnzip := Unzip(zipFile, installLocation)
86+
_, errUnzip := Unzip(zipFile, installLocation)
9687
if errUnzip != nil {
9788
fmt.Println("Unable to unzip downloaded zip file")
9889
log.Fatal(errUnzip)
9990
os.Exit(1)
10091
}
10192

102-
fmt.Println("Unzipped: " + strings.Join(files, "\n"))
103-
10493
/* rename unzipped file to terraform version name - terraform_x.x.x */
10594
RenameFile(installLocation+installFile, installLocation+installVersion+tfversion)
10695

10796
/* remove zipped file to clear clutter */
10897
RemoveFiles(installLocation + installVersion + tfversion + "_" + goos + "_" + goarch + ".zip")
10998

11099
/* remove current symlink if exist*/
111-
exist := CheckFileExist(installedBinPath)
100+
symlinkExist := CheckSymlink(installedBinPath)
112101

113-
if !exist {
114-
fmt.Println("Symlink does not exist")
115-
} else {
116-
fmt.Println("Symlink exist")
102+
if symlinkExist {
117103
RemoveSymlink(installedBinPath)
118104
}
119105

lib/symlink.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package lib
22

33
import (
4+
"fmt"
45
"log"
56
"os"
67
)
@@ -30,3 +31,24 @@ func RemoveSymlink(symlinkPath string) {
3031
}
3132
}
3233
}
34+
35+
// CheckSymlink : check file is symlink
36+
func CheckSymlink(symlinkPath string) bool {
37+
38+
//symlink := false
39+
//fmt.Println("Checking symlink")
40+
41+
fi, err := os.Lstat(symlinkPath)
42+
if err != nil {
43+
fmt.Println(err)
44+
// symlink = false
45+
return false
46+
}
47+
48+
if fi.Mode()&os.ModeSymlink != 0 {
49+
//symlink = true
50+
return true
51+
}
52+
53+
return false
54+
}

lib/symlink_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,37 @@ func TestRemoveSymlink(t *testing.T) {
8282
t.Logf("Symlink was removed %v [expected]", lnCheck)
8383
}
8484
}
85+
86+
// TestCheckSymlink : Create symlink, test if file is symlink
87+
func TestCheckSymlink(t *testing.T) {
88+
89+
testSymlinkSrc := "/test-tgshifter-src"
90+
91+
testSymlinkDest := "/test-tgshifter-dest"
92+
93+
usr, errCurr := user.Current()
94+
if errCurr != nil {
95+
log.Fatal(errCurr)
96+
}
97+
symlinkPathSrc := usr.HomeDir + testSymlinkSrc
98+
symlinkPathDest := usr.HomeDir + testSymlinkDest
99+
100+
ln, _ := os.Readlink(symlinkPathSrc)
101+
102+
if ln != symlinkPathDest {
103+
t.Log("Creating symlink")
104+
if err := os.Symlink(symlinkPathDest, symlinkPathSrc); err != nil {
105+
t.Error(err)
106+
}
107+
}
108+
109+
symlinkExist := lib.CheckSymlink(symlinkPathSrc)
110+
111+
if symlinkExist {
112+
t.Logf("Symlink does exist %v [expected]", ln)
113+
} else {
114+
t.Logf("Symlink does not exist %v [unexpected]", ln)
115+
}
116+
117+
os.Remove(symlinkPathSrc)
118+
}

main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ func main() {
9191
os.Exit(1)
9292
}
9393

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

0 commit comments

Comments
 (0)