Skip to content

Commit

Permalink
added option to see version
Browse files Browse the repository at this point in the history
  • Loading branch information
warrensbox committed May 23, 2018
1 parent c521297 commit 8db37b1
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 99 deletions.
16 changes: 6 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
EXE := tfswitch
PKG := github.com/warren-veerasingam/terraform-switcher
#VER := $(shell git describe --tags)
VER := $(shell git ls-remote --tags git://github.com/warren-veerasingam/terraform-switcher | awk '{print $$2}'| awk -F"/" '{print $$3}' | sort -n -t. -k1,1 -k2,2 -k3,3 | tail -n 1)
PATH := build:$(PATH)
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)

$(EXE): Gopkg.lock *.go lib/*.go
go build -v -o $@ $(PKG)
go build -v -ldflags "-X main.version=$(VER)" -o $@ $(PKG)

Gopkg.lock: Gopkg.toml
dep ensure

.PHONY: release
release: $(EXE) darwin windows linux
release: $(EXE) darwin linux

# .PHONY: darwin linux
# darwin linux:
# GOOS=$@ go build -o $(EXE)-$(VER)-$@-$(GOARCH) $(PKG)
.PHONY: darwin linux
darwin linux:
GOOS=$@ go build -ldflags "-X main.version=$(VER)" -o $(EXE)-$(VER)-$@-$(GOARCH) $(PKG)

.PHONY: clean
clean:
rm -f $(EXE) $(EXE)-*-*-*

.PHONY: dist-clean
dist-clean: clean
rm -f Gopkg.lock

.PHONY: test
test: $(EXE)
mv $(EXE) build
Expand Down
194 changes: 105 additions & 89 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,124 +44,140 @@ const (
macOS = "_darwin_amd64.zip"
)

var version = "0.0.1\n"

func main() {

/* get current user */
usr, errCurr := user.Current()
if errCurr != nil {
log.Fatal(errCurr)
}
fmt.Printf("Current user: %v \n", usr.HomeDir)
args := os.Args

