-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatcher.go
207 lines (180 loc) · 4.1 KB
/
matcher.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package gosnap
import (
"errors"
"fmt"
"image"
"strings"
"sync"
"time"
"github.com/ecwid/gosnap/registry"
"github.com/google/uuid"
)
var (
defaultRegistry registry.Abstract
)
func SetRegistry(r registry.Abstract) {
defaultRegistry = r
}
func getUnixTs() int64 {
return time.Now().Unix()
}
type Matcher struct {
runID string
addChange bool
approvalEnabled bool
update bool
forceUpdate bool
normalize bool
approvalKey string
distance int
hashSize uint
data map[string]string
sync Synced
path []string
}
func NewMatcher(runID string) Matcher {
return Matcher{
runID: runID,
approvalKey: "",
approvalEnabled: true,
addChange: true,
update: false,
forceUpdate: false,
normalize: false,
distance: 6,
hashSize: 1024,
sync: NewSyncedOps(),
data: map[string]string{},
}
}
func (m Matcher) NormalizeSize(enable bool) Matcher {
m.normalize = enable
return m
}
func (m Matcher) Update(enable bool) Matcher {
m.update = enable
return m
}
func (m Matcher) ForceUpdate(enable bool) Matcher {
m.forceUpdate = enable
return m
}
func (m Matcher) HashSize(bits uint) Matcher {
m.hashSize = bits
return m
}
func (m Matcher) Delta(delta int) Matcher {
m.distance = delta
return m
}
func (m Matcher) ApprovalEnabled(enable bool, key string) Matcher {
m.approvalEnabled = enable
m.approvalKey = key
return m
}
func (m Matcher) Metadata(key string, value any) Matcher {
m.data[key] = fmt.Sprint(value)
return m
}
func (m Matcher) SnapshotSource(args ...string) Matcher {
m.path = append(m.path, args...)
return m
}
func (m Matcher) prependPathString() string {
if len(m.path) > 0 {
return strings.Join(m.path, "/") + "/"
}
return ""
}
func (m Matcher) addChangeForApproval(compareError error) error {
if err, ok := compareError.(Change); ok && m.addChange {
syncError := m.sync.Sync(func() error {
return addChanges(m.runID, err)
})
if syncError != nil {
return errors.Join(compareError, errors.New("can't add changes for approval"), syncError)
}
err.approveLabel = m.runID
return err
}
return compareError
}
func (m Matcher) generateKey() string {
return m.prependPathString() + uuid.NewString()
}
func Upload(image image.Image, hash Hash) (string, error) {
return Query{matcher: Matcher{}}.uploadSnapshot(hash, image)
}
func DefaultCompare(expected, actual image.Image) error {
const (
hashSize = 1024
distance = 6
)
var (
baselineHash = MakeHash(expected, hashSize)
targetHash = MakeHash(actual, hashSize)
)
xorHash, equal := baselineHash.equal(targetHash, distance)
if equal {
return nil
}
baseline, err := Upload(expected, baselineHash)
if err != nil {
return err
}
return Query{matcher: Matcher{}}.UploadChange(Change{
Key: baseline,
XorHash: xorHash,
TargetHash: targetHash,
target: actual,
})
}
type Synced struct {
value *sync.Mutex
}
func (s Synced) Sync(cb func() error) error {
s.value.Lock()
defer s.value.Unlock()
return cb()
}
func NewSyncedOps() Synced {
return Synced{
value: &sync.Mutex{},
}
}
func (s Synced) Accept(key string, hash Hash, approver string) error {
return s.Sync(func() error {
var approvals = new(Approvals)
err := approvals.Pull(key)
if err != nil && !errors.Is(err, registry.ErrNoSuchKey) {
return err
}
approvals.accept(Approval{Hash: hash, Approver: approver})
return approvals.Push(key)
})
}
func (s Synced) Decline(key string, hash Hash) error {
return s.Sync(func() error {
var approvals = new(Approvals)
if err := approvals.Pull(key); err != nil {
return err
}
approvals.decline(hash)
return approvals.Push(key)
})
}
func (s Synced) CopySnapshot(src, dest, author string) error {
return s.Sync(func() error {
var snapshot = new(Snapshot)
if err := snapshot.Pull(src); err != nil {
return err
}
snapshot.Metadata["author"] = author
return snapshot.Push(dest)
})
}
func (s Synced) DeleteChanges(key string, change *string) error {
return s.Sync(func() error {
return deleteChanges(key, change)
})
}