Skip to content

Commit 1d6f09c

Browse files
committed
Go: Refactor go.mod version retrieval into its own method
1 parent 44a16ce commit 1d6f09c

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

go/extractor/project/project.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ type GoModule struct {
3636
Module *modfile.File // The parsed contents of the `go.mod` file
3737
}
3838

39+
// Tries to find the Go toolchain version required for this module.
40+
func (module *GoModule) RequiredGoVersion() util.SemVer {
41+
if module.Module != nil && module.Module.Go != nil {
42+
return util.NewSemVer(module.Module.Go.Version)
43+
} else {
44+
return tryReadGoDirective(module.Path)
45+
}
46+
}
47+
3948
// Represents information about a Go project workspace: this may either be a folder containing
4049
// a `go.work` file or a collection of `go.mod` files.
4150
type GoWorkspace struct {
@@ -67,17 +76,10 @@ func (workspace *GoWorkspace) RequiredGoVersion() util.SemVer {
6776
// Otherwise, if we have `go.work` files, find the greatest Go version in those.
6877
var greatestVersion util.SemVer = nil
6978
for _, module := range workspace.Modules {
70-
if module.Module != nil && module.Module.Go != nil {
71-
// If we have parsed the file, retrieve the version number we have already obtained.
72-
modVersion := util.NewSemVer(module.Module.Go.Version)
73-
if greatestVersion == nil || modVersion.IsNewerThan(greatestVersion) {
74-
greatestVersion = modVersion
75-
}
76-
} else {
77-
modVersion := tryReadGoDirective(module.Path)
78-
if modVersion != nil && (greatestVersion == nil || modVersion.IsNewerThan(greatestVersion)) {
79-
greatestVersion = modVersion
80-
}
79+
modVersion := module.RequiredGoVersion()
80+
81+
if modVersion != nil && (greatestVersion == nil || modVersion.IsNewerThan(greatestVersion)) {
82+
greatestVersion = modVersion
8183
}
8284
}
8385

0 commit comments

Comments
 (0)