diff --git a/gobrew.go b/gobrew.go index 90e9bb5..43e9b90 100644 --- a/gobrew.go +++ b/gobrew.go @@ -14,6 +14,7 @@ import ( "github.com/Masterminds/semver" "github.com/gookit/color" + "github.com/kevincobain2000/gobrew/utils" ) @@ -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 diff --git a/helpers.go b/helpers.go index d564450..d8bed96 100644 --- a/helpers.go +++ b/helpers.go @@ -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" ) @@ -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 } @@ -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) } @@ -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)) @@ -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 diff --git a/helpers_test.go b/helpers_test.go index 38f491c..61f6763 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -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 // {