Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ builds:
- linux
- windows
goarch:
- 386
- '386'
- amd64
- arm
- arm64
ignore:
- goos: windows
goarch: arm
goarch: arm
checksum:
name_template: '{{ .ProjectName }}_{{.Env.RELEASE_VERSION}}_checksums.txt'
changelog:
Expand All @@ -32,7 +32,7 @@ archives:
replacements:
darwin: darwin
linux: linux
386: 386
386: '386'
amd64: amd64
format_overrides:
- goos: windows
Expand Down
34 changes: 26 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
EXE := tgswitch
PKG := github.com/warrensbox/tgswitch
VER := $(shell { git ls-remote --tags . 2>/dev/null || git ls-remote --tags git@github.com:warrensbox/tgswitch.git; } | awk '{if ($$2 ~ "\\^\\{\\}$$") next; print vers[split($$2,vers,"\\/")]}' | sort -n -t. -k1,1 -k2,2 -k3,3 | tail -1)
VER := $(shell { git ls-remote --tags . 2>/dev/null || git ls-remote --tags git@github.com:warrensbox/tgswitch.git; } | awk '{if ($$2 ~ "\\^\\{\\}$$") next; print vers[split($$2,vers,"\\/")]}' | sort -n -t. -k1,1 -k2,2 -k3,3 | tail -1)
PATH := build:$(PATH)
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)

GOMOD = on
DIR = ./build
GOOSX = darwin
GOOSLINUX = linux
OSXBIN = $(DIR)/$(EXE)-$(VER)-$(GOOSX)-$(GOARCH)
LINUXBIN = $(DIR)/$(EXE)-$(VER)-$(GOOSLINUX)-$(GOARCH)

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

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

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

.PHONY: darwin
darwin: $(OSXBIN)
chmod +x $(OSXBIN)

.PHONY: $(LINUXBIN)
$(LINUXBIN):
GO111MODULE=$(GOMOD) GOOS=$(GOOSLINUX) go build -ldflags "-X main.version=$(VER)" -o $(LINUXBIN) $(PKG)

.PHONY: linux
linux: $(LINUXBIN)
chmod +x $(LINUXBIN)

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

.PHONY: test
test: $(EXE)
mkdir -p build
mv $(EXE) build
mkdir -vp $(DIR)
mv $(EXE) $(DIR)
go test -v ./...

.PHONY: install
install: $(EXE)
mkdir -p ~/bin
mkdir -vp ~/bin
mv $(EXE) ~/bin

.PHONY: docs
Expand All @@ -37,4 +56,3 @@ docs:
.PHONY: version
version:
@echo $(VER)

42 changes: 37 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
module github.com/warrensbox/tgswitch

go 1.16
go 1.19

require (
github.com/hashicorp/go-version v1.5.0 // indirect
github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80 // indirect
github.com/google/go-github/v49 v49.1.0
github.com/hashicorp/go-version v1.5.0
github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80
github.com/manifoldco/promptui v0.9.0
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/pborman/getopt v0.0.0-20190130022237-7402d34a12a8
github.com/spf13/viper v1.12.0 // indirect
github.com/spf13/viper v1.12.0
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a
)

require (
github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg v1.0.0 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.3.0 // indirect
github.com/zclconf/go-cty v1.0.0 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)
399 changes: 23 additions & 376 deletions go.sum

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion lib/dir_perm.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//go:build !windows
// +build !windows

package lib

import "golang.org/x/sys/unix"
import (
"golang.org/x/sys/unix"
)

//Check if user has permission to directory :
//dir=path to file
Expand Down
20 changes: 12 additions & 8 deletions lib/download.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package lib

import (
"context"
"fmt"
"io"
"net/http"
"os"
"strings"

"github.com/google/go-github/v49/github"
)

// DownloadFromURL : Downloads the binary from the source url
func DownloadFromURL(installLocation string, url string) (string, error) {
func DownloadFromURL(ctx context.Context, ghClient *github.Client, installLocation string, asset *github.ReleaseAsset) (string, error) {

tokens := strings.Split(url, "/")
fileName := tokens[len(tokens)-1]
fileName := *asset.Name
url := *asset.BrowserDownloadURL
repoOwner := ctx.Value("repoOwner").(string)
repoName := ctx.Value("repoName").(string)
fmt.Println("Downloading", url, "to", fileName)
fmt.Println("Downloading ...")

Expand All @@ -23,16 +27,16 @@ func DownloadFromURL(installLocation string, url string) (string, error) {
}
defer output.Close()

response, err := http.Get(url)
rc, _, err := ghClient.Repositories.DownloadReleaseAsset(ctx, repoOwner, repoName, *asset.ID, http.DefaultClient)
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
return "", err
}
defer response.Body.Close()
defer rc.Close()

n, errCopy := io.Copy(output, response.Body)
n, errCopy := io.Copy(output, rc)
if errCopy != nil {
fmt.Println("Error while downloading", url, "-", errCopy)
fmt.Println("Error while writing to disk", url, "-", errCopy)
return "", errCopy
}

Expand Down
37 changes: 27 additions & 10 deletions lib/download_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lib_test

import (
"context"
"fmt"
"log"
"net/url"
Expand All @@ -9,18 +10,29 @@ import (
"runtime"
"testing"

"github.com/google/go-github/v49/github"
lib "github.com/warrensbox/tgswitch/lib"
)

const (
repoOwner string = "gruntwork-io"
repoName string = "terragrunt"
)

// TestDownloadFromURL_FileNameMatch : Check expected filename exist when downloaded
func TestDownloadFromURL_FileNameMatch(t *testing.T) {

gruntURL := "https://github.com/gruntwork-io/terragrunt/releases/download/"
installVersion := "terragrunt_"
installPath := "/.terragrunt.versions_test/"
goarch := runtime.GOARCH
goos := runtime.GOOS

ctx := context.Background()
ctx = context.WithValue(ctx, "repoOwner", repoOwner)
ctx = context.WithValue(ctx, "repoName", repoName)

ghClient := github.NewClient(nil)

// get current user
usr, errCurr := user.Current()
if errCurr != nil {
Expand All @@ -42,10 +54,10 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {

/* test download lowest terragrunt version */
lowestVersion := "0.13.9"
asset := lib.FindMatchingReleaseAsset(ctx, ghClient, lowestVersion)

url := gruntURL + "v" + lowestVersion + "/" + installVersion + goos + "_" + goarch
expectedFile := usr.HomeDir + installPath + installVersion + goos + "_" + goarch
installedFile, _ := lib.DownloadFromURL(installLocation, url)
installedFile, _ := lib.DownloadFromURL(ctx, ghClient, installLocation, asset)

if installedFile == expectedFile {
t.Logf("Expected file %v", expectedFile)
Expand All @@ -59,10 +71,10 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {

/* test download latest terragrunt version */
latestVersion := "0.14.11"
asset = lib.FindMatchingReleaseAsset(ctx, ghClient, latestVersion)

url = gruntURL + "v" + latestVersion + "/" + installVersion + goos + "_" + goarch
expectedFile = usr.HomeDir + installPath + installVersion + goos + "_" + goarch
installedFile, _ = lib.DownloadFromURL(installLocation, url)
installedFile, _ = lib.DownloadFromURL(ctx, ghClient, installLocation, asset)

if installedFile == expectedFile {
t.Logf("Expected file name %v", expectedFile)
Expand All @@ -80,12 +92,17 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {
// TestDownloadFromURL_FileExist : Check expected file exist when downloaded
func TestDownloadFromURL_FileExist(t *testing.T) {

gruntURL := "https://github.com/gruntwork-io/terragrunt/releases/download/"
installVersion := "terragrunt_"
installPath := "/.terragrunt.versions_test/"
goarch := runtime.GOARCH
goos := runtime.GOOS

ctx := context.Background()
ctx = context.WithValue(ctx, "repoOwner", repoOwner)
ctx = context.WithValue(ctx, "repoName", repoName)

ghClient := github.NewClient(nil)

// get current user
usr, errCurr := user.Current()
if errCurr != nil {
Expand All @@ -107,10 +124,10 @@ func TestDownloadFromURL_FileExist(t *testing.T) {

/* test download lowest terragrunt version */
lowestVersion := "0.13.9"
asset := lib.FindMatchingReleaseAsset(ctx, ghClient, lowestVersion)

url := gruntURL + "v" + lowestVersion + "/" + installVersion + goos + "_" + goarch
expectedFile := usr.HomeDir + installPath + installVersion + goos + "_" + goarch
installedFile, _ := lib.DownloadFromURL(installLocation, url)
installedFile, _ := lib.DownloadFromURL(ctx, ghClient, installLocation, asset)

if checkFileExist(expectedFile) {
t.Logf("Expected file %v", expectedFile)
Expand All @@ -124,10 +141,10 @@ func TestDownloadFromURL_FileExist(t *testing.T) {

/* test download latest terragrunt version */
latestVersion := "0.14.11"
asset = lib.FindMatchingReleaseAsset(ctx, ghClient, latestVersion)

url = gruntURL + "v" + latestVersion + "/" + installVersion + goos + "_" + goarch
expectedFile = usr.HomeDir + installPath + installVersion + goos + "_" + goarch
installedFile, _ = lib.DownloadFromURL(installLocation, url)
installedFile, _ = lib.DownloadFromURL(ctx, ghClient, installLocation, asset)

if checkFileExist(expectedFile) {
t.Logf("Expected file %v", expectedFile)
Expand Down
Loading