Skip to content

Commit b7943a4

Browse files
committed
feat: add gobrew config
1 parent e9ff7bd commit b7943a4

32 files changed

+4502
-111
lines changed

cmd/gobrew/main.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,27 @@ func init() {
7474
}
7575

7676
func main() {
77-
homeDir, ok := os.LookupEnv("GOBREW_ROOT")
78-
if !ok || homeDir == "" {
77+
rootDir := os.Getenv("GOBREW_ROOT")
78+
if rootDir == "" {
7979
var err error
80-
homeDir, err = os.UserHomeDir()
80+
rootDir, err = os.UserHomeDir()
8181
utils.CheckError(err, "failed get home directory and GOBREW_ROOT not defined")
8282
}
8383

84-
gb := gobrew.NewGoBrew(homeDir)
84+
registryPath := gobrew.DefaultRegistryPath
85+
if p := os.Getenv("GOBREW_REGISTRY"); p != "" {
86+
registryPath = p
87+
}
88+
89+
config := gobrew.Config{
90+
RootDir: rootDir,
91+
RegistryPathUrl: registryPath,
92+
GobrewDownloadUrl: gobrew.GoBrewDownloadUrl,
93+
GobrewTags: gobrew.GoBrewTagsApi,
94+
GobrewVersionsUrl: gobrew.GoBrewVersionsUrl,
95+
}
96+
97+
gb := gobrew.NewGoBrew(config)
8598
switch actionArg {
8699
case "interactive", "info":
87100
gb.Interactive(true)

gobrew.go

+41-23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"io/fs"
7+
"net/url"
78
"os"
89
"path/filepath"
910
"regexp"
@@ -18,9 +19,10 @@ import (
1819

1920
const (
2021
goBrewDir string = ".gobrew"
21-
defaultRegistryPath string = "https://go.dev/dl/"
22-
goBrewDownloadUrl string = "https://github.com/kevincobain2000/gobrew/releases/latest/download/"
23-
goBrewTagsApi string = "https://raw.githubusercontent.com/kevincobain2000/gobrew/json/golang-tags.json"
22+
DefaultRegistryPath string = "https://go.dev/dl/"
23+
GoBrewDownloadUrl string = "https://github.com/kevincobain2000/gobrew/releases/latest/download/"
24+
GoBrewTagsApi = "https://raw.githubusercontent.com/kevincobain2000/gobrew/json/golang-tags.json"
25+
GoBrewVersionsUrl string = "https://api.github.com/repos/kevincobain2000/gobrew/releases/latest"
2426
)
2527

2628
// check GoBrew implement is Command interface
@@ -36,32 +38,48 @@ type Command interface {
3638
Use(version string)
3739
Prune()
3840
Version(currentVersion string)
39-
Upgrade(currentVersion string)
41+
Upgrade(string)
4042
Interactive(ask bool)
4143
}
4244

4345
// GoBrew struct
4446
type GoBrew struct {
45-
homeDir string
46-
installDir string
47-
versionsDir string
48-
currentDir string
49-
currentBinDir string
50-
currentGoDir string
51-
downloadsDir string
47+
rootDir string
48+
installDir string
49+
versionsDir string
50+
currentDir string
51+
currentBinDir string
52+
currentGoDir string
53+
downloadsDir string
54+
registryPathUrl string
55+
gobrewDownloadUrl string
56+
gobrewTags string
57+
gobrewVersionsUrl string
58+
}
59+
60+
type Config struct {
61+
RootDir string
62+
RegistryPathUrl string
63+
GobrewDownloadUrl string
64+
GobrewTags string
65+
GobrewVersionsUrl string
5266
}
5367

5468
// NewGoBrew instance
55-
func NewGoBrew(homeDir string) GoBrew {
56-
installDir := filepath.Join(homeDir, goBrewDir)
69+
func NewGoBrew(config Config) GoBrew {
70+
installDir := filepath.Join(config.RootDir, goBrewDir)
5771
gb := GoBrew{
58-
homeDir: homeDir,
59-
installDir: installDir,
60-
versionsDir: filepath.Join(installDir, "versions"),
61-
currentDir: filepath.Join(installDir, "current"),
62-
currentBinDir: filepath.Join(installDir, "current", "bin"),
63-
currentGoDir: filepath.Join(installDir, "current", "go"),
64-
downloadsDir: filepath.Join(installDir, "downloads"),
72+
rootDir: config.RootDir,
73+
installDir: installDir,
74+
versionsDir: filepath.Join(installDir, "versions"),
75+
currentDir: filepath.Join(installDir, "current"),
76+
currentBinDir: filepath.Join(installDir, "current", "bin"),
77+
currentGoDir: filepath.Join(installDir, "current", "go"),
78+
downloadsDir: filepath.Join(installDir, "downloads"),
79+
registryPathUrl: config.RegistryPathUrl,
80+
gobrewDownloadUrl: config.GobrewDownloadUrl,
81+
gobrewTags: config.GobrewTags,
82+
gobrewVersionsUrl: config.GobrewVersionsUrl,
6583
}
6684

6785
return gb
@@ -297,7 +315,7 @@ func (gb *GoBrew) Install(version string) string {
297315
gb.mkDirs(version)
298316

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

331349
mkdirTemp, _ := os.MkdirTemp("", "gobrew")
332350
tmpFile := filepath.Join(mkdirTemp, "gobrew"+fileExt)
333-
url := goBrewDownloadUrl + "gobrew-" + gb.getArch() + fileExt
351+
downloadUrl, _ := url.JoinPath(gb.gobrewDownloadUrl, "gobrew-"+gb.getArch()+fileExt)
334352
utils.CheckError(
335-
utils.DownloadWithProgress(url, "gobrew"+fileExt, mkdirTemp),
353+
utils.DownloadWithProgress(downloadUrl, "gobrew"+fileExt, mkdirTemp),
336354
"[Error] Download GoBrew failed")
337355

338356
source, err := os.Open(tmpFile)

gobrew_test.go

+46-17
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,60 @@
11
package gobrew
22

33
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"net/url"
47
"os"
58
"path/filepath"
6-
"strings"
79
"testing"
810

911
"github.com/stretchr/testify/assert"
1012
)
1113

14+
func setupGobrew(t *testing.T, ts *httptest.Server) GoBrew {
15+
tags, _ := url.JoinPath(ts.URL, "golang-tags.json")
16+
versionUrl, _ := url.JoinPath(ts.URL, "latest")
17+
config := Config{
18+
RootDir: t.TempDir(),
19+
RegistryPathUrl: ts.URL,
20+
GobrewDownloadUrl: ts.URL,
21+
GobrewTags: tags,
22+
GobrewVersionsUrl: versionUrl,
23+
}
24+
gb := NewGoBrew(config)
25+
return gb
26+
}
27+
1228
func TestInstallAndExistVersion(t *testing.T) {
1329
t.Parallel()
14-
gb := NewGoBrew(t.TempDir())
15-
gb.Install("1.19")
16-
exists := gb.existsVersion("1.19")
30+
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
31+
defer ts.Close()
32+
gb := setupGobrew(t, ts)
33+
gb.Install("1.9")
34+
exists := gb.existsVersion("1.9")
1735
assert.Equal(t, true, exists)
1836
t.Log("test finished")
1937
}
2038

2139
func TestUnInstallThenNotExistVersion(t *testing.T) {
2240
t.Parallel()
23-
gb := NewGoBrew(t.TempDir())
24-
gb.Uninstall("1.19")
25-
exists := gb.existsVersion("1.19")
41+
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
42+
defer ts.Close()
43+
gb := setupGobrew(t, ts)
44+
gb.Install("1.9")
45+
exists := gb.existsVersion("1.9")
46+
assert.Equal(t, true, exists)
47+
gb.Uninstall("1.9")
48+
exists = gb.existsVersion("1.9")
2649
assert.Equal(t, false, exists)
2750
t.Log("test finished")
2851
}
2952

3053
func TestUpgrade(t *testing.T) {
3154
t.Parallel()
32-
gb := NewGoBrew(t.TempDir())
55+
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
56+
defer ts.Close()
57+
gb := setupGobrew(t, ts)
3358

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

5378
func TestDoNotUpgradeLatestVersion(t *testing.T) {
5479
t.Skip("skipping test...needs to rewrite")
55-
gb := NewGoBrew(t.TempDir())
80+
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
81+
defer ts.Close()
82+
gb := setupGobrew(t, ts)
5683

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

77104
func TestInteractive(t *testing.T) {
78105
t.Parallel()
79-
gb := NewGoBrew(t.TempDir())
106+
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
107+
defer ts.Close()
108+
gb := setupGobrew(t, ts)
109+
80110
currentVersion := gb.CurrentVersion()
81111
latestVersion := gb.getLatestVersion()
82-
// modVersion := gb.getModVersion()
83112
assert.Equal(t, "None", currentVersion)
84113
assert.NotEqual(t, currentVersion, latestVersion)
85114

86115
gb.Interactive(false)
87116

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

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

100126
gb.Interactive(false)
101127
currentVersion = gb.CurrentVersion()
102-
currentVersion = strings.Replace(currentVersion, "private", "", -1)
103128
assert.Equal(t, currentVersion, latestVersion)
104129
t.Log("test finished")
105130
}
106131

107132
func TestPrune(t *testing.T) {
108133
t.Parallel()
109-
gb := NewGoBrew(t.TempDir())
134+
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
135+
defer ts.Close()
136+
gb := setupGobrew(t, ts)
110137
gb.Install("1.20")
111138
gb.Install("1.19")
112139
gb.Use("1.19")
@@ -118,7 +145,9 @@ func TestPrune(t *testing.T) {
118145

119146
func TestGoBrew_CurrentVersion(t *testing.T) {
120147
t.Parallel()
121-
gb := NewGoBrew(t.TempDir())
148+
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
149+
defer ts.Close()
150+
gb := setupGobrew(t, ts)
122151
assert.Equal(t, true, gb.CurrentVersion() == "None")
123152
gb.Install("1.19")
124153
gb.Use("1.19")

helpers.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99
"net"
1010
"net/http"
11+
"net/url"
1112
"os"
1213
"path/filepath"
1314
"regexp"
@@ -319,14 +320,10 @@ func (gb *GoBrew) getVersionDir(version string) string {
319320
return filepath.Join(gb.versionsDir, version)
320321
}
321322

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

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

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

371368
func (gb *GoBrew) getGobrewVersion() string {
372-
url := "https://api.github.com/repos/kevincobain2000/gobrew/releases/latest"
373-
data := doRequest(url)
369+
data := doRequest(gb.gobrewVersionsUrl)
374370
if len(data) == 0 {
375371
return ""
376372
}
@@ -385,7 +381,7 @@ func (gb *GoBrew) getGobrewVersion() string {
385381
}
386382

387383
func (gb *GoBrew) getGolangVersions() (result []string) {
388-
data := doRequest(goBrewTagsApi)
384+
data := doRequest(gb.gobrewTags)
389385
if len(data) == 0 {
390386
return
391387
}

0 commit comments

Comments
 (0)