Skip to content

Commit

Permalink
getting version from go.mod may throw error when there is no go.mod f…
Browse files Browse the repository at this point in the history
…ile.

check for file existance first, otherwise set to None
  • Loading branch information
kevincobain2000 committed Nov 1, 2023
1 parent f691f10 commit 38a053e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
13 changes: 11 additions & 2 deletions README.md
Expand Up @@ -72,10 +72,18 @@ Reload config.
### Confirm

```sh
gobrew help
gobrew
```

### Usage
### Quick Usage

Just use command `gobrew`

```sh
gobrew
```

### Full Usage

Will install and set Go

Expand Down Expand Up @@ -333,3 +341,4 @@ alias cd='builtin cd "$@" && ls go.mod 2> /dev/null && gobrew use mod'
- v1.9.0 - v1.8.6 ~ v1.9.0, updates colors packages, fixes UT issues for Github status codes
- v1.9.1 - Minor logging fixes
- v1.9.2 - Minor log message updated
- v1.9.4 - `gobrew` interactive
21 changes: 18 additions & 3 deletions gobrew.go
Expand Up @@ -91,7 +91,10 @@ func (gb *GoBrew) Interactive(ask bool) {
latestVersion := gb.getLatestVersion()
latestMajorVersion := ExtractMajorVersion(latestVersion)

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

if modVersion == "" {
modVersion = "None"
Expand Down Expand Up @@ -120,7 +123,7 @@ func (gb *GoBrew) Interactive(ask bool) {
color.Successln("GO Installed Version", ".......", currentVersion+label)
}

if latestMajorVersion != modVersion {
if modVersion != "None" && latestMajorVersion != modVersion {
label := " " + color.FgYellow.Render("(not latest)")
color.Successln("GO go.mod Version", " .......", modVersion+label)
} else {
Expand Down Expand Up @@ -345,7 +348,7 @@ func (gb *GoBrew) getGroupedVersion(versions []string, print bool) map[string][]
gb.print("\t", print)
} else {
if print {
color.Infop(lookupKey)
color.Successp(lookupKey)
}
gb.print("\t", print)
}
Expand Down Expand Up @@ -563,6 +566,18 @@ func (gb *GoBrew) judgeVersion(version string) string {
return version
}

func (gb *GoBrew) hasModFile() bool {
modFilePath := filepath.Join("go.mod")
_, err := os.Stat(modFilePath)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}

// read go.mod file and extract version
// Do not use go to get the version as go list -m -f '{{.GoVersion}}'
// Because go might not be installed
Expand Down

0 comments on commit 38a053e

Please sign in to comment.