Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not install not exist version #198

Merged
merged 1 commit into from
Jul 28, 2024
Merged
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
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
Loading