-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcheck.go
More file actions
85 lines (69 loc) · 1.72 KB
/
check.go
File metadata and controls
85 lines (69 loc) · 1.72 KB
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
// Copyright 2026 Redpanda Data, Inc.
//
// Licensed as a Redpanda Enterprise file under the Redpanda Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/redpanda-data/redpanda/blob/master/licenses/rcl.md
package main
import (
"bytes"
"fmt"
"os"
"sort"
"strings"
"sync"
"github.com/cockroachdb/errors"
"github.com/sergi/go-diff/diffmatchpatch"
)
type diff struct {
path string
diffs []diffmatchpatch.Diff
}
func (d *diff) string(differ *diffmatchpatch.DiffMatchPatch) string {
diff := differ.DiffPrettyText(d.diffs)
return fmt.Sprintf("%s:\n%s", d.path, diff)
}
type checkDiffs struct {
differ *diffmatchpatch.DiffMatchPatch
diffs []diff
mutex sync.RWMutex
}
func diffChecker() *checkDiffs {
differ := diffmatchpatch.New()
return &checkDiffs{
differ: differ,
}
}
func (c *checkDiffs) diff(path string, newData []byte) error {
data, err := os.ReadFile(path) //nolint:gosec // user's responsibility for security of passed file here
if err != nil {
return err
}
if !bytes.Equal(data, newData) {
diffs := c.differ.DiffMain(string(data), string(newData), false)
c.mutex.Lock()
c.diffs = append(c.diffs, diff{path, diffs})
c.mutex.Unlock()
}
return nil
}
func (c *checkDiffs) error() error {
c.mutex.RLock()
if len(c.diffs) == 0 {
c.mutex.RUnlock()
return nil
}
diffs := make([]diff, len(c.diffs))
copy(diffs, c.diffs)
c.mutex.RUnlock()
sort.SliceStable(diffs, func(i, j int) bool {
a, b := diffs[i], diffs[j]
return a.path < b.path
})
errs := []string{}
for _, diff := range diffs {
errs = append(errs, diff.string(c.differ))
}
return errors.New(strings.Join(errs, "\n"))
}