Skip to content

Commit

Permalink
Merge pull request #174 from Shuliyey/wheelchair/improve-path-join-su…
Browse files Browse the repository at this point in the history
…pport-cross-platform

♿  changes to make tfswitch work on windows, fix some test 🐛 bugs, ♻️ refactor code, add 🚀 ci on `windows/osx`
  • Loading branch information
warrensbox authored Jun 29, 2021
2 parents 008b085 + 309e0ba commit 5615fec
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 108 deletions.
55 changes: 52 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,55 @@ install: true
go:
- "1.16"

script:
- make test
- make clean
jobs:
include:
- os: linux
cache:
directories:
- $HOME/go
script:
- make test
- make clean
- os: osx
cache:
directories:
- $HOME/go
script:
- make test
- make clean
- os: windows
cache:
directories:
- $HOME/go
- $HOME/AppData/Local/Temp/chocolatey
- /C/tools/msys64
before_install:
- |-
case $TRAVIS_OS_NAME in
windows)
[[ ! -f C:/tools/msys64/msys2_shell.cmd ]] && rm -rf C:/tools/msys64
choco uninstall -y mingw
choco upgrade --no-progress -y msys2
export msys2='cmd //C RefreshEnv.cmd '
export msys2+='& set MSYS=winsymlinks:nativestrict '
export msys2+='& C:\\tools\\msys64\\msys2_shell.cmd -defterm -no-start'
export mingw64="$msys2 -mingw64 -full-path -here -c "\"\$@"\" --"
export msys2+=" -msys2 -c "\"\$@"\" --"
$msys2 pacman --sync --noconfirm --needed mingw-w64-x86_64-toolchain
## Install more MSYS2 packages from https://packages.msys2.org/base here
taskkill //IM gpg-agent.exe //F # https://travis-ci.community/t/4967
export PATH=/C/tools/msys64/mingw64/bin:$PATH
export MAKE=mingw32-make # so that Autotools can find it
;;
esac
before_cache:
- |-
case $TRAVIS_OS_NAME in
windows)
# https://unix.stackexchange.com/a/137322/107554
$msys2 pacman --sync --clean --noconfirm
;;
esac
script:
- $MAKE test
- $MAKE clean
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ The most recently selected versions are presented at the top of the dropdown.
2. For example, `tfswitch 0.10.5` for version 0.10.5 of terraform.
3. Hit **Enter** to switch.

**NOTE** for windows host `tfswitch` need to be run under `Administrator` mode, and `$HOME/.tfswitch.toml` with `bin` must be defined (with a valid path) as minimum, below is an example for `$HOME/.tfswitch.toml` on windows

```toml
bin = "C:\\Users\\<%USRNAME%>\\bin\\terraform.exe"
```

### See all versions including beta, alpha and release candidates(rc)
<img src="https://s3.us-east-2.amazonaws.com/kepler-images/warrensbox/tfswitch/tfswitch-v5.gif#1" alt="drawing" style="width: 370px;"/>

Expand Down
1 change: 0 additions & 1 deletion lib/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func cleanUp(path string) {
func removeFiles(src string) {
files, err := filepath.Glob(src)
if err != nil {

panic(err)
}
for _, f := range files {
Expand Down
8 changes: 5 additions & 3 deletions lib/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"
"os"
"path/filepath"
"strings"
)

Expand All @@ -29,9 +30,10 @@ func DownloadFromURL(installLocation string, url string) (string, error) {
return "", fmt.Errorf("Unable to download from %s\nPlease download manually from https://releases.hashicorp.com/terraform/", url)
}

output, err := os.Create(installLocation + fileName)
zipFile := filepath.Join(installLocation, fileName)
output, err := os.Create(zipFile)
if err != nil {
fmt.Println("Error while creating", installLocation+fileName, "-", err)
fmt.Println("Error while creating", zipFile, "-", err)
return "", err
}
defer output.Close()
Expand All @@ -43,5 +45,5 @@ func DownloadFromURL(installLocation string, url string) (string, error) {
}

fmt.Println(n, "bytes downloaded.")
return installLocation + fileName, nil
return zipFile, nil
}
17 changes: 9 additions & 8 deletions lib/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"
"os"
"os/user"
"path/filepath"
"testing"

lib "github.com/warrensbox/terraform-switcher/lib"
Expand All @@ -26,7 +27,7 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {
}

fmt.Printf("Current user: %v \n", usr.HomeDir)
installLocation := usr.HomeDir + installPath
installLocation := filepath.Join(usr.HomeDir, installPath)

// create /.terraform.versions_test/ directory to store code
if _, err := os.Stat(installLocation); os.IsNotExist(err) {
Expand All @@ -42,7 +43,7 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {
lowestVersion := "0.1.0"

url := hashiURL + lowestVersion + "/" + installVersion + lowestVersion + macOS
expectedFile := usr.HomeDir + installPath + installVersion + lowestVersion + macOS
expectedFile := filepath.Join(usr.HomeDir, installPath, installVersion+lowestVersion+macOS)
installedFile, errDownload := lib.DownloadFromURL(installLocation, url)

if errDownload != nil {
Expand All @@ -64,7 +65,7 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {
latestVersion := "0.11.7"

url = hashiURL + latestVersion + "/" + installVersion + latestVersion + macOS
expectedFile = usr.HomeDir + installPath + installVersion + latestVersion + macOS
expectedFile = filepath.Join(usr.HomeDir, installPath, installVersion+latestVersion+macOS)
installedFile, errDownload = lib.DownloadFromURL(installLocation, url)

if errDownload != nil {
Expand Down Expand Up @@ -101,7 +102,7 @@ func TestDownloadFromURL_FileExist(t *testing.T) {
}

fmt.Printf("Current user: %v \n", usr.HomeDir)
installLocation := usr.HomeDir + installPath
installLocation := filepath.Join(usr.HomeDir, installPath)

// create /.terraform.versions_test/ directory to store code
if _, err := os.Stat(installLocation); os.IsNotExist(err) {
Expand All @@ -117,7 +118,7 @@ func TestDownloadFromURL_FileExist(t *testing.T) {
lowestVersion := "0.1.0"

url := hashiURL + lowestVersion + "/" + installVersion + lowestVersion + macOS
expectedFile := usr.HomeDir + installPath + installVersion + lowestVersion + macOS
expectedFile := filepath.Join(usr.HomeDir, installPath, installVersion+lowestVersion+macOS)
installedFile, errDownload := lib.DownloadFromURL(installLocation, url)

if errDownload != nil {
Expand All @@ -139,7 +140,7 @@ func TestDownloadFromURL_FileExist(t *testing.T) {
latestVersion := "0.11.7"

url = hashiURL + latestVersion + "/" + installVersion + latestVersion + macOS
expectedFile = usr.HomeDir + installPath + installVersion + latestVersion + macOS
expectedFile = filepath.Join(usr.HomeDir, installPath, installVersion+latestVersion+macOS)
installFile, errDownload = lib.DownloadFromURL(installLocation, url)

if errDownload != nil {
Expand Down Expand Up @@ -176,7 +177,7 @@ func TestInvalidURL(t *testing.T) {
}

fmt.Printf("Current user: %v \n", usr.HomeDir)
installLocation := usr.HomeDir + installPath
installLocation := filepath.Join(usr.HomeDir, installPath)

// create /.terraform.versions_test/ directory to store code
if _, err := os.Stat(installLocation); os.IsNotExist(err) {
Expand All @@ -189,7 +190,7 @@ func TestInvalidURL(t *testing.T) {
}

url := hashiURL + invalidVersion + "/" + installVersion + invalidVersion + macOS
//expectedFile := usr.HomeDir + installPath + installVersion + invalidVersion + macOS
//expectedFile :=filepath.Join(usr.HomeDir, installPath, installVersion + invalidVersion + macOS)
_, errDownload := lib.DownloadFromURL(installLocation, url)

if errDownload != nil {
Expand Down
Loading

0 comments on commit 5615fec

Please sign in to comment.