Skip to content

Commit

Permalink
Merge pull request #162 from kevincobain2000/refactoring-08122023
Browse files Browse the repository at this point in the history
Refactoring 08122023
  • Loading branch information
kevincobain2000 authored Dec 9, 2023
2 parents 0e6fe5f + 34e5862 commit bf6d96c
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 90 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
- '**/*.go'
- '**/*.mod'
- '**/*.sum'
- '**/*.yml'

name: "Build"
jobs:
Expand All @@ -23,5 +24,5 @@ jobs:
run: go version
shell: bash
- name: Build
run: go build cmd/gobrew/main.go
run: go build ./cmd/gobrew
shell: bash
6 changes: 3 additions & 3 deletions .github/workflows/coveritup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ on:
- '**/*.go'
- '**/*.mod'
- '**/*.sum'
- '**/*.yaml'
- '**/*.yml'
push:
paths:
- '**/*.go'
- '**/*.mod'
- '**/*.sum'
- '**/*.yaml'
- '**/*.yml'

name: "Cover It Up"
jobs:
Expand Down Expand Up @@ -49,7 +49,7 @@ jobs:
- name: Build
run: |
BUILD_START=$SECONDS
go build -ldflags '-s -w' -o main cmd/gobrew/main.go
go build -ldflags '-s -w' -o main ./cmd/gobrew
echo SCORE=$(($SECONDS-BUILD_START)) >> "$GITHUB_ENV"
shell: bash

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
- '**/*.go'
- '**/*.mod'
- '**/*.sum'
- '**/*.yaml'
- '**/*.yml'

name: "Go test"
jobs:
Expand All @@ -20,4 +20,4 @@ jobs:
with:
version: ${{ matrix.go-version }}
- name: Test
run: go test -race -v ./... -count=1
run: go test -v ./...
22 changes: 2 additions & 20 deletions cmd/gobrew/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"flag"
"log"
"os"
"runtime"
"strconv"
"strings"

