Skip to content

Commit 0bfb242

Browse files
committed
Factor out logic for needGopath
1 parent b169f1b commit 0bfb242

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,28 @@ func fixGoVendorIssues(modMode ModMode, depMode DependencyInstallerMode, goDirec
279279
return modMode
280280
}
281281

282+
func getNeedGopath(depMode DependencyInstallerMode, importpath string) bool {
283+
needGopath := true
284+
if depMode == GoGetWithModules {
285+
needGopath = false
286+
}
287+
// if `LGTM_INDEX_NEED_GOPATH` is set, it overrides the value for `needGopath` inferred above
288+
if needGopathOverride := os.Getenv("LGTM_INDEX_NEED_GOPATH"); needGopathOverride != "" {
289+
if needGopathOverride == "true" {
290+
needGopath = true
291+
} else if needGopathOverride == "false" {
292+
needGopath = false
293+
} else {
294+
log.Fatalf("Unexpected value for Boolean environment variable LGTM_NEED_GOPATH: %v.\n", needGopathOverride)
295+
}
296+
}
297+
if needGopath && importpath == "" {
298+
log.Printf("Failed to determine import path, not setting up GOPATH")
299+
needGopath = false
300+
}
301+
return needGopath
302+
}
303+
282304
func main() {
283305
if len(os.Args) > 1 {
284306
usage()
@@ -306,13 +328,11 @@ func main() {
306328
// determine how to install dependencies and whether a GOPATH needs to be set up before
307329
// extraction
308330
depMode := getDepMode()
309-
needGopath := true
310331
goDirectiveFound := false
311332
if _, present := os.LookupEnv("GO111MODULE"); !present {
312333
os.Setenv("GO111MODULE", "auto")
313334
}
314335
if depMode == GoGetWithModules {
315-
needGopath = false
316336
versionRe := regexp.MustCompile(`(?m)^go[ \t\r]+([0-9]+\.[0-9]+)$`)
317337
goMod, err := ioutil.ReadFile("go.mod")
318338
if err != nil {
@@ -372,22 +392,11 @@ func main() {
372392
}
373393
}
374394

375-
// if `LGTM_INDEX_NEED_GOPATH` is set, it overrides the value for `needGopath` inferred above
376-
if needGopathOverride := os.Getenv("LGTM_INDEX_NEED_GOPATH"); needGopathOverride != "" {
377-
inLGTM = true
378-
if needGopathOverride == "true" {
379-
needGopath = true
380-
} else if needGopathOverride == "false" {
381-
needGopath = false
382-
} else {
383-
log.Fatalf("Unexpected value for Boolean environment variable LGTM_NEED_GOPATH: %v.\n", needGopathOverride)
384-
}
385-
}
386-
387395
importpath := getImportPath()
388-
if needGopath && importpath == "" {
389-
log.Printf("Failed to determine import path, not setting up GOPATH")
390-
needGopath = false
396+
needGopath := getNeedGopath(depMode, importpath)
397+
398+
if os.Getenv("LGTM_INDEX_NEED_GOPATH") != "" {
399+
inLGTM = true
391400
}
392401

393402
if inLGTM && needGopath {

0 commit comments

Comments
 (0)