diff --git a/docs/_site/index.md b/docs/_site/index.md
index d97d1e8f..2450c92f 100644
--- a/docs/_site/index.md
+++ b/docs/_site/index.md
@@ -82,11 +82,12 @@ version = "0.11.3"
2. For example, `echo "0.10.5" >> .tfswitchrc` for version 0.10.5 of terraform.
3. Run the command `tfswitch` in the same directory as your `.tfswitchrc`.
+*Instead of a `.tfswitchrc` file, a `.terraform-version` file may be used for compatibility with [`tfenv`](https://github.com/tfutils/tfenv#terraform-version-file) and other tools which use it*
**Automatically switch with bash**
Add the following to the end of your `~/.bashrc` file:
-(Use either `.tfswitchrc` or `.tfswitch.toml`)
+(Use either `.tfswitchrc` or `.tfswitch.toml` or `.terraform-version`)
```
cdtfswitch(){
@@ -130,6 +131,20 @@ cd(){
}
```
+### Jenkins setup
+
+
+```
+#!/bin/bash
+
+echo "Installing tfswitch locally"
+wget https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh
+chmod 755 install.sh
+./install.sh -b bin-directory
+
+./bin-directory/tfswitch
+```
+
## Issues
diff --git a/docs/index.md b/docs/index.md
index d97d1e8f..2450c92f 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -82,11 +82,12 @@ version = "0.11.3"
2. For example, `echo "0.10.5" >> .tfswitchrc` for version 0.10.5 of terraform.
3. Run the command `tfswitch` in the same directory as your `.tfswitchrc`.
+*Instead of a `.tfswitchrc` file, a `.terraform-version` file may be used for compatibility with [`tfenv`](https://github.com/tfutils/tfenv#terraform-version-file) and other tools which use it*
**Automatically switch with bash**
Add the following to the end of your `~/.bashrc` file:
-(Use either `.tfswitchrc` or `.tfswitch.toml`)
+(Use either `.tfswitchrc` or `.tfswitch.toml` or `.terraform-version`)
```
cdtfswitch(){
@@ -130,6 +131,20 @@ cd(){
}
```
+### Jenkins setup
+
+
+```
+#!/bin/bash
+
+echo "Installing tfswitch locally"
+wget https://raw.githubusercontent.com/warrensbox/terraform-switcher/release/install.sh
+chmod 755 install.sh
+./install.sh -b bin-directory
+
+./bin-directory/tfswitch
+```
+
## Issues
diff --git a/lib/install.go b/lib/install.go
index 6d416a43..c3794d29 100644
--- a/lib/install.go
+++ b/lib/install.go
@@ -12,27 +12,21 @@ const (
hashiURL = "https://releases.hashicorp.com/terraform/"
installFile = "terraform"
installVersion = "terraform_"
- //binLocation = "/usr/local/bin/terraform"
- installPath = "/.terraform.versions/"
- recentFile = "RECENT"
+ installPath = "/.terraform.versions/"
+ recentFile = "RECENT"
)
var (
installLocation = "/tmp"
- //installedBinPath = "/tmp"
)
-func init() {
- /* get current user */
- usr, errCurr := user.Current()
- if errCurr != nil {
- log.Fatal(errCurr)
- }
-
- /* set installation location */
- installLocation = usr.HomeDir + installPath
+// initialize : removes existing symlink to terraform binary
+func initialize() {
- /* set default binary path for terraform */
+ /* Step 1 */
+ /* initilize default binary path for terraform */
+ /* assumes that terraform is installed here */
+ /* we will find the terraform path instalation later and replace this variable with the correct installed bin path */
installedBinPath := "/usr/local/bin/terraform"
/* find terraform binary location if terraform is already installed*/
@@ -45,16 +39,33 @@ func init() {
installedBinPath = path
}
- /* remove current symlink if exist*/
+ /* check if current symlink to terraform binary exist */
symlinkExist := CheckSymlink(installedBinPath)
+ /* remove current symlink if exist*/
if symlinkExist {
RemoveSymlink(installedBinPath)
}
+}
+
+// getInstallLocation : get location where the terraform binary will be installed,
+// will create a directory in the home location if it does not exist
+func getInstallLocation() string {
+ /* get current user */
+ usr, errCurr := user.Current()
+ if errCurr != nil {
+ log.Fatal(errCurr)
+ }
+
+ /* set installation location */
+ installLocation = usr.HomeDir + installPath
+
/* Create local installation directory if it does not exist */
CreateDirIfNotExist(installLocation)
+ return installLocation
+
}
//Install : Install the provided version in the argument
@@ -65,6 +76,18 @@ 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
+ installLocation = getInstallLocation() //get installation location - this is where we will put our terraform binary file
+
goarch := runtime.GOARCH
goos := runtime.GOOS
@@ -130,6 +153,8 @@ func Install(tfversion string, binPath string) {
// AddRecent : add to recent file
func AddRecent(requestedVersion string) {
+ installLocation = getInstallLocation() //get installation location - this is where we will put our terraform binary file
+
fileExist := CheckFileExist(installLocation + recentFile)
if fileExist {
lines, errRead := ReadLines(installLocation + recentFile)
@@ -170,6 +195,8 @@ func AddRecent(requestedVersion string) {
// GetRecentVersions : get recent version from file
func GetRecentVersions() ([]string, error) {
+ installLocation = getInstallLocation() //get installation location - this is where we will put our terraform binary file
+
fileExist := CheckFileExist(installLocation + recentFile)
if fileExist {
@@ -205,5 +232,8 @@ func GetRecentVersions() ([]string, error) {
//CreateRecentFile : create a recent file
func CreateRecentFile(requestedVersion string) {
+
+ installLocation = getInstallLocation() //get installation location - this is where we will put our terraform binary file
+
WriteLines([]string{requestedVersion}, installLocation+recentFile)
}
diff --git a/lib/symlink.go b/lib/symlink.go
index f0df72c4..ba7f8291 100644
--- a/lib/symlink.go
+++ b/lib/symlink.go
@@ -36,6 +36,7 @@ func RemoveSymlink(symlinkPath string) {
os.Exit(1)
} else {
errRemove := os.Remove(symlinkPath)
+
if errRemove != nil {
log.Fatalf(`
Unable to remove symlink.
diff --git a/main.go b/main.go
index c1498d8f..445b0f56 100644
--- a/main.go
+++ b/main.go
@@ -107,14 +107,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
@@ -130,7 +123,7 @@ func main() {
}
} else if _, err := os.Stat(rcfile); err == nil && len(args) == 0 { //if there is a .tfswitchrc file, and no commmand line arguments
- fmt.Printf("Reading required terraform version %s ", rcFilename)
+ fmt.Printf("Reading required terraform version %s \n", rcFilename)
fileContents, err := ioutil.ReadFile(rcfile)
if err != nil {
@@ -147,7 +140,7 @@ func main() {
os.Exit(1)
}
} else if _, err := os.Stat(tfvfile); err == nil && len(args) == 0 { //if there is a .terraform-version file, and no command line arguments
- fmt.Printf("Reading required terraform version %s ", tfvFilename)
+ fmt.Printf("Reading required terraform version %s \n", tfvFilename)
fileContents, err := ioutil.ReadFile(tfvfile)
if err != nil {