...
1
16
17 package driver
18
19 import (
20 "sort"
21 "strconv"
22
23 rspb "helm.sh/helm/v3/pkg/release"
24 )
25
26
27 type records []*record
28
29 func (rs records) Len() int { return len(rs) }
30 func (rs records) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] }
31 func (rs records) Less(i, j int) bool { return rs[i].rls.Version < rs[j].rls.Version }
32
33 func (rs *records) Add(r *record) error {
34 if r == nil {
35 return nil
36 }
37
38 if rs.Exists(r.key) {
39 return ErrReleaseExists
40 }
41
42 *rs = append(*rs, r)
43 sort.Sort(*rs)
44
45 return nil
46 }
47
48 func (rs records) Get(key string) *record {
49 if i, ok := rs.Index(key); ok {
50 return rs[i]
51 }
52 return nil
53 }
54
55 func (rs *records) Iter(fn func(int, *record) bool) {
56 cp := make([]*record, len(*rs))
57 copy(cp, *rs)
58
59 for i, r := range cp {
60 if !fn(i, r) {
61 return
62 }
63 }
64 }
65
66 func (rs *records) Index(key string) (int, bool) {
67 for i, r := range *rs {
68 if r.key == key {
69 return i, true
70 }
71 }
72 return -1, false
73 }
74
75 func (rs records) Exists(key string) bool {
76 _, ok := rs.Index(key)
77 return ok
78 }
79
80 func (rs *records) Remove(key string) (r *record) {
81 if i, ok := rs.Index(key); ok {
82 return rs.removeAt(i)
83 }
84 return nil
85 }
86
87 func (rs *records) Replace(key string, rec *record) *record {
88 if i, ok := rs.Index(key); ok {
89 old := (*rs)[i]
90 (*rs)[i] = rec
91 return old
92 }
93 return nil
94 }
95
96 func (rs *records) removeAt(index int) *record {
97 r := (*rs)[index]
98 (*rs)[index] = nil
99 copy((*rs)[index:], (*rs)[index+1:])
100 *rs = (*rs)[:len(*rs)-1]
101 return r
102 }
103
104
105
106 type record struct {
107 key string
108 lbs labels
109 rls *rspb.Release
110 }
111
112
113 func newRecord(key string, rls *rspb.Release) *record {
114 var lbs labels
115
116 lbs.init()
117 lbs.set("name", rls.Name)
118 lbs.set("owner", "helm")
119 lbs.set("status", rls.Info.Status.String())
120 lbs.set("version", strconv.Itoa(rls.Version))
121
122
123 return &record{key: key, lbs: lbs, rls: rls}
124 }
125
View as plain text