Skip to content

Commit 570d3f4

Browse files
committed
Use os.Stat instead of os.File.Stat
1 parent 22ccbba commit 570d3f4

File tree

1 file changed

+14
-47
lines changed

1 file changed

+14
-47
lines changed

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

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -292,31 +292,12 @@ func main() {
292292

293293
if depMode == GoGetWithModules {
294294
// stat go.mod and go.sum
295-
var beforeGoModFileInfo, beforeGoSumFileInfo os.FileInfo
296-
297-
beforeGoMod, beforeGoModerr := os.Open("go.mod")
298-
if beforeGoModerr == nil {
299-
var beforeGoModStatErr error
300-
beforeGoModFileInfo, beforeGoModStatErr = beforeGoMod.Stat()
301-
if beforeGoModStatErr != nil {
302-
log.Println("Failed to stat go.mod before running `go mod tidy -e`")
303-
}
304-
} else {
305-
log.Println("Failed to read go.mod before running `go mod tidy -e`")
295+
beforeGoModFileInfo, beforeGoModErr := os.Stat("go.mod")
296+
if beforeGoModErr != nil {
297+
log.Println("Failed to stat go.mod before running `go mod tidy -e`")
306298
}
307-
beforeGoMod.Close()
308299

309-
beforeGoSum, beforeGoSumErr := os.Open("go.sum")
310-
if beforeGoSumErr == nil {
311-
var beforeGoSumStatErr error
312-
beforeGoSumFileInfo, beforeGoSumStatErr = beforeGoSum.Stat()
313-
if beforeGoSumStatErr != nil {
314-
log.Println("Failed to stat go.sum before running `go mod tidy -e`")
315-
}
316-
}
317-
// don't print a warning if beforeGoSumErr != nil as it may be that the
318-
// file doesn't exist
319-
beforeGoSum.Close()
300+
beforeGoSumFileInfo, beforeGoSumErr := os.Stat("go.sum")
320301

321302
// run `go mod tidy -e`
322303
res := util.RunCmd(exec.Command("go", "mod", "tidy", "-e"))
@@ -325,38 +306,24 @@ func main() {
325306
log.Println("Failed to run `go mod tidy -e`")
326307
} else {
327308
if beforeGoModFileInfo != nil {
328-
afterGoMod, afterGoModErr := os.Open("go.mod")
309+
afterGoModFileInfo, afterGoModErr := os.Stat("go.mod")
329310
if afterGoModErr != nil {
330-
log.Println("Failed to read go.mod after running `go mod tidy -e`")
331-
} else {
332-
afterGoModFileInfo, afterGoModStatErr := afterGoMod.Stat()
333-
if afterGoModStatErr != nil {
334-
log.Println("Failed to stat go.mod after running `go mod tidy -e`")
335-
} else {
336-
if afterGoModFileInfo.ModTime().After(beforeGoModFileInfo.ModTime()) {
337-
// if go.mod has been changed then notify the user
338-
log.Println("We have run `go mod tidy -e` and it altered go.mod. You may wish to check these changes into version control. ")
339-
}
340-
}
311+
log.Println("Failed to stat go.mod after running `go mod tidy -e`")
312+
} else if afterGoModFileInfo.ModTime().After(beforeGoModFileInfo.ModTime()) {
313+
// if go.mod has been changed then notify the user
314+
log.Println("We have run `go mod tidy -e` and it altered go.mod. You may wish to check these changes into version control. ")
341315
}
342-
afterGoMod.Close()
343316
}
344317

345-
afterGoSum, afterGoSumErr := os.Open("go.sum")
318+
afterGoSumFileInfo, afterGoSumErr := os.Stat("go.sum")
346319
if afterGoSumErr != nil {
347-
log.Println("Failed to read go.sum after running `go mod tidy -e`")
320+
log.Println("Failed to stat go.sum after running `go mod tidy -e`")
348321
} else {
349-
afterGoSumFileInfo, afterGoSumStatErr := afterGoSum.Stat()
350-
if afterGoSumStatErr != nil {
351-
log.Println("Failed to stat go.sum after running `go mod tidy -e`")
352-
} else {
353-
if beforeGoSumErr != nil || afterGoSumFileInfo.ModTime().After(beforeGoSumFileInfo.ModTime()) {
354-
// if go.sum has been changed then notify the user
355-
log.Println("We have run `go mod tidy -e` and it altered go.sum. You may wish to check these changes into version control. ")
356-
}
322+
if beforeGoSumErr != nil || afterGoSumFileInfo.ModTime().After(beforeGoSumFileInfo.ModTime()) {
323+
// if go.sum has been changed then notify the user
324+
log.Println("We have run `go mod tidy -e` and it altered go.sum. You may wish to check these changes into version control. ")
357325
}
358326
}
359-
afterGoSum.Close()
360327

361328
}
362329
}

0 commit comments

Comments
 (0)