Skip to content

Commit f018695

Browse files
committed
Factor out tryUpdateGoModAndGoSum
1 parent 0bfb242 commit f018695

File tree

1 file changed

+41
-37
lines changed

1 file changed

+41
-37
lines changed

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

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,46 @@ func getNeedGopath(depMode DependencyInstallerMode, importpath string) bool {
301301
return needGopath
302302
}
303303

304+
func tryUpdateGoModAndGoSum(modMode ModMode, depMode DependencyInstallerMode) {
305+
// Go 1.16 and later won't automatically attempt to update go.mod / go.sum during package loading, so try to update them here:
306+
if modMode != ModVendor && depMode == GoGetWithModules && semver.Compare(getEnvGoSemVer(), "1.16") >= 0 {
307+
// stat go.mod and go.sum
308+
beforeGoModFileInfo, beforeGoModErr := os.Stat("go.mod")
309+
if beforeGoModErr != nil {
310+
log.Println("Failed to stat go.mod before running `go mod tidy -e`")
311+
}
312+
313+
beforeGoSumFileInfo, beforeGoSumErr := os.Stat("go.sum")
314+
315+
// run `go mod tidy -e`
316+
res := util.RunCmd(exec.Command("go", "mod", "tidy", "-e"))
317+
318+
if !res {
319+
log.Println("Failed to run `go mod tidy -e`")
320+
} else {
321+
if beforeGoModFileInfo != nil {
322+
afterGoModFileInfo, afterGoModErr := os.Stat("go.mod")
323+
if afterGoModErr != nil {
324+
log.Println("Failed to stat go.mod after running `go mod tidy -e`")
325+
} else if afterGoModFileInfo.ModTime().After(beforeGoModFileInfo.ModTime()) {
326+
// if go.mod has been changed then notify the user
327+
log.Println("We have run `go mod tidy -e` and it altered go.mod. You may wish to check these changes into version control. ")
328+
}
329+
}
330+
331+
afterGoSumFileInfo, afterGoSumErr := os.Stat("go.sum")
332+
if afterGoSumErr != nil {
333+
log.Println("Failed to stat go.sum after running `go mod tidy -e`")
334+
} else {
335+
if beforeGoSumErr != nil || afterGoSumFileInfo.ModTime().After(beforeGoSumFileInfo.ModTime()) {
336+
// if go.sum has been changed then notify the user
337+
log.Println("We have run `go mod tidy -e` and it altered go.sum. You may wish to check these changes into version control. ")
338+
}
339+
}
340+
}
341+
}
342+
}
343+
304344
func main() {
305345
if len(os.Args) > 1 {
306346
usage()
@@ -354,43 +394,7 @@ func main() {
354394
modMode := getModMode(depMode)
355395
modMode = fixGoVendorIssues(modMode, depMode, goDirectiveFound)
356396

357-
// Go 1.16 and later won't automatically attempt to update go.mod / go.sum during package loading, so try to update them here:
358-
if modMode != ModVendor && depMode == GoGetWithModules && semver.Compare(getEnvGoSemVer(), "1.16") >= 0 {
359-
// stat go.mod and go.sum
360-
beforeGoModFileInfo, beforeGoModErr := os.Stat("go.mod")
361-
if beforeGoModErr != nil {
362-
log.Println("Failed to stat go.mod before running `go mod tidy -e`")
363-
}
364-
365-
beforeGoSumFileInfo, beforeGoSumErr := os.Stat("go.sum")
366-
367-
// run `go mod tidy -e`
368-
res := util.RunCmd(exec.Command("go", "mod", "tidy", "-e"))
369-
370-
if !res {
371-
log.Println("Failed to run `go mod tidy -e`")
372-
} else {
373-
if beforeGoModFileInfo != nil {
374-
afterGoModFileInfo, afterGoModErr := os.Stat("go.mod")
375-
if afterGoModErr != nil {
376-
log.Println("Failed to stat go.mod after running `go mod tidy -e`")
377-
} else if afterGoModFileInfo.ModTime().After(beforeGoModFileInfo.ModTime()) {
378-
// if go.mod has been changed then notify the user
379-
log.Println("We have run `go mod tidy -e` and it altered go.mod. You may wish to check these changes into version control. ")
380-
}
381-
}
382-
383-
afterGoSumFileInfo, afterGoSumErr := os.Stat("go.sum")
384-
if afterGoSumErr != nil {
385-
log.Println("Failed to stat go.sum after running `go mod tidy -e`")
386-
} else {
387-
if beforeGoSumErr != nil || afterGoSumFileInfo.ModTime().After(beforeGoSumFileInfo.ModTime()) {
388-
// if go.sum has been changed then notify the user
389-
log.Println("We have run `go mod tidy -e` and it altered go.sum. You may wish to check these changes into version control. ")
390-
}
391-
}
392-
}
393-
}
397+
tryUpdateGoModAndGoSum(modMode, depMode)
394398

395399
importpath := getImportPath()
396400
needGopath := getNeedGopath(depMode, importpath)

0 commit comments

Comments
 (0)