forked from fsjobwd/loggeradapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
219 lines (176 loc) · 4.97 KB
/
parser.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
208
209
210
211
212
213
214
215
216
217
218
219
package loggeradapter
import (
"errors"
"fmt"
"regexp"
"strconv"
)
const (
regexpDataDuration = `(?i)^(\d+)(y|year|M|month|mo|mon|w|week|d|day|h|hour|m|minute|min|s|second)$`
regexpDataFileSize = `(?i)^(\d+)(b|byte|kb|kilobyte|mb|megabyte|gb|gigabyte|tb|terabyte)$` // |pb|petabyte|zb|zettabyte
regexpDuration = `(?i)^(y|year|M|month|mo|mon|w|week|d|day|h|hour|m|minute|min|s|second)$`
regexpFileSize = `(?i)^(b|byte|kb|kiloByte|mb|megabyte|gb|gigabyte|tb|terabyte)$` // |pb|petabyte|zb|zettabyte
regexpFixedInterval = `(?i)^(annually|monthly|weekly|daily|hourly|minutely|secondly)$`
regexpYear = `(?i)^(y|year)$`
regexpMonth = `(?i)^(month|mo|mon)$`
regexpMonthM = `^(M)$`
regexpWeek = `(?i)^(w|week)$`
regexpDay = `(?i)^(d|day)$`
regexpHour = `(?i)^(h|hour)$`
regexpMinute = `(?i)^(minute|min)$`
regexpMinuteM = `^(m)$`
regexpSecond = `(?i)^(s|second)$`
regexpByte = `(?i)^(b|byte)$`
regexpKB = `(?i)^(kb|kilobyte)$`
regexpMB = `(?i)^(mb|megabyte)$`
regexpGB = `(?i)^(gb|gigabyte)$`
regexpTB = `(?i)^(tb|terabyte)$`
regexpFixedAnnually = `(?i)^(annually)$`
regexpFixedMonthly = `(?i)^(monthly)$`
regexpFixedWeekly = `(?i)^(weekly)$`
regexpFixedDaily = `(?i)^(daily)$`
regexpFixedHourly = `(?i)^(hourly)$`
regexpFixedMinutely = `(?i)^(minutely)$`
regexpFixedSecondly = `(?i)^(secondly)$`
)
// ParseExpression match and parse expression
//
// rotation: [y/M/w/d/h/m/s] / [b/kb/mb/gb/tb] / [annually|monthly|weekly|daily|hourly|minutely|secondly]
//
// retain/archive: [y/M/w/d/h/m/s] / [<number>]
func ParseExpression(expression string) (int, string, error) {
if IsFixedInterval(expression) {
return parseFixedInterval(expression)
}
matches, err := parseExpression(regexpDataDuration, expression)
if err == nil {
return parseMatches(matches)
}
matches, err = parseExpression(regexpDataFileSize, expression)
if err == nil {
return parseMatches(matches)
}
var v int
v, err = strconv.Atoi(expression)
if err != nil {
return 0, "", fmt.Errorf("invalid expression: %s", expression)
}
return v, "", nil
}
func parseExpression(regexpPattern string, expression string) ([]string, error) {
regex, err := regexp.Compile(regexpPattern)
if err != nil {
return nil, fmt.Errorf("invalid regexp: %s", regexpPattern)
}
matches := regex.FindStringSubmatch(expression)
if len(matches) != 3 {
return nil, fmt.Errorf("invalid expression: %s", expression)
}
return matches, nil
}
func parseMatches(matches []string) (int, string, error) {
if len(matches) != 3 {
return 0, "", errors.New("not enough matches")
}
vs := matches[1]
unit := matches[2]
v, _err := strconv.Atoi(vs)
if _err != nil {
return 0, "", fmt.Errorf("invalid expression value: %s", vs)
}
return v, unit, nil
}
func parseFixedInterval(expression string) (int, string, error) {
if IsFixedAnnually(expression) {
return 1, "y", nil
}
if IsFixedMonthly(expression) {
return 1, "M", nil
}
if IsFixedWeekly(expression) {
return 1, "w", nil
}
if IsFixedDaily(expression) {
return 1, "d", nil
}
if IsFixedHourly(expression) {
return 1, "h", nil
}
if IsFixedMinutely(expression) {
return 1, "m", nil
}
if IsFixedSecondly(expression) {
return 1, "s", nil
}
return 0, "", fmt.Errorf("invalid expression: %s", expression)
}
func Match(pattern string, s string) bool {
matched, _ := regexp.MatchString(pattern, s)
return matched
}
func IsFixedInterval(unit string) bool {
return Match(regexpFixedInterval, unit)
}
func IsDuration(unit string) bool {
return Match(regexpDuration, unit)
}
func IsFileSize(unit string) bool {
return Match(regexpFileSize, unit)
}
func IsYear(unit string) bool {
return Match(regexpYear, unit)
}
func IsMonth(unit string) bool {
return Match(regexpMonth, unit) || Match(regexpMonthM, unit)
}
func IsWeek(unit string) bool {
return Match(regexpWeek, unit)
}
func IsDay(unit string) bool {
return Match(regexpDay, unit)
}
func IsHour(unit string) bool {
return Match(regexpHour, unit)
}
func IsMinute(unit string) bool {
return Match(regexpMinute, unit) || Match(regexpMinuteM, unit)
}
func IsSecond(unit string) bool {
return Match(regexpSecond, unit)
}
func IsByte(unit string) bool {
return Match(regexpByte, unit)
}
func IsKB(unit string) bool {
return Match(regexpKB, unit)
}
func IsMB(unit string) bool {
return Match(regexpMB, unit)
}
func IsGB(unit string) bool {
return Match(regexpGB, unit)
}
func IsTB(unit string) bool {
return Match(regexpTB, unit)
}
func IsFixedAnnually(unit string) bool {
return Match(regexpFixedAnnually, unit)
}
func IsFixedMonthly(unit string) bool {
return Match(regexpFixedMonthly, unit)
}
func IsFixedWeekly(unit string) bool {
return Match(regexpFixedWeekly, unit)
}
func IsFixedDaily(unit string) bool {
return Match(regexpFixedDaily, unit)
}
func IsFixedHourly(unit string) bool {
return Match(regexpFixedHourly, unit)
}
func IsFixedMinutely(unit string) bool {
return Match(regexpFixedMinutely, unit)
}
func IsFixedSecondly(unit string) bool {
return Match(regexpFixedSecondly, unit)
}