-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructs.go
101 lines (77 loc) · 1.85 KB
/
structs.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
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"fmt"
"strconv"
)
type Hash map[string]string
type OutdatedRecords map[string]*PkgItem
type PkgItem struct {
Manager string
Package string
Current string
Available string
Family string
Major string
}
func (p *PkgItem) Update(piece Hash) {
if value, ok := piece["pkg"]; ok {
p.Package = value
}
if value, ok := piece["current"]; ok {
p.Current = value
}
if value, ok := piece["available"]; ok {
p.Available = value
}
if value, ok := piece["family"]; ok {
p.Family = value
}
if value, ok := piece["major"]; ok {
p.Major = value
}
}
func (p *PkgItem) HasMajor() bool {
return p.Major != ""
}
func (p *PkgItem) IsYoungerThan(major string) bool {
our, _ := strconv.ParseFloat(p.Major, 32)
their, _ := strconv.ParseFloat(major, 32)
return our > their
}
func (p *PkgItem) Installed() bool {
return p.Current != ""
}
func (p *PkgItem) UpToDate() bool {
return p.Current == p.Available || p.Available == ""
}
func (p *PkgItem) String() string {
return fmt.Sprintf("%-8s %-20s %-10s %s", p.Manager, p.Package, p.Current, p.Available)
}
func (o OutdatedRecords) Update(mgr string, item Hash) {
if _, ok := o[item["pkg"]]; !ok {
o[item["pkg"]] = &PkgItem{Manager: mgr}
}
o[item["pkg"]].Update(item)
}
func (o OutdatedRecords) Filter() OutdatedRecords {
installed := make(Hash)
for _, item := range o {
if item.HasMajor() && item.Installed() && item.IsYoungerThan(installed[item.Family]) {
installed[item.Family] = item.Major
}
}
for name, item := range o {
major := item.HasMajor() && installed[item.Family] != "" && item.IsYoungerThan(installed[item.Family])
if !(item.Installed() || major) || item.UpToDate() {
delete(o, name)
}
}
return o
}
func (o OutdatedRecords) List() []string {
var list []string
for _, item := range o {
list = append(list, item.String())
}
return list
}