-
Notifications
You must be signed in to change notification settings - Fork 1
/
productrecord.go
88 lines (63 loc) · 1.6 KB
/
productrecord.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
88
package cicada
import (
"github.com/Masterminds/semver"
"regexp"
"strconv"
"time"
)
// SemVerPattern matches semantic versions.
var SemVerPattern = regexp.MustCompile(`^(?P<semver>[0-9]+(\.[0-9](\.[0-9])?)?).*$`)
// ProductRecords models endoflife.date product detail records.
type ProductRecords []map[string]interface{}
// ProductRecordsToSchedules converts ProductRecords to Schedule arrays.
func ProductRecordsToSchedules(name string, records ProductRecords) ([]Schedule, error) {
var schedules []Schedule
semVerIndex := SemVerPattern.SubexpIndex("semver")
for _, record := range records {
codename := record["codename"]
var cn string
if c, ok := codename.(string); ok {
cn = c
}
cycle := record["cycle"]
var version *semver.Version
if c, ok := cycle.(string); ok {
match := SemVerPattern.FindStringSubmatch(c)
if len(match) <= semVerIndex {
continue
}
d := match[semVerIndex]
v, err := semver.NewVersion(d)
if err != nil {
return nil, err
}
version = v
} else if c, ok := cycle.(int); ok {
majorString := strconv.Itoa(c)
v, err := semver.NewVersion(majorString)
if err != nil {
return nil, err
}
version = v
}
schedule := Schedule{
Name: name,
Codename: cn,
Version: *version,
}
eol := record["eol"]
var expiration *time.Time
if e, ok := eol.(string); ok {
exp, err := time.Parse(RFC3339DateFormat, e)
if err != nil {
return nil, err
}
expiration = &exp
}
if expiration != nil {
schedule.Expiration = expiration
}
schedules = append(schedules, schedule)
}
return schedules, nil
}