Skip to content

Commit 76781e5

Browse files
committed
Go: Add GoVersionInfo type
Refactors `tryReadGoDirective` to return this instead of a pair. This will make it easier to return multiple versions.
1 parent eb3f196 commit 76781e5

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

go/extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,15 @@ func getDepMode(emitDiagnostics bool) (DependencyInstallerMode, string) {
367367
return GoGetNoModules, "."
368368
}
369369

370+
type GoVersionInfo struct {
371+
// The version string, if any
372+
Version string
373+
// A value indicating whether a version string was found
374+
Found bool
375+
}
376+
370377
// Tries to open `go.mod` and read a go directive, returning the version and whether it was found.
371-
func tryReadGoDirective(buildInfo BuildInfo) (string, bool) {
378+
func tryReadGoDirective(buildInfo BuildInfo) GoVersionInfo {
372379
if buildInfo.DepMode == GoGetWithModules {
373380
versionRe := regexp.MustCompile(`(?m)^go[ \t\r]+([0-9]+\.[0-9]+(\.[0-9]+)?)$`)
374381
goMod, err := os.ReadFile(filepath.Join(buildInfo.BaseDir, "go.mod"))
@@ -378,12 +385,12 @@ func tryReadGoDirective(buildInfo BuildInfo) (string, bool) {
378385
matches := versionRe.FindSubmatch(goMod)
379386
if matches != nil {
380387
if len(matches) > 1 {
381-
return string(matches[1]), true
388+
return GoVersionInfo{string(matches[1]), true}
382389
}
383390
}
384391
}
385392
}
386-
return "", false
393+
return GoVersionInfo{"", false}
387394
}
388395

389396
// Returns the appropriate ModMode for the current project
@@ -771,13 +778,13 @@ func installDependenciesAndBuild() {
771778
os.Setenv("GO111MODULE", "auto")
772779
}
773780

774-
goModVersion, goModVersionFound := tryReadGoDirective(buildInfo)
781+
goVersionInfo := tryReadGoDirective(buildInfo)
775782

776-
if goModVersionFound && semver.Compare("v"+goModVersion, getEnvGoSemVer()) > 0 {
783+
if goVersionInfo.Found && semver.Compare("v"+goVersionInfo.Version, getEnvGoSemVer()) > 0 {
777784
diagnostics.EmitNewerGoVersionNeeded()
778785
}
779786

780-
fixGoVendorIssues(&buildInfo, goModVersionFound)
787+
fixGoVendorIssues(&buildInfo, goVersionInfo.Found)
781788

782789
tryUpdateGoModAndGoSum(buildInfo)
783790

@@ -1092,7 +1099,8 @@ func isGoInstalled() bool {
10921099
func identifyEnvironment() {
10931100
var v versionInfo
10941101
buildInfo := getBuildInfo(false)
1095-
v.goModVersion, v.goModVersionFound = tryReadGoDirective(buildInfo)
1102+
goVersionInfo := tryReadGoDirective(buildInfo)
1103+
v.goModVersion, v.goModVersionFound = goVersionInfo.Version, goVersionInfo.Found
10961104

10971105
v.goEnvVersionFound = isGoInstalled()
10981106
if v.goEnvVersionFound {

0 commit comments

Comments
 (0)