internal/stdlib: add semanticVersion function
Logic for resolving the semantic version in Zip is moved to the
function semanticVersion.
For golang/go#39973
@@ -273,42 +273,11 @@ func Zip(requestedVersion string) (_ *zip.Reader, commitTime time.Time, _ string
// https://github.com/shurcooL/play/blob/master/256/moduleproxy/std/std.go.
defer derrors.Wrap(&err, "stdlib.Zip(%q)", requestedVersion)
- knownVersions, err := Versions()
+ resolvedVersion, err := semanticVersion(requestedVersion)
if err != nil {
return nil, time.Time{}, "", err
}
- var resolvedVersion string
- switch requestedVersion {
- case "latest":
- var latestVersion string
- for _, v := range knownVersions {
- versionType, err := version.ParseType(v)
- if err != nil {
- return nil, time.Time{}, "", err
- }
- if versionType != version.TypeRelease {
- // We expect there to always be at least 1 release version.
- continue
- }
- if semver.Compare(v, latestVersion) > 0 {
- latestVersion = v
- }
- }
- resolvedVersion = latestVersion
- default:
- for _, v := range knownVersions {
- if v == requestedVersion {
- resolvedVersion = requestedVersion
- break
- }
- }
- }
-
- if resolvedVersion == "" {
- return nil, time.Time{}, "", fmt.Errorf("%w: requested version unknown: %q", derrors.InvalidArgument, requestedVersion)
- }
-
var repo *git.Repository
if UseTestData {
repo, err = getTestGoRepo(resolvedVersion)
@@ -359,6 +328,41 @@ func Zip(requestedVersion string) (_ *zip.Reader, commitTime time.Time, _ string
return zr, commit.Committer.When, resolvedVersion, nil
}
+// semanticVersion returns the semantic version corresponding to the
+// requestedVersion.
+func semanticVersion(requestedVersion string) (_ string, err error) {
+ defer derrors.Wrap(&err, "semanticVersion(%q)", requestedVersion)
+
+ knownVersions, err := Versions()
+ if err != nil {
+ return "", err
+ }
+ if requestedVersion == "latest" {
+ var latestVersion string
+ for _, v := range knownVersions {
+ versionType, err := version.ParseType(v)
+ if err != nil {
+ return "", err
+ }
+ if versionType != version.TypeRelease {
+ // We expect there to always be at least 1 release version.
+ continue
+ }
+ if semver.Compare(v, latestVersion) > 0 {
+ latestVersion = v
+ }
+ }
+ return latestVersion, nil
+ }
+
+ for _, v := range knownVersions {
+ if v == requestedVersion {
+ return requestedVersion, nil
+ }
+ }
+ return "", fmt.Errorf("%w: requested version unknown: %q", derrors.InvalidArgument, requestedVersion)
+}
+
// addFiles adds the files in t to z, using dirpath as the path prefix.
// If recursive is true, it also adds the files in all subdirectories.
func addFiles(z *zip.Writer, r *git.Repository, t *object.Tree, dirpath string, recursive bool) (err error) {
