Skip to content

Commit

Permalink
fix: do not install not exist version
Browse files Browse the repository at this point in the history
  • Loading branch information
juev committed Jul 27, 2024
1 parent 50870b9 commit ca2412d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
5 changes: 5 additions & 0 deletions gobrew.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/Masterminds/semver"
"github.com/gookit/color"

"github.com/kevincobain2000/gobrew/utils"
)

Expand Down Expand Up @@ -304,6 +305,10 @@ func (gb *GoBrew) Install(version string) string {
os.Exit(1)
}
version = gb.judgeVersion(version)
if version == NoneVersion {
color.Errorln("[Error] Version non exists")
os.Exit(1)
}
if gb.existsVersion(version) {
color.Infof("==> [Info] Version: %s exists\n", version)
return version
Expand Down
19 changes: 16 additions & 3 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (
"path/filepath"
"regexp"
"runtime"
"slices"
"sort"
"strings"

"github.com/Masterminds/semver"
"github.com/c4milo/unpackit"
"github.com/gookit/color"

"github.com/kevincobain2000/gobrew/utils"
)

Expand Down Expand Up @@ -269,6 +271,17 @@ func (gb *GoBrew) judgeVersion(version string) string {
}
}

exists := false
for _, value := range groupedVersions {
if slices.Contains(value, version) {
exists = true
break
}
}
if !exists {
return NoneVersion
}

return version
}

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

if err != nil {
gb.cleanDownloadsDir()
color.Infoln("==> [Info] Downloading version failed:", err)
color.Errorln("==> [Error] Downloading version failed:", err)
color.Errorln("==> [Error]: Please check connectivity to url:", downloadURL)
os.Exit(1)
}
Expand All @@ -365,7 +378,7 @@ func (gb *GoBrew) downloadAndExtract(version string) {
if err != nil {
// clean up dir
gb.cleanVersionDir(version)
color.Infoln("==> [Info] Extract failed:", err)
color.Errorln("==> [Info] Extract failed:", err)
os.Exit(1)
}
color.Infoln("==> [Success] Extract to", gb.getVersionDir(version))
Expand Down Expand Up @@ -452,7 +465,7 @@ func doRequest(url string) (data []byte) {
}

func (gb *GoBrew) extract(srcTar string, dstDir string) error {
//#nosec G304
// #nosec G304
file, err := os.Open(srcTar)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func TestJudgeVersion(t *testing.T) {
version: "1.18@dev-latest",
wantVersion: "1.18.10",
},
{
version: "go1.18",
wantVersion: "None",
},
// following 2 tests fail upon new version release
// commenting out for now as the tool is stable
// {
Expand Down

0 comments on commit ca2412d

Please sign in to comment.