Go: Add GoVersionInfo type

Refactors `tryReadGoDirective` to return this instead of a pair.
This will make it easier to return multiple versions.
This commit is contained in:
Michael B. Gale
2023-09-08 13:55:35 +01:00
parent eb3f1967a5
commit 76781e5d75

View File

@@ -367,8 +367,15 @@ func getDepMode(emitDiagnostics bool) (DependencyInstallerMode, string) {
return GoGetNoModules, "."
}
type GoVersionInfo struct {
// The version string, if any
Version string
// A value indicating whether a version string was found
Found bool
}
// Tries to open `go.mod` and read a go directive, returning the version and whether it was found.
func tryReadGoDirective(buildInfo BuildInfo) (string, bool) {
func tryReadGoDirective(buildInfo BuildInfo) GoVersionInfo {
if buildInfo.DepMode == GoGetWithModules {
versionRe := regexp.MustCompile(`(?m)^go[ \t\r]+([0-9]+\.[0-9]+(\.[0-9]+)?)$`)
goMod, err := os.ReadFile(filepath.Join(buildInfo.BaseDir, "go.mod"))
@@ -378,12 +385,12 @@ func tryReadGoDirective(buildInfo BuildInfo) (string, bool) {
matches := versionRe.FindSubmatch(goMod)
if matches != nil {
if len(matches) > 1 {
return string(matches[1]), true
return GoVersionInfo{string(matches[1]), true}
}
}
}
}
return "", false
return GoVersionInfo{"", false}
}
// Returns the appropriate ModMode for the current project
@@ -771,13 +778,13 @@ func installDependenciesAndBuild() {
os.Setenv("GO111MODULE", "auto")
}
goModVersion, goModVersionFound := tryReadGoDirective(buildInfo)
goVersionInfo := tryReadGoDirective(buildInfo)
if goModVersionFound && semver.Compare("v"+goModVersion, getEnvGoSemVer()) > 0 {
if goVersionInfo.Found && semver.Compare("v"+goVersionInfo.Version, getEnvGoSemVer()) > 0 {
diagnostics.EmitNewerGoVersionNeeded()
}
fixGoVendorIssues(&buildInfo, goModVersionFound)
fixGoVendorIssues(&buildInfo, goVersionInfo.Found)
tryUpdateGoModAndGoSum(buildInfo)
@@ -1092,7 +1099,8 @@ func isGoInstalled() bool {
func identifyEnvironment() {
var v versionInfo
buildInfo := getBuildInfo(false)
v.goModVersion, v.goModVersionFound = tryReadGoDirective(buildInfo)
goVersionInfo := tryReadGoDirective(buildInfo)
v.goModVersion, v.goModVersionFound = goVersionInfo.Version, goVersionInfo.Found
v.goEnvVersionFound = isGoInstalled()
if v.goEnvVersionFound {