Skip to content

Commit

Permalink
Fixes #145 by adding interactivity
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Oct 31, 2023
1 parent b48f97f commit 7d911bf
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 11 deletions.
12 changes: 9 additions & 3 deletions cmd/gobrew/main.go
Expand Up @@ -25,6 +25,8 @@ var allowedArgs = []string{
"install",
"use",
"uninstall",
"interactive",
"noninteractive",
"prune",
"version",
"self-update",
Expand All @@ -42,11 +44,11 @@ func init() {
flag.Parse()
args = flag.Args()
if len(args) == 0 {
log.Print(usage())
return
actionArg = "interactive"
} else {
actionArg = args[0]
}

actionArg = args[0]
if len(args) == 2 {
versionArg = args[1]
versionArgSlice := strings.Split(versionArg, ".")
Expand All @@ -64,6 +66,10 @@ func init() {
func main() {
gb := gobrew.NewGoBrew()
switch actionArg {
case "interactive", "info":
gb.Interactive(true)
case "noninteractive":
gb.Interactive(false)
case "h", "help":
log.Print(usage())
case "ls", "list":
Expand Down
4 changes: 0 additions & 4 deletions go.sum
@@ -1,7 +1,6 @@
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 h1:GKTyiRCL6zVf5wWaqKnf+7Qs6GbEPfd4iMOitWzXJx8=
github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8/go.mod h1:spo1JLcs67NmW1aVLEgtA8Yy1elc+X8y5SRW1sFW4Og=
github.com/c4milo/unpackit v1.0.0 h1:Umce1lwtFvEHNFQev+xENObYiiYxdSmKhvGlkcufUGE=
github.com/c4milo/unpackit v1.0.0/go.mod h1:0cXRaRz5pMcJm7o9jYQmPAeBl6y1na9BKy3K+og0UJY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -15,7 +14,6 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
github.com/hooklift/assert v0.1.0 h1:UZzFxx5dSb9aBtvMHTtnPuvFnBvcEhHTPb9+0+jpEjs=
github.com/hooklift/assert v0.1.0/go.mod h1:pfexfvIHnKCdjh6CkkIZv5ic6dQ6aU2jhKghBlXuwwY=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/klauspost/compress v1.4.1 h1:8VMb5+0wMgdBykOV96DwNwKFQ+WTI4pzYURP99CcB9E=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
Expand All @@ -39,7 +37,6 @@ github.com/schollz/progressbar/v3 v3.13.1/go.mod h1:xvrbki8kfT1fzWzBT/UZd9L6GA+j
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/ulikunitz/xz v0.5.6/go.mod h1:2bypXElzHzzJZwzH67Y6wb67pO62Rzfn7BSiF4ABRW8=
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
Expand All @@ -52,6 +49,5 @@ golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
95 changes: 92 additions & 3 deletions gobrew.go
Expand Up @@ -84,6 +84,95 @@ func NewGoBrewDirectory(homeDir string) GoBrew {
return gb
}

func (gb *GoBrew) Interactive(ask bool) {
currentVersion := gb.CurrentVersion()
currentMajorVersion := ExtractMajorVersion(currentVersion)

latestVersion := gb.getLatestVersion()
latestMajorVersion := ExtractMajorVersion(latestVersion)

modVersion := gb.getModVersion()

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

fmt.Println()

if currentVersion == "" {
currentVersion = "None"
color.Warnln("GO Installed Version", ".......", currentVersion)
} else {
labels := []string{}
if currentMajorVersion != modVersion && modVersion != "None" {
labels = append(labels, "not same as go.mod")
}
if currentVersion != latestVersion {
labels = append(labels, "not latest")
}
label := ""
if len(labels) > 0 {
label = "(" + strings.Join(labels, ", ") + ")"
}
if label != "" {
label = " " + color.FgRed.Render(label)
}
color.Successln("GO Installed Version", ".......", currentVersion+label)
}

if latestMajorVersion != modVersion {
label := " " + color.FgYellow.Render("(not latest)")
color.Successln("GO go.mod Version", " .......", modVersion+label)
} else {
color.Successln("GO go.mod Version", " .......", modVersion)
}

color.Successln("GO Latest Version", " .......", latestVersion)
fmt.Println()

// do not implement this yet. gobrew should not tinker with someone's project and edit their go.mod files
// if latestMajorVersion != modVersion {
// color.Yellowf("GO Installed Major Version (%s) and GO Latest Version (%s) are different.\n", currentMajorVersion, latestMajorVersion)
// // do you want to install latest and update go.mod file
// }

if currentMajorVersion != modVersion {
color.Warnf("GO Installed Version (%s) and go.mod Version (%s) are different.\n", currentMajorVersion, modVersion)
c := true
if ask {
c = AskForConfirmation("Do you want to use GO version same as go.mod version (" + modVersion + "@latest)?")
}
if c {
gb.Use(modVersion + "@latest")
}
return
}

if currentVersion != latestVersion {
color.Warnf("GO Installed Version (%s) and GO Latest Version (%s) are different.\n", currentVersion, latestVersion)
c := true
if ask {
c = AskForConfirmation("Do you want to update GO to latest version (" + latestVersion + ")?")
}
if c {
gb.Use(latestVersion)
}
return
}
}

func (gb *GoBrew) getLatestVersion() string {
getGolangVersions := gb.getGolangVersions()
// loop through reverse and ignore beta and rc versions to get latest version
for i := len(getGolangVersions) - 1; i >= 0; i-- {
r := regexp.MustCompile("beta.*|rc.*")
matches := r.FindAllString(getGolangVersions[i], -1)
if len(matches) == 0 {
return strings.ReplaceAll(getGolangVersions[i], "go", "")
}
}
return ""
}
func (gb *GoBrew) getArch() string {
return runtime.GOOS + "-" + runtime.GOARCH
}
Expand Down Expand Up @@ -514,7 +603,7 @@ func (gb *GoBrew) Version(currentVersion string) {

// Upgrade of GoBrew
func (gb *GoBrew) Upgrade(currentVersion string) {
if "v"+currentVersion == gb.getLatestVersion() {
if "v"+currentVersion == gb.getGobrewVersion() {
color.Infoln("[INFO] your version is already newest")
return
}
Expand All @@ -532,7 +621,7 @@ func (gb *GoBrew) Upgrade(currentVersion string) {
"[Error] Download GoBrew failed")

source, err := os.Open(tmpFile)
utils.CheckError(err, "[Error] Cannot open file")
utils.CheckError(err, "==> [Error] Cannot open file")
defer func(source *os.File) {
_ = source.Close()
utils.CheckError(os.Remove(source.Name()), "==> [Error] Cannot remove tmp file:")
Expand Down Expand Up @@ -638,7 +727,7 @@ func (gb *GoBrew) changeSymblinkGo(version string) {
utils.CheckError(os.Symlink(versionGoDir, gb.currentGoDir), "==> [Error]: symbolic link failed")
}

func (gb *GoBrew) getLatestVersion() string {
func (gb *GoBrew) getGobrewVersion() string {
url := "https://api.github.com/repos/kevincobain2000/gobrew/releases/latest"
data := doRequest(url)
if len(data) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion gobrew_test.go
Expand Up @@ -196,7 +196,7 @@ func TestDoNotUpgradeLatestVersion(t *testing.T) {
}
binaryFile := filepath.Join(binaryDir, baseName)

currentVersion := gb.getLatestVersion()
currentVersion := gb.getGobrewVersion()

if currentVersion == "" {
t.Skip("could not determine the current version")
Expand Down
41 changes: 41 additions & 0 deletions helpers.go
@@ -0,0 +1,41 @@
package gobrew

import (
"bufio"
"fmt"
"log"
"os"
"strings"

"github.com/gookit/color"
)

func ExtractMajorVersion(version string) string {
parts := strings.Split(version, ".")

// Take the first two parts and join them back with a period to create the new version.
majorVersion := strings.Join(parts[:2], ".")
return majorVersion
}

func AskForConfirmation(s string) bool {
reader := bufio.NewReader(os.Stdin)

for {
fmt.Printf("%s ", s)
color.Successf("[y/n]: ")

response, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}

response = strings.ToLower(strings.TrimSpace(response))

if response == "y" || response == "yes" {
return true
} else if response == "" || response == "n" || response == "no" {
return false
}
}
}

0 comments on commit 7d911bf

Please sign in to comment.