Skip to content

Commit 338c4a4

Browse files
authored
fix: do not install not exist version (#198)
1 parent 50870b9 commit 338c4a4

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

gobrew.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/Masterminds/semver"
1616
"github.com/gookit/color"
17+
1718
"github.com/kevincobain2000/gobrew/utils"
1819
)
1920

@@ -304,6 +305,10 @@ func (gb *GoBrew) Install(version string) string {
304305
os.Exit(1)
305306
}
306307
version = gb.judgeVersion(version)
308+
if version == NoneVersion {
309+
color.Errorln("[Error] Version non exists")
310+
os.Exit(1)
311+
}
307312
if gb.existsVersion(version) {
308313
color.Infof("==> [Info] Version: %s exists\n", version)
309314
return version

helpers.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ import (
1212
"path/filepath"
1313
"regexp"
1414
"runtime"
15+
"slices"
1516
"sort"
1617
"strings"
1718

1819
"github.com/Masterminds/semver"
1920
"github.com/c4milo/unpackit"
2021
"github.com/gookit/color"
22+
2123
"github.com/kevincobain2000/gobrew/utils"
2224
)
2325

@@ -269,6 +271,17 @@ func (gb *GoBrew) judgeVersion(version string) string {
269271
}
270272
}
271273

274+
exists := false
275+
for _, value := range groupedVersions {
276+
if slices.Contains(value, version) {
277+
exists = true
278+
break
279+
}
280+
}
281+
if !exists {
282+
return NoneVersion
283+
}
284+
272285
return version
273286
}
274287

@@ -350,7 +363,7 @@ func (gb *GoBrew) downloadAndExtract(version string) {
350363

351364
if err != nil {
352365
gb.cleanDownloadsDir()
353-
color.Infoln("==> [Info] Downloading version failed:", err)
366+
color.Errorln("==> [Error] Downloading version failed:", err)
354367
color.Errorln("==> [Error]: Please check connectivity to url:", downloadURL)
355368
os.Exit(1)
356369
}
@@ -365,7 +378,7 @@ func (gb *GoBrew) downloadAndExtract(version string) {
365378
if err != nil {
366379
// clean up dir
367380
gb.cleanVersionDir(version)
368-
color.Infoln("==> [Info] Extract failed:", err)
381+
color.Errorln("==> [Info] Extract failed:", err)
369382
os.Exit(1)
370383
}
371384
color.Infoln("==> [Success] Extract to", gb.getVersionDir(version))
@@ -452,7 +465,7 @@ func doRequest(url string) (data []byte) {
452465
}
453466

454467
func (gb *GoBrew) extract(srcTar string, dstDir string) error {
455-
//#nosec G304
468+
// #nosec G304
456469
file, err := os.Open(srcTar)
457470
if err != nil {
458471
return err

helpers_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func TestJudgeVersion(t *testing.T) {
4242
version: "1.18@dev-latest",
4343
wantVersion: "1.18.10",
4444
},
45+
{
46+
version: "go1.18",
47+
wantVersion: "None",
48+
},
4549
// following 2 tests fail upon new version release
4650
// commenting out for now as the tool is stable
4751
// {

0 commit comments

Comments
 (0)