-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
156 lines (140 loc) · 3.47 KB
/
utils.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
package dsync
import (
"hash/crc32"
"io"
"io/fs"
"slices"
"strconv"
"strings"
"unicode"
"github.com/pkg/errors"
)
type state int
const (
stateReadVersion state = iota
stateReadName
stateReadSeparators
)
type parserError struct {
pos int
filename string
}
func (pe parserError) Error() string {
return pe.filename + ": invalid character in migration file name at " + strconv.FormatInt(int64(pe.pos), 10)
}
// ParseMigration Parse migration information from file name
func ParseMigration(filename string) (*Migration, error) {
var pos = 0
var migration Migration
var separatorsCount = 0
var builder strings.Builder
var _state = stateReadVersion
reader := strings.NewReader(filename)
for {
r, _, err := reader.ReadRune()
if err != nil {
if err == io.EOF {
switch _state {
case stateReadName:
migration.File = filename
migration.Name = builder.String()
return &migration, nil
case stateReadSeparators:
fallthrough
case stateReadVersion:
return nil, parserError{pos: pos, filename: filename}
}
} else {
return nil, errors.Wrap(err, "error parsing migration file info")
}
}
switch _state {
case stateReadVersion:
if !unicode.IsDigit(r) {
_version, err := strconv.ParseInt(builder.String(), 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "%s:%d error parsing migration file name", filename, pos)
}
_state = stateReadSeparators
reader.UnreadRune()
migration.Version = _version
builder.Reset()
} else {
builder.WriteRune(r)
}
case stateReadSeparators:
if r != '_' {
if separatorsCount == 2 {
_state = stateReadName
reader.UnreadRune()
} else {
return nil, parserError{pos: pos, filename: filename}
}
} else if separatorsCount > 2 {
return nil, parserError{pos: pos, filename: filename}
} else {
separatorsCount++
}
case stateReadName:
builder.WriteRune(r)
}
pos++
}
}
// HashFile Calculate file content checksum using CRC32(IEEE)
func HashFile(_fs fs.FS, filename string) (int64, error) {
var buf []byte
var h = crc32.New(crc32.MakeTable(crc32.IEEE))
buf = make([]byte, 1024)
file, err := _fs.Open(filename)
if err != nil {
return 0, errors.Wrap(err, "failed to calculate file hash")
}
defer file.Close()
for {
r, err := file.Read(buf)
if err != nil {
if err == io.EOF {
return int64(h.Sum32()), nil
}
return 0, err
}
h.Write(buf[:r])
}
}
// ExtractVersion Extract version from a migration changeset file
func ExtractVersion(filename string) (version int64, err error) {
versionString, _, found := strings.Cut(filename, "__")
if !found {
return 0, errors.Errorf("invalid file name format")
}
versionString = strings.TrimLeft(versionString, "0")
version, err = strconv.ParseInt(versionString, 10, 64)
if err != nil {
return 0, errors.Wrapf(err, "error parsing version number %q", filename)
}
return
}
// SortDirectoryEntries Sorts the slice in place using the library's naming scheme.
//
// NOTE: This function should not be treated as a validation function.
func SortDirectoryEntries(entries []fs.DirEntry) (status error) {
defer func() {
p := recover()
if p != nil {
status = p.(error)
}
}()
slices.SortStableFunc(entries, func(a, b fs.DirEntry) int {
entryAVersion, err := ExtractVersion(a.Name())
if err != nil {
panic(err)
}
entryBVersion, err := ExtractVersion(b.Name())
if err != nil {
panic(err)
}
return int(entryAVersion - entryBVersion)
})
return
}