Expand Down Expand Up @@ -88,11 +87,10 @@ func main() {
gb.ListRemoteVersions(true)
case "install":
gb.Install(versionArg)
if gb.CurrentVersion() == "" {
if gb.CurrentVersion() == "None" {
gb.Use(versionArg)
}
case "use":
gb.Install(versionArg)
gb.Use(versionArg)
case "uninstall":
gb.Uninstall(versionArg)
Expand Down Expand Up @@ -168,23 +166,7 @@ Examples:
gobrew use dev-latest # use go version latest avalable, including rc and beta
Installation Path:
`

if runtime.GOOS == "windows" {
msg = msg + `
# Add gobrew to your environment variables
PATH="%USERPROFILE%\.gobrew\current\bin;%USERPROFILE%\.gobrew\bin;%PATH%"
GOROOT="%USERPROFILE%\.gobrew\current\go"
`
} else {
msg = msg + `
# Add gobrew to your ~/.bashrc or ~/.zshrc
export PATH="$HOME/.gobrew/current/bin:$HOME/.gobrew/bin:$PATH"
export GOROOT="$HOME/.gobrew/current/go"
`
}
` + usageMsg

return msg
}
10 changes: 10 additions & 0 deletions cmd/gobrew/main_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build unix

package main

const usageMsg = `
# Add gobrew to your ~/.bashrc or ~/.zshrc
export PATH="$HOME/.gobrew/current/bin:$HOME/.gobrew/bin:$PATH"
export GOROOT="$HOME/.gobrew/current/go"
`
8 changes: 8 additions & 0 deletions cmd/gobrew/main_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

const usageMsg = `
# Add gobrew to your ~/.bashrc or ~/.zshrc
export PATH="$HOME/.gobrew/current/bin:$HOME/.gobrew/bin:$PATH"
export GOROOT="$HOME/.gobrew/current/go"
`
74 changes: 31 additions & 43 deletions gobrew.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Command interface {
ListRemoteVersions(print bool) map[string][]string
CurrentVersion() string
Uninstall(version string)
Install(version string)
Install(version string) string
Use(version string)
Prune()
Version(currentVersion string)
Expand All @@ -55,8 +55,6 @@ type GoBrew struct {
downloadsDir string
}

var gb GoBrew

// NewGoBrew instance
func NewGoBrew() GoBrew {
homeDir, err := os.UserHomeDir()
Expand All @@ -72,6 +70,7 @@ func NewGoBrew() GoBrew {
}

func NewGoBrewDirectory(homeDir string) GoBrew {
gb := GoBrew{}
gb.homeDir = homeDir

gb.installDir = filepath.Join(gb.homeDir, goBrewDir)
Expand All @@ -91,23 +90,18 @@ func (gb *GoBrew) Interactive(ask bool) {
latestVersion := gb.getLatestVersion()
latestMajorVersion := ExtractMajorVersion(latestVersion)

modVersion := ""
modVersion := "None"
if gb.hasModFile() {
modVersion = gb.getModVersion()
modVersion = ExtractMajorVersion(modVersion)
}

if modVersion == "" {
modVersion = "None"
}

fmt.Println()

if currentVersion == "" {
currentVersion = "None"
if currentVersion == "None" {
color.Warnln("GO Installed Version", ".......", currentVersion)
} else {
labels := []string{}
var labels []string
if modVersion != "None" && currentMajorVersion != modVersion {
labels = append(labels, "not same as go.mod")
}
Expand All @@ -117,8 +111,6 @@ func (gb *GoBrew) Interactive(ask bool) {
label := ""
if len(labels) > 0 {
label = "(" + strings.Join(labels, ", ") + ")"
}
if label != "" {
label = " " + color.FgRed.Render(label)
}
color.Successln("GO Installed Version", ".......", currentVersion+label)
Expand All @@ -141,7 +133,6 @@ func (gb *GoBrew) Interactive(ask bool) {
c = AskForConfirmation("Do you want to use latest GO version (" + latestVersion + ")?")
}
if c {
gb.Install(latestVersion)
gb.Use(latestVersion)
}
return
Expand All @@ -154,7 +145,6 @@ func (gb *GoBrew) Interactive(ask bool) {
c = AskForConfirmation("Do you want to use GO version same as go.mod version (" + modVersion + "@latest)?")
}
if c {
gb.Install(modVersion + "@latest")
gb.Use(modVersion + "@latest")
}
return
Expand All @@ -167,7 +157,6 @@ func (gb *GoBrew) Interactive(ask bool) {
c = AskForConfirmation("Do you want to update GO to latest version (" + latestVersion + ")?")
}
if c {
gb.Install(latestVersion)
gb.Use(latestVersion)
}
return
Expand All @@ -181,7 +170,7 @@ func (gb *GoBrew) getLatestVersion() string {
r := regexp.MustCompile("beta.*|rc.*")
matches := r.FindAllString(getGolangVersions[i], -1)
if len(matches) == 0 {
return strings.ReplaceAll(getGolangVersions[i], "go", "")
return getGolangVersions[i]
}
}
return ""
Expand Down Expand Up @@ -285,13 +274,13 @@ func (gb *GoBrew) ListVersions() {

// ListRemoteVersions that are installed by dir ls
func (gb *GoBrew) ListRemoteVersions(print bool) map[string][]string {
color.Infoln("==> [Info] Fetching remote versions\n")
if print {
color.Infoln("==> [Info] Fetching remote versions")
}
tags := gb.getGolangVersions()

var versions []string
for _, tag := range tags {
versions = append(versions, strings.ReplaceAll(tag, "go", ""))
}
versions = append(versions, tags...)

return gb.getGroupedVersion(versions, print)
}
Expand Down Expand Up @@ -427,13 +416,13 @@ func (gb *GoBrew) existsVersion(version string) bool {
func (gb *GoBrew) CurrentVersion() string {
fp, err := filepath.EvalSymlinks(gb.currentBinDir)
if err != nil {
return ""
return "None"
}

version := strings.TrimSuffix(fp, filepath.Join("go", "bin"))
version = filepath.Base(version)
if version == "." {
return ""
return "None"
}
return version
}
Expand Down Expand Up @@ -461,42 +450,43 @@ func (gb *GoBrew) cleanDownloadsDir() {
}

// Install the given version of go
func (gb *GoBrew) Install(version string) {
if version == "" {
func (gb *GoBrew) Install(version string) string {
if version == "" || version == "None" {
color.Errorln("[Error] No version provided")
os.Exit(1)
}
version = gb.judgeVersion(version)
gb.mkDirs(version)
if gb.existsVersion(version) {
color.Infof("==> [Info] Version: %s exists\n", version)
return
return version
}
gb.mkDirs(version)

color.Infof("==> [Info] Downloading version: %s\n", version)
gb.downloadAndExtract(version)
gb.cleanDownloadsDir()
color.Successf("==> [Success] Downloaded version: %s\n", version)
return version
}

func (gb *GoBrew) judgeVersion(version string) string {
judgedVersion := ""
judgedVersion := "None"
rcBetaOk := false
reRcOrBeta := regexp.MustCompile("beta.*|rc.*")
// check if version string ends with x

// check if version string ends with x
if strings.HasSuffix(version, "x") {
judgedVersion = version[:len(version)-1]
judgedVersion = strings.TrimSuffix(version, "x")
}

if strings.HasSuffix(version, ".x") {
judgedVersion = version[:len(version)-2]
judgedVersion = strings.TrimSuffix(version, ".x")
}
if strings.HasSuffix(version, "@latest") {
judgedVersion = version[:len(version)-7]
judgedVersion = strings.TrimSuffix(version, "@latest")
}
if strings.HasSuffix(version, "@dev-latest") {
judgedVersion = version[:len(version)-11]
judgedVersion = strings.TrimSuffix(version, "@dev-latest")
rcBetaOk = true
}

Expand All @@ -509,8 +499,8 @@ func (gb *GoBrew) judgeVersion(version string) string {
}
return gb.judgeVersion(modVersion)
}
groupedVersions := gb.ListRemoteVersions(false) // donot print
if version == "latest" || version == "dev-latest" {
groupedVersions := gb.ListRemoteVersions(false) // donot print
groupedVersionKeys := make([]string, 0, len(groupedVersions))
for groupedVersionKey := range groupedVersions {
groupedVersionKeys = append(groupedVersionKeys, groupedVersionKey)
Expand All @@ -522,7 +512,7 @@ func (gb *GoBrew) judgeVersion(version string) string {
}
}
if len(versionsSemantic) == 0 {
return ""
return "None"
}

// sort semantic versions
Expand All @@ -533,7 +523,7 @@ func (gb *GoBrew) judgeVersion(version string) string {
// get last element
if version == "dev-latest" {
if len(judgedVersions) == 0 {
return ""
return "None"
}
return judgedVersions[len(judgedVersions)-1]
}
Expand All @@ -551,8 +541,7 @@ func (gb *GoBrew) judgeVersion(version string) string {
return gb.judgeVersion(latest)
}

if judgedVersion != "" {
groupedVersions := gb.ListRemoteVersions(false) // donot print
if judgedVersion != "None" {
// check if judgedVersion is in the groupedVersions
if _, ok := groupedVersions[judgedVersion]; ok {
// get last item in the groupedVersions excluding rc and beta
Expand Down Expand Up @@ -592,8 +581,7 @@ func (gb *GoBrew) getModVersion() string {
modFilePath := filepath.Join("go.mod")
modFile, err := os.Open(modFilePath)
if err != nil {
color.Errorln(err)
os.Exit(1)
return "None"
}
defer func(modFile *os.File) {
_ = modFile.Close()
Expand All @@ -611,12 +599,12 @@ func (gb *GoBrew) getModVersion() string {
color.Errorln(err)
os.Exit(1)
}
return ""
return "None"
}

// Use a version
func (gb *GoBrew) Use(version string) {
version = gb.judgeVersion(version)
version = gb.Install(version)
if gb.CurrentVersion() == version {
color.Infof("==> [Info] Version: %s is already your current version \n", version)
return
Expand Down Expand Up @@ -774,7 +762,7 @@ func (gb *GoBrew) getGolangVersions() (result []string) {
for _, tag := range tags {
t := strings.ReplaceAll(tag.Ref, "refs/tags/", "")
if strings.HasPrefix(t, "go") {
result = append(result, t)
result = append(result, strings.TrimPrefix(t, "go"))
}
}

Expand Down
Loading

0 comments on commit bf6d96c

Please sign in to comment.