/* set installation location */
installLocation := usr.HomeDir + installPath
if len(os.Args) > 1 {
switch os := args[1]; os {
case "--version":
fmt.Println(version)
case "version":
fmt.Println(version)
case "-v":
fmt.Println(version)
}
} else {

/* set default binary path for terraform */
installedBinPath := binLocation
/* get current user */
usr, errCurr := user.Current()
if errCurr != nil {
log.Fatal(errCurr)
}

/* find terraform binary location if terraform is already installed*/
cmd := lib.NewCommand("terraform")
next := cmd.Find()
//existed := false
/* set installation location */
installLocation := usr.HomeDir + installPath

/* 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
}
/* set default binary path for terraform */
installedBinPath := binLocation

fmt.Printf("Terraform binary path: %v", installedBinPath)
/* find terraform binary location if terraform is already installed*/
cmd := lib.NewCommand("terraform")
next := cmd.Find()
//existed := false

/* Create local installation directory if it does not exist */
lib.CreateDirIfNotExist(installLocation)
/* 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
}

/* Get list of terraform versions from hashicorp releases */
resp, errURL := http.Get(hashiURL)
if errURL != nil {
log.Printf("Error getting url: %v", errURL)
}
defer resp.Body.Close()
fmt.Printf("Terraform binary path: %v", installedBinPath)

body, errBody := ioutil.ReadAll(resp.Body)
if errBody != nil {
log.Printf("Error reading body: %v", errBody)
return
}
/* Create local installation directory if it does not exist */
lib.CreateDirIfNotExist(installLocation)

bodyString := string(body)
result := strings.Split(bodyString, "\n")
/* Get list of terraform versions from hashicorp releases */
resp, errURL := http.Get(hashiURL)
if errURL != nil {
log.Printf("Error getting url: %v", errURL)
}
defer resp.Body.Close()

body, errBody := ioutil.ReadAll(resp.Body)
if errBody != nil {
log.Printf("Error reading body: %v", errBody)
return
}

bodyString := string(body)
result := strings.Split(bodyString, "\n")

var tfVersionList tfVersionList
var tfVersionList tfVersionList

for i := range result {
//getting versions from body; should return match /X.X.X/
r, _ := regexp.Compile(`\/(\d+)(\.)(\d+)(\.)(\d+)\/`)
for i := range result {
//getting versions from body; should return match /X.X.X/
r, _ := regexp.Compile(`\/(\d+)(\.)(\d+)(\.)(\d+)\/`)

if r.MatchString(result[i]) {
str := r.FindString(result[i])
trimstr := strings.Trim(str, "/") //remove "/" from /X.X.X/
tfVersionList.tflist = append(tfVersionList.tflist, trimstr)
if r.MatchString(result[i]) {
str := r.FindString(result[i])
trimstr := strings.Trim(str, "/") //remove "/" from /X.X.X/
tfVersionList.tflist = append(tfVersionList.tflist, trimstr)
}
}
}

/* prompt user to select version of terraform */
prompt := promptui.Select{
Label: "Select Terraform version",
Items: tfVersionList.tflist,
}
/* prompt user to select version of terraform */
prompt := promptui.Select{
Label: "Select Terraform version",
Items: tfVersionList.tflist,
}

_, version, errPrompt := prompt.Run()
_, version, errPrompt := prompt.Run()

if errPrompt != nil {
log.Printf("Prompt failed %v\n", errPrompt)
os.Exit(1)
}
if errPrompt != nil {
log.Printf("Prompt failed %v\n", errPrompt)
os.Exit(1)
}

fmt.Printf("Terraform version %q selected\n", version)
fmt.Printf("Terraform version %q selected\n", version)

/* check if selected version already downloaded */
fileExist := lib.CheckFileExist(installLocation + installVersion + version)
/* check if selected version already downloaded */
fileExist := lib.CheckFileExist(installLocation + installVersion + version)

/* if selected version already exist, */
if fileExist {
/* remove current symlink and set new symlink to desired version */
lib.RemoveSymlink(installedBinPath)
/* if selected version already exist, */
if fileExist {
/* remove current symlink and set new symlink to desired version */
lib.RemoveSymlink(installedBinPath)

/* set symlink to desired version */
lib.CreateSymlink(installLocation+installVersion+version, installedBinPath)
fmt.Printf("Swicthed terraform to version %q \n", version)
os.Exit(0)
}
/* set symlink to desired version */
lib.CreateSymlink(installLocation+installVersion+version, installedBinPath)
fmt.Printf("Swicthed terraform to version %q \n", version)
os.Exit(0)
}

/* if selected version already exist, */
/* proceed to download it from the hashicorp release page */
url := hashiURL + version + "/" + installVersion + version + macOS
zipFile, _ := lib.DownloadFromURL(installLocation, url)
/* if selected version already exist, */
/* proceed to download it from the hashicorp release page */
url := hashiURL + version + "/" + installVersion + version + macOS
zipFile, _ := lib.DownloadFromURL(installLocation, url)

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

/* unzip the downloaded zipfile */
files, errUnzip := lib.Unzip(zipFile, installLocation)
if errUnzip != nil {
fmt.Println("Unable to unzip downloaded zip file")
log.Fatal(errUnzip)
os.Exit(1)
}
/* unzip the downloaded zipfile */
files, errUnzip := lib.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"))
fmt.Println("Unzipped: " + strings.Join(files, "\n"))

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

/* remove zipped file to clear clutter */
lib.RemoveFiles(installLocation + installVersion + version + macOS)
/* remove zipped file to clear clutter */
lib.RemoveFiles(installLocation + installVersion + version + macOS)

/* remove current symlink and set new symlink to desired version */
lib.RemoveSymlink(installedBinPath)
/* remove current symlink and set new symlink to desired version */
lib.RemoveSymlink(installedBinPath)

/* set symlink to desired version */
lib.CreateSymlink(installLocation+installVersion+version, installedBinPath)
fmt.Printf("Swicthed terraform to version %q \n", version)
/* set symlink to desired version */
lib.CreateSymlink(installLocation+installVersion+version, installedBinPath)
fmt.Printf("Swicthed terraform to version %q \n", version)
}
os.Exit(0)

}

0 comments on commit 8db37b1

Please sign in to comment.