Skip to content

Commit 644d7f1

Browse files
committed
Factor out tryReadGoDirective()
1 parent 5e87111 commit 644d7f1

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

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

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,31 @@ func getDepMode() DependencyInstallerMode {
252252
return GoGetNoModules
253253
}
254254

255+
func tryReadGoDirective(depMode DependencyInstallerMode) (string, bool) {
256+
version := ""
257+
found := false
258+
if depMode == GoGetWithModules {
259+
versionRe := regexp.MustCompile(`(?m)^go[ \t\r]+([0-9]+\.[0-9]+)$`)
260+
goMod, err := os.ReadFile("go.mod")
261+
if err != nil {
262+
log.Println("Failed to read go.mod to check for missing Go version")
263+
} else {
264+
matches := versionRe.FindSubmatch(goMod)
265+
if matches != nil {
266+
found = true
267+
if len(matches) > 1 {
268+
version := string(matches[1])
269+
semverVersion := "v" + version
270+
if semver.Compare(semverVersion, getEnvGoSemVer()) >= 0 {
271+
diagnostics.EmitNewerGoVersionNeeded()
272+
}
273+
}
274+
}
275+
}
276+
}
277+
return version, found
278+
}
279+
255280
func getModMode(depMode DependencyInstallerMode) ModMode {
256281
if depMode == GoGetWithModules {
257282
// if a vendor/modules.txt file exists, we assume that there are vendored Go dependencies, and
@@ -612,28 +637,11 @@ func installDependenciesAndBuild() {
612637
// determine how to install dependencies and whether a GOPATH needs to be set up before
613638
// extraction
614639
depMode := getDepMode()
615-
goDirectiveFound := false
616640
if _, present := os.LookupEnv("GO111MODULE"); !present {
617641
os.Setenv("GO111MODULE", "auto")
618642
}
619-
if depMode == GoGetWithModules {
620-
versionRe := regexp.MustCompile(`(?m)^go[ \t\r]+([0-9]+\.[0-9]+)$`)
621-
goMod, err := os.ReadFile("go.mod")
622-
if err != nil {
623-
log.Println("Failed to read go.mod to check for missing Go version")
624-
} else {
625-
matches := versionRe.FindSubmatch(goMod)
626-
if matches != nil {
627-
goDirectiveFound = true
628-
if len(matches) > 1 {
629-
goDirectiveVersion := "v" + string(matches[1])
630-
if semver.Compare(goDirectiveVersion, getEnvGoSemVer()) >= 0 {
631-
diagnostics.EmitNewerGoVersionNeeded()
632-
}
633-
}
634-
}
635-
}
636-
}
643+
644+
_, goDirectiveFound := tryReadGoDirective(depMode)
637645

638646
modMode := getModMode(depMode)
639647
modMode = fixGoVendorIssues(modMode, depMode, goDirectiveFound)

0 commit comments

Comments
 (0)