-
Notifications
You must be signed in to change notification settings - Fork 69
/
os_version_validator.go
84 lines (72 loc) · 1.92 KB
/
os_version_validator.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
package discovery
import (
"fmt"
"strconv"
"strings"
"github.com/newrelic/newrelic-cli/internal/install/types"
)
type OsVersionValidator struct {
minMajor int
minMinor int
os string
platform string
}
var (
versionNoLongerSupported = "This version of %s is no longer supported"
noVersionMessage = "Failed to identified a valid version of %s"
)
func NewOsVersionValidator(os string, platform string, minMajor int, minMinor int) *OsVersionValidator {
validator := OsVersionValidator{
minMajor: minMajor,
minMinor: minMinor,
os: os,
platform: platform,
}
return &validator
}
func (v *OsVersionValidator) Validate(m *types.DiscoveryManifest) error {
if v.os != m.OS {
return nil
}
if (m.Platform != v.platform) && v.platform != "" {
return nil
}
versions := strings.Split(m.PlatformVersion, ".")
switch len(versions) {
case 0:
return v.formatErrorMessage(noVersionMessage, m)
case 1:
major, err := strconv.Atoi(versions[0])
if err == nil {
return v.ensureMinimumVersion(major, v.minMinor-1, m)
}
return v.formatErrorMessage(noVersionMessage, m)
default:
major, aerr := strconv.Atoi(versions[0])
if aerr == nil {
minor, ierr := strconv.Atoi(versions[1])
if ierr == nil {
return v.ensureMinimumVersion(major, minor, m)
}
}
}
return v.formatErrorMessage(noVersionMessage, m)
}
func (v *OsVersionValidator) ensureMinimumVersion(major int, minor int, m *types.DiscoveryManifest) error {
if major < v.minMajor {
return v.formatErrorMessage(versionNoLongerSupported, m)
}
if major == v.minMajor {
if minor < v.minMinor {
return v.formatErrorMessage(versionNoLongerSupported, m)
}
}
return nil
}
func (v *OsVersionValidator) formatErrorMessage(message string, m *types.DiscoveryManifest) error {
targetSpecific := m.OS
if m.Platform != "" {
targetSpecific = fmt.Sprintf("%s/%s", targetSpecific, m.Platform)
}
return fmt.Errorf(message, targetSpecific)
}