-
Notifications
You must be signed in to change notification settings - Fork 0
/
versionComparator.go
executable file
·71 lines (58 loc) · 1.91 KB
/
versionComparator.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
package versioning
import (
"fmt"
"strings"
"github.com/Reshusk23/sr-chain-core-go/core"
)
const numComponents = 3
type versionComparator struct {
version string
major string
minor string
release string
}
// NewVersionComparator returns a new version comparator instance
func NewVersionComparator(providedVersion string) (*versionComparator, error) {
vc := &versionComparator{
version: providedVersion,
}
var err error
vc.major, vc.minor, vc.release, err = vc.splitVersionComponents(providedVersion)
if err != nil {
return nil, err
}
return vc, nil
}
func (vc *versionComparator) splitVersionComponents(version string) (string, string, string, error) {
components := strings.Split(version, ".")
if len(components) != numComponents {
return "", "", "", fmt.Errorf("%w, expected %d, got %d",
core.ErrVersionNumComponents,
numComponents,
len(components),
)
}
return components[0], components[1], components[2], nil
}
// Check compares if the provided value is compatible with the stored values. The comparison is done on all 3 components:
// major, minor and release. A wildcard in any of the components will accept any string
func (vc *versionComparator) Check(version string) error {
major, minor, release, err := vc.splitVersionComponents(version)
if err != nil {
return err
}
if major != vc.major && vc.major != "*" {
return fmt.Errorf("%w, expected version %s, got %s", core.ErrMajorVersionMismatch, vc.version, version)
}
if minor != vc.minor && vc.minor != "*" {
return fmt.Errorf("%w, expected version %s, got %s", core.ErrMinorVersionMismatch, vc.version, version)
}
if release != vc.release && vc.release != "*" {
return fmt.Errorf("%w, expected version %s, got %s", core.ErrReleaseVersionMismatch, vc.version, version)
}
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (vc *versionComparator) IsInterfaceNil() bool {
return vc == nil
}