-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
87 lines (70 loc) · 1.85 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
package vcs
import (
"regexp"
"time"
)
type VersionNumber string
var versionRe = regexp.MustCompile("^[a-zA-Z0-9/._-]+$")
const maxVersionLength = 255
// Validate validates the version strings against the assumptions the registry makes about version numbers.
func (v VersionNumber) Validate() error {
if len(v) > maxVersionLength {
return &InvalidVersionError{
Version: v,
}
}
if !versionRe.MatchString(string(v)) {
return &InvalidVersionError{
Version: v,
}
}
return nil
}
func (v VersionNumber) Equals(tag VersionNumber) bool {
return v == tag
}
type Version struct {
VersionNumber VersionNumber
Created time.Time
}
func (v Version) Validate() error {
if err := v.VersionNumber.Validate(); err != nil {
return err
}
return nil
}
func (v Version) Equals(other Version) bool {
return v.VersionNumber.Equals(other.VersionNumber) && v.Created == other.Created
}
func (v Version) String() string {
return string(v.VersionNumber)
}
type InvalidVersionError struct {
Version VersionNumber
Cause error
}
func (r InvalidVersionError) Error() string {
if r.Cause != nil {
return "Failed to parse version: " + string(r.Version) + " (" + r.Cause.Error() + ")"
}
return "Failed to parse version: " + string(r.Version)
}
func (r InvalidVersionError) Unwrap() error {
return r.Cause
}
type VersionNotFoundError struct {
RepositoryAddr RepositoryAddr
Version VersionNumber
Cause error
}
func (v VersionNotFoundError) Error() string {
if v.Cause != nil {
return "Version " + string(v.Version) + " not found in repository" + v.RepositoryAddr.String() + " (" + v.Cause.Error() + ")"
}
return "Version " + string(v.Version) + " not found in repository" + v.RepositoryAddr.String()
}
func (v VersionNotFoundError) Unwrap() error {
return v.Cause
}