-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
Output and consider Go toolchain version, too #156
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
|
@@ -48,6 +49,8 @@ type Package struct { | |
ModulePath string | ||
// Version store Package version (current and latest). | ||
Version *Version | ||
// GoVersion stores version of Go toolchain | ||
GoVersion *Version | ||
} | ||
|
||
// Version is package version information. | ||
|
@@ -73,30 +76,70 @@ func (p *Package) SetLatestVer() { | |
|
||
// CurrentToLatestStr returns string about the current version and the latest version | ||
func (p *Package) CurrentToLatestStr() string { | ||
if IsAlreadyUpToDate(*p.Version) { | ||
return "Already up-to-date: " + color.GreenString(p.Version.Latest) | ||
if p.IsAlreadyUpToDate() { | ||
return "Already up-to-date: " + color.GreenString(p.Version.Latest) + " / " + color.GreenString(p.GoVersion.Current) | ||
} | ||
return color.GreenString(p.Version.Current) + " to " + color.YellowString(p.Version.Latest) | ||
var ret string | ||
if p.Version.Current != p.Version.Latest { | ||
ret += color.GreenString(p.Version.Current) + " to " + color.YellowString(p.Version.Latest) | ||
} | ||
if p.GoVersion.Current != p.GoVersion.Latest { | ||
if len(ret) != 0 { | ||
ret += ", " | ||
} | ||
ret += color.GreenString(p.GoVersion.Current) + " to " + color.YellowString(p.GoVersion.Latest) | ||
} | ||
return ret | ||
Comment on lines
+79
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enhanced the string representation functions to include both package and Go toolchain versions. However, consider simplifying the logic to improve readability and maintainability. Refactor the repeated conditional logic into a helper function: func formatVersionChange(current, latest string) string {
if current == latest {
return color.GreenString(current)
}
return "current: " + color.GreenString(current) + ", latest: " + color.YellowString(latest)
} Also applies to: 97-123 |
||
} | ||
|
||
// VersionCheckResultStr returns string about command version check. | ||
func (p *Package) VersionCheckResultStr() string { | ||
if IsAlreadyUpToDate(*p.Version) { | ||
return "Already up-to-date: " + color.GreenString(p.Version.Latest) | ||
if p.IsAlreadyUpToDate() { | ||
return "Already up-to-date: " + color.GreenString(p.Version.Latest) + " / " + color.GreenString(p.GoVersion.Current) | ||
} | ||
var ret string | ||
// TODO: yellow only if latest > current | ||
if p.Version.Current == p.Version.Latest { | ||
ret += color.GreenString(p.Version.Current) | ||
} else { | ||
ret += "current: " + color.GreenString(p.Version.Current) + ", latest: " | ||
if versionUpToDate(p.Version.Current, p.Version.Latest) { | ||
ret += color.GreenString(p.Version.Latest) | ||
} else { | ||
ret += color.YellowString(p.Version.Latest) | ||
} | ||
} | ||
return "current: " + color.GreenString(p.Version.Current) + ", latest: " + color.YellowString(p.Version.Latest) | ||
ret += " / " | ||
if p.GoVersion.Current == p.GoVersion.Latest { | ||
ret += color.GreenString(p.GoVersion.Current) | ||
} else { | ||
ret += "current: " + color.GreenString(p.GoVersion.Current) + ", installed: " | ||
if versionUpToDate(p.GoVersion.Current, p.GoVersion.Latest) { | ||
ret += color.GreenString(p.GoVersion.Latest) | ||
} else { | ||
ret += color.YellowString(p.GoVersion.Latest) | ||
} | ||
} | ||
return ret | ||
} | ||
|
||
// IsAlreadyUpToDate return whether binary is already up to date or not. | ||
func IsAlreadyUpToDate(ver Version) bool { | ||
if ver.Current == ver.Latest { | ||
func (p *Package) IsAlreadyUpToDate() bool { | ||
if p.Version.Current == p.Version.Latest && p.GoVersion.Current == p.GoVersion.Latest { | ||
return true | ||
} | ||
|
||
return strings.Compare( | ||
strings.TrimLeft(ver.Current, "v"), | ||
strings.TrimLeft(ver.Latest, "v"), | ||
) >= 0 | ||
return versionUpToDate( | ||
strings.TrimLeft(p.Version.Current, "v"), | ||
strings.TrimLeft(p.Version.Latest, "v"), | ||
) && versionUpToDate( | ||
strings.TrimLeft(p.GoVersion.Current, "go"), | ||
strings.TrimLeft(p.GoVersion.Latest, "go"), | ||
) | ||
} | ||
|
||
func versionUpToDate(current, available string) bool { | ||
return current >= available | ||
} | ||
|
||
// NewGoPaths return GoPaths instance. | ||
|
@@ -288,6 +331,10 @@ func BinaryPathList(path string) ([]string, error) { | |
// GetPackageInformation return golang package information. | ||
func GetPackageInformation(binList []string) []Package { | ||
pkgs := []Package{} | ||
goVer, err := GetInstalledGoVersion() | ||
if err != nil { | ||
goVer = "unknown" | ||
} | ||
for _, v := range binList { | ||
info, err := buildinfo.ReadFile(v) | ||
if err != nil { | ||
|
@@ -299,8 +346,11 @@ func GetPackageInformation(binList []string) []Package { | |
ImportPath: info.Path, | ||
ModulePath: info.Main.Path, | ||
Version: NewVersion(), | ||
GoVersion: NewVersion(), | ||
} | ||
pkg.Version.Current = info.Main.Version | ||
pkg.GoVersion.Current = info.GoVersion | ||
pkg.GoVersion.Latest = goVer | ||
pkgs = append(pkgs, pkg) | ||
} | ||
return pkgs | ||
|
@@ -318,3 +368,23 @@ func GetPackageVersion(cmdName string) string { | |
} | ||
return info.Main.Version | ||
} | ||
|
||
var goVersionRegex = regexp.MustCompile(`(^|\s)(go[1-9]\S+)`) | ||
|
||
func GetInstalledGoVersion() (string, error) { | ||
var stdout, stderr bytes.Buffer | ||
cmd := exec.Command(goExe, "version") | ||
cmd.Stdout = &stdout | ||
cmd.Stderr = &stderr | ||
|
||
err := cmd.Run() | ||
if err != nil { | ||
return "", fmt.Errorf("can't check go version:\n%s", stderr.String()) | ||
} | ||
|
||
if m := goVersionRegex.FindStringSubmatch(stdout.String()); m != nil { | ||
return m[2], nil | ||
} | ||
|
||
return "", fmt.Errorf("can't find go version string in %q", strings.TrimSpace(stdout.String())) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure the mutex lock is held for the shortest time necessary to avoid potential performance bottlenecks.
Consider moving the
mu.Unlock()
call immediately after theappend
operation to minimize the critical section:Committable suggestion