Skip to content

Commit d329da6

Browse files
committed
Refactor logic for which version to install
This does not change the version returned. In the case the the go mod version is supported and the go env version is below goMinVersion, the message now talks about go env version being unsupported instead of it being less than go mod version. This seems more sensible to me.
1 parent 3f7a230 commit d329da6

File tree

1 file changed

+40
-67
lines changed

1 file changed

+40
-67
lines changed

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

Lines changed: 40 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -727,81 +727,61 @@ func outsideSupportedRange(version string) bool {
727727
return semver.Compare(short, "v"+minGoVersion) < 0 || semver.Compare(short, "v"+maxGoVersion) > 0
728728
}
729729

730-
// Check if `v.goModVersion` or `v.goEnvVersion` are outside of the supported range. If so, emit
731-
// a diagnostic and return an empty version to indicate that we should not attempt to install a
732-
// different version of Go.
733-
func checkForUnsupportedVersions(v versionInfo) (msg, version string) {
734-
if v.goModVersionFound && outsideSupportedRange(v.goModVersion) {
735-
msg = "The version of Go found in the `go.mod` file (" + v.goModVersion +
736-
") is outside of the supported range (" + minGoVersion + "-" + maxGoVersion +
737-
"). Writing an environment file not specifying any version of Go."
738-
version = ""
739-
diagnostics.EmitUnsupportedVersionGoMod(msg)
740-
}
741-
742-
return msg, version
743-
}
744-
745-
// Check if either `v.goEnvVersionFound` or `v.goModVersionFound` are false. If so, emit
746-
// a diagnostic and return the version to install, or the empty string if we should not attempt to
747-
// install a version of Go. We assume that `checkForUnsupportedVersions` has already been
748-
// called, so any versions that are found are within the supported range.
749-
func checkForVersionsNotFound(v versionInfo) (msg, version string) {
750-
if !v.goEnvVersionFound && !v.goModVersionFound {
730+
// Assuming `v.goModVersionFound` is false, emit a diagnostic and return the version to install,
731+
// or the empty string if we should not attempt to install a version of Go.
732+
func checkForGoModVersionNotFound(v versionInfo) (msg, version string) {
733+
if !v.goEnvVersionFound {
751734
msg = "No version of Go installed and no `go.mod` file found. Writing an environment " +
752735
"file specifying the maximum supported version of Go (" + maxGoVersion + ")."
753736
version = maxGoVersion
754737
diagnostics.EmitNoGoModAndNoGoEnv(msg)
755-
}
756-
757-
if !v.goEnvVersionFound && v.goModVersionFound {
758-
msg = "No version of Go installed. Writing an environment file specifying the version " +
759-
"of Go found in the `go.mod` file (" + v.goModVersion + ")."
760-
version = v.goModVersion
761-
diagnostics.EmitNoGoEnv(msg)
762-
}
763-
764-
if v.goEnvVersionFound && !v.goModVersionFound {
765-
if outsideSupportedRange(v.goEnvVersion) {
766-
msg = "No `go.mod` file found. The version of Go installed in the environment (" +
767-
v.goEnvVersion + ") is outside of the supported range (" + minGoVersion + "-" +
768-
maxGoVersion + "). Writing an environment file specifying the maximum supported " +
769-
"version of Go (" + maxGoVersion + ")."
770-
version = maxGoVersion
771-
diagnostics.EmitNoGoModAndGoEnvUnsupported(msg)
772-
} else {
773-
msg = "No `go.mod` file found. Version " + v.goEnvVersion + " installed in the " +
774-
"environment is supported. Writing an environment file not specifying any " +
775-
"version of Go."
776-
version = ""
777-
diagnostics.EmitNoGoModAndGoEnvSupported(msg)
778-
}
738+
} else if outsideSupportedRange(v.goEnvVersion) {
739+
msg = "No `go.mod` file found. The version of Go installed in the environment (" +
740+
v.goEnvVersion + ") is outside of the supported range (" + minGoVersion + "-" +
741+
maxGoVersion + "). Writing an environment file specifying the maximum supported " +
742+
"version of Go (" + maxGoVersion + ")."
743+
version = maxGoVersion
744+
diagnostics.EmitNoGoModAndGoEnvUnsupported(msg)
745+
} else {
746+
msg = "No `go.mod` file found. Version " + v.goEnvVersion + " installed in the " +
747+
"environment is supported. Writing an environment file not specifying any " +
748+
"version of Go."
749+
version = ""
750+
diagnostics.EmitNoGoModAndGoEnvSupported(msg)
779751
}
780752

781753
return msg, version
782754
}
783755

784-
// Compare `v.goModVersion` and `v.goEnvVersion`. emit a diagnostic and return the version to
785-
// install, or the empty string if we should not attempt to install a version of Go. We assume that
786-
// `checkForUnsupportedVersions` and `checkForVersionsNotFound` have already been called, so both
787-
// versions are found and are within the supported range.
788-
func compareVersions(v versionInfo) (msg, version string) {
789-
if semver.Compare("v"+v.goModVersion, "v"+v.goEnvVersion) > 0 {
790-
msg = "The version of Go installed in the environment (" + v.goEnvVersion +
791-
") is lower than the version found in the `go.mod` file (" + v.goModVersion +
792-
"). Writing an environment file specifying the version of Go from the `go.mod` " +
793-
"file (" + v.goModVersion + ")."
756+
// Assuming `v.goModVersionFound` is true, emit a diagnostic and return the version to install,
757+
// or the empty string if we should not attempt to install a version of Go.
758+
func checkForGoModVersionFound(v versionInfo) (msg, version string) {
759+
if outsideSupportedRange(v.goModVersion) {
760+
msg = "The version of Go found in the `go.mod` file (" + v.goModVersion +
761+
") is outside of the supported range (" + minGoVersion + "-" + maxGoVersion +
762+
"). Writing an environment file not specifying any version of Go."
763+
version = ""
764+
diagnostics.EmitUnsupportedVersionGoMod(msg)
765+
} else if !v.goEnvVersionFound {
766+
msg = "No version of Go installed. Writing an environment file specifying the version " +
767+
"of Go found in the `go.mod` file (" + v.goModVersion + ")."
794768
version = v.goModVersion
795-
diagnostics.EmitVersionGoModHigherVersionEnvironment(msg)
769+
diagnostics.EmitNoGoEnv(msg)
796770
} else if outsideSupportedRange(v.goEnvVersion) {
797771
msg = "The version of Go installed in the environment (" + v.goEnvVersion +
798772
") is outside of the supported range (" + minGoVersion + "-" + maxGoVersion + "). " +
799773
"Writing an environment file specifying the version of Go from the `go.mod` file (" +
800774
v.goModVersion + ")."
801775
version = v.goModVersion
802776
diagnostics.EmitVersionGoModSupportedAndGoEnvUnsupported(msg)
777+
} else if semver.Compare("v"+v.goModVersion, "v"+v.goEnvVersion) > 0 {
778+
msg = "The version of Go installed in the environment (" + v.goEnvVersion +
779+
") is lower than the version found in the `go.mod` file (" + v.goModVersion +
780+
"). Writing an environment file specifying the version of Go from the `go.mod` " +
781+
"file (" + v.goModVersion + ")."
782+
version = v.goModVersion
783+
diagnostics.EmitVersionGoModHigherVersionEnvironment(msg)
803784
} else {
804-
805785
msg = "The version of Go installed in the environment (" + v.goEnvVersion +
806786
") is supported and is high enough for the version found in the `go.mod` file (" +
807787
v.goModVersion + "). Writing an environment file not specifying any version of Go."
@@ -815,18 +795,11 @@ func compareVersions(v versionInfo) (msg, version string) {
815795
// Check the versions of Go found in the environment and in the `go.mod` file, and return a
816796
// version to install. If the version is the empty string then no installation is required.
817797
func getVersionToInstall(v versionInfo) (msg, version string) {
818-
msg, version = checkForUnsupportedVersions(v)
819-
if msg != "" {
820-
return msg, version
798+
if !v.goModVersionFound {
799+
return checkForGoModVersionNotFound(v)
821800
}
822801

823-
msg, version = checkForVersionsNotFound(v)
824-
if msg != "" {
825-
return msg, version
826-
}
827-
828-
msg, version = compareVersions(v)
829-
return msg, version
802+
return checkForGoModVersionFound(v)
830803
}
831804

832805
// Write an environment file to the current directory. If `version` is the empty string then

0 commit comments

Comments
 (0)