Skip to content

Commit

Permalink
feat: add gobrew config
Browse files Browse the repository at this point in the history
  • Loading branch information
juev committed Dec 11, 2023
1 parent e9ff7bd commit b7943a4
Show file tree
Hide file tree
Showing 32 changed files with 4,502 additions and 111 deletions.
21 changes: 17 additions & 4 deletions cmd/gobrew/main.go
Expand Up @@ -74,14 +74,27 @@ func init() {
}

func main() {
homeDir, ok := os.LookupEnv("GOBREW_ROOT")
if !ok || homeDir == "" {
rootDir := os.Getenv("GOBREW_ROOT")
if rootDir == "" {
var err error
homeDir, err = os.UserHomeDir()
rootDir, err = os.UserHomeDir()
utils.CheckError(err, "failed get home directory and GOBREW_ROOT not defined")
}

gb := gobrew.NewGoBrew(homeDir)
registryPath := gobrew.DefaultRegistryPath
if p := os.Getenv("GOBREW_REGISTRY"); p != "" {
registryPath = p
}

config := gobrew.Config{
RootDir: rootDir,
RegistryPathUrl: registryPath,
GobrewDownloadUrl: gobrew.GoBrewDownloadUrl,
GobrewTags: gobrew.GoBrewTagsApi,
GobrewVersionsUrl: gobrew.GoBrewVersionsUrl,
}

gb := gobrew.NewGoBrew(config)
switch actionArg {
case "interactive", "info":
gb.Interactive(true)
Expand Down
64 changes: 41 additions & 23 deletions gobrew.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"io/fs"
"net/url"
"os"
"path/filepath"
"regexp"
Expand All @@ -18,9 +19,10 @@ import (

const (
goBrewDir string = ".gobrew"
defaultRegistryPath string = "https://go.dev/dl/"
goBrewDownloadUrl string = "https://github.com/kevincobain2000/gobrew/releases/latest/download/"
goBrewTagsApi string = "https://raw.githubusercontent.com/kevincobain2000/gobrew/json/golang-tags.json"
DefaultRegistryPath string = "https://go.dev/dl/"
GoBrewDownloadUrl string = "https://github.com/kevincobain2000/gobrew/releases/latest/download/"
GoBrewTagsApi = "https://raw.githubusercontent.com/kevincobain2000/gobrew/json/golang-tags.json"
GoBrewVersionsUrl string = "https://api.github.com/repos/kevincobain2000/gobrew/releases/latest"
)

// check GoBrew implement is Command interface
Expand All @@ -36,32 +38,48 @@ type Command interface {
Use(version string)
Prune()
Version(currentVersion string)
Upgrade(currentVersion string)
Upgrade(string)
Interactive(ask bool)
}

// GoBrew struct
type GoBrew struct {
homeDir string
installDir string
versionsDir string
currentDir string
currentBinDir string
currentGoDir string
downloadsDir string
rootDir string
installDir string
versionsDir string
currentDir string
currentBinDir string
currentGoDir string
downloadsDir string
registryPathUrl string
gobrewDownloadUrl string
gobrewTags string
gobrewVersionsUrl string
}

type Config struct {
RootDir string
RegistryPathUrl string
GobrewDownloadUrl string
GobrewTags string
GobrewVersionsUrl string
}

// NewGoBrew instance
func NewGoBrew(homeDir string) GoBrew {
installDir := filepath.Join(homeDir, goBrewDir)
func NewGoBrew(config Config) GoBrew {
installDir := filepath.Join(config.RootDir, goBrewDir)
gb := GoBrew{
homeDir: homeDir,
installDir: installDir,
versionsDir: filepath.Join(installDir, "versions"),
currentDir: filepath.Join(installDir, "current"),
currentBinDir: filepath.Join(installDir, "current", "bin"),
currentGoDir: filepath.Join(installDir, "current", "go"),
downloadsDir: filepath.Join(installDir, "downloads"),
rootDir: config.RootDir,
installDir: installDir,
versionsDir: filepath.Join(installDir, "versions"),
currentDir: filepath.Join(installDir, "current"),
currentBinDir: filepath.Join(installDir, "current", "bin"),
currentGoDir: filepath.Join(installDir, "current", "go"),
downloadsDir: filepath.Join(installDir, "downloads"),
registryPathUrl: config.RegistryPathUrl,
gobrewDownloadUrl: config.GobrewDownloadUrl,
gobrewTags: config.GobrewTags,
gobrewVersionsUrl: config.GobrewVersionsUrl,
}

return gb
Expand Down Expand Up @@ -297,7 +315,7 @@ func (gb *GoBrew) Install(version string) string {
gb.mkDirs(version)

color.Infof("==> [Info] Downloading version: %s\n", version)
gb.downloadAndExtract(defaultRegistryPath, version)
gb.downloadAndExtract(version)
gb.cleanDownloadsDir()
color.Successf("==> [Success] Downloaded version: %s\n", version)
return version
Expand Down Expand Up @@ -330,9 +348,9 @@ func (gb *GoBrew) Upgrade(currentVersion string) {

mkdirTemp, _ := os.MkdirTemp("", "gobrew")
tmpFile := filepath.Join(mkdirTemp, "gobrew"+fileExt)
url := goBrewDownloadUrl + "gobrew-" + gb.getArch() + fileExt
downloadUrl, _ := url.JoinPath(gb.gobrewDownloadUrl, "gobrew-"+gb.getArch()+fileExt)
utils.CheckError(
utils.DownloadWithProgress(url, "gobrew"+fileExt, mkdirTemp),
utils.DownloadWithProgress(downloadUrl, "gobrew"+fileExt, mkdirTemp),
"[Error] Download GoBrew failed")

source, err := os.Open(tmpFile)
Expand Down
63 changes: 46 additions & 17 deletions gobrew_test.go
@@ -1,35 +1,60 @@
package gobrew

import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func setupGobrew(t *testing.T, ts *httptest.Server) GoBrew {
tags, _ := url.JoinPath(ts.URL, "golang-tags.json")
versionUrl, _ := url.JoinPath(ts.URL, "latest")
config := Config{
RootDir: t.TempDir(),
RegistryPathUrl: ts.URL,
GobrewDownloadUrl: ts.URL,
GobrewTags: tags,
GobrewVersionsUrl: versionUrl,
}
gb := NewGoBrew(config)
return gb
}

func TestInstallAndExistVersion(t *testing.T) {
t.Parallel()
gb := NewGoBrew(t.TempDir())
gb.Install("1.19")
exists := gb.existsVersion("1.19")
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)
gb.Install("1.9")
exists := gb.existsVersion("1.9")
assert.Equal(t, true, exists)
t.Log("test finished")
}

func TestUnInstallThenNotExistVersion(t *testing.T) {
t.Parallel()
gb := NewGoBrew(t.TempDir())
gb.Uninstall("1.19")
exists := gb.existsVersion("1.19")
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)
gb.Install("1.9")
exists := gb.existsVersion("1.9")
assert.Equal(t, true, exists)
gb.Uninstall("1.9")
exists = gb.existsVersion("1.9")
assert.Equal(t, false, exists)
t.Log("test finished")
}

func TestUpgrade(t *testing.T) {
t.Parallel()
gb := NewGoBrew(t.TempDir())
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)

binaryDir := filepath.Join(gb.installDir, "bin")
_ = os.MkdirAll(binaryDir, os.ModePerm)
Expand All @@ -52,7 +77,9 @@ func TestUpgrade(t *testing.T) {

func TestDoNotUpgradeLatestVersion(t *testing.T) {
t.Skip("skipping test...needs to rewrite")
gb := NewGoBrew(t.TempDir())
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)

binaryDir := filepath.Join(gb.installDir, "bin")
_ = os.MkdirAll(binaryDir, os.ModePerm)
Expand All @@ -76,37 +103,37 @@ func TestDoNotUpgradeLatestVersion(t *testing.T) {

func TestInteractive(t *testing.T) {
t.Parallel()
gb := NewGoBrew(t.TempDir())
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)

currentVersion := gb.CurrentVersion()
latestVersion := gb.getLatestVersion()
// modVersion := gb.getModVersion()
assert.Equal(t, "None", currentVersion)
assert.NotEqual(t, currentVersion, latestVersion)

gb.Interactive(false)

currentVersion = gb.CurrentVersion()
// remove string private from currentVersion (for macOS) due to /private/var symlink issue
currentVersion = strings.Replace(currentVersion, "private", "", -1)
assert.Equal(t, currentVersion, latestVersion)

gb.Install("1.16.5") // we know, it is not latest
gb.Use("1.16.5")
currentVersion = gb.CurrentVersion()
currentVersion = strings.Replace(currentVersion, "private", "", -1)
assert.Equal(t, "1.16.5", currentVersion)
assert.NotEqual(t, currentVersion, latestVersion)

gb.Interactive(false)
currentVersion = gb.CurrentVersion()
currentVersion = strings.Replace(currentVersion, "private", "", -1)
assert.Equal(t, currentVersion, latestVersion)
t.Log("test finished")
}

func TestPrune(t *testing.T) {
t.Parallel()
gb := NewGoBrew(t.TempDir())
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)
gb.Install("1.20")
gb.Install("1.19")
gb.Use("1.19")
Expand All @@ -118,7 +145,9 @@ func TestPrune(t *testing.T) {

func TestGoBrew_CurrentVersion(t *testing.T) {
t.Parallel()
gb := NewGoBrew(t.TempDir())
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer ts.Close()
gb := setupGobrew(t, ts)
assert.Equal(t, true, gb.CurrentVersion() == "None")
gb.Install("1.19")
gb.Use("1.19")
Expand Down
14 changes: 5 additions & 9 deletions helpers.go
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -319,14 +320,10 @@ func (gb *GoBrew) getVersionDir(version string) string {
return filepath.Join(gb.versionsDir, version)
}

func (gb *GoBrew) downloadAndExtract(url, version string) {
func (gb *GoBrew) downloadAndExtract(version string) {
tarName := "go" + version + "." + gb.getArch() + tarNameExt

registryPath := url
if p := os.Getenv("GOBREW_REGISTRY"); p != "" {
registryPath = p
}
downloadURL := registryPath + tarName
downloadURL, _ := url.JoinPath(gb.registryPathUrl, tarName)
color.Infoln("==> [Info] Downloading from:", downloadURL)

dstDownloadDir := filepath.Join(gb.downloadsDir)
Expand Down Expand Up @@ -369,8 +366,7 @@ func (gb *GoBrew) changeSymblinkGo(version string) {
}

func (gb *GoBrew) getGobrewVersion() string {
url := "https://api.github.com/repos/kevincobain2000/gobrew/releases/latest"
data := doRequest(url)
data := doRequest(gb.gobrewVersionsUrl)
if len(data) == 0 {
return ""
}
Expand All @@ -385,7 +381,7 @@ func (gb *GoBrew) getGobrewVersion() string {
}

func (gb *GoBrew) getGolangVersions() (result []string) {
data := doRequest(goBrewTagsApi)
data := doRequest(gb.gobrewTags)
if len(data) == 0 {
return
}
Expand Down

0 comments on commit b7943a4

Please sign in to comment.