-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathantutils.go
77 lines (60 loc) · 2.47 KB
/
antutils.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
package utils
import (
"fmt"
"os"
"regexp"
"strings"
"github.com/jfrog/gofrog/stringutils"
"github.com/jfrog/jfrog-client-go/utils/io"
)
var (
// Replace ** with a special string.
doubleStartSpecialString = "__JFROG_DOUBLE_STAR__"
// Match **/ ('__JFROG_DOUBLE_STAR__\/...')
prefixDoubleStarRegex = regexp.MustCompile(fmt.Sprintf("%s%s", doubleStartSpecialString, getFileSeparatorForDoubleStart()))
// match /** ('\/__JFROG_DOUBLE_STAR__...')
postfixDoubleStarRegex = regexp.MustCompile(fmt.Sprintf("%s%s", getFileSeparatorForDoubleStart(), doubleStartSpecialString))
// match ** ('...__JFROG_DOUBLE_STAR__...')
middleDoubleStarNoSeparateRegex = regexp.MustCompile(doubleStartSpecialString)
)
func getFileSeparatorForDoubleStart() string {
if io.IsWindows() {
return `\\\\`
}
return `\/`
}
func AntToRegex(antPattern string) string {
antPattern = stringutils.EscapeSpecialChars(antPattern)
antPattern = antQuestionMarkToRegex(antPattern)
return "^" + antStarsToRegex(antPattern) + "$"
}
func getFileSeparatorForAntToRegex() string {
if io.IsWindows() {
return `\\`
}
return `/`
}
func antStarsToRegex(antPattern string) string {
separator := getFileSeparatorForAntToRegex()
antPattern = addMissingShorthand(antPattern)
// Replace ** with a special string, so it doesn't get mixed up with single *
antPattern = strings.ReplaceAll(antPattern, "**", doubleStartSpecialString)
// ant `*` => regexp `([^/]*)` : `*` matches zero or more characters except from `/`.
antPattern = strings.ReplaceAll(antPattern, `*`, "([^"+separator+"]*)")
// ant `**/` => regexp `(.*/)*` : Matches zero or more 'directories' at the beginning of the path.
antPattern = prefixDoubleStarRegex.ReplaceAllString(antPattern, "(.*"+separator+")*")
// ant `/**` => regexp `(/.*)*` : Matches zero or more 'directories' at the end of the path.
antPattern = postfixDoubleStarRegex.ReplaceAllString(antPattern, "("+separator+".*)*")
// ant `**` => regexp `(.*)*` : Matches zero or more 'directories'.
return middleDoubleStarNoSeparateRegex.ReplaceAllString(antPattern, "(.*)")
}
func antQuestionMarkToRegex(antPattern string) string {
return strings.ReplaceAll(antPattern, "?", ".")
}
func addMissingShorthand(antPattern string) string {
// There is one "shorthand": if a pattern ends with / or \, then ** is appended. For example, mypackage/test/ is interpreted as if it were mypackage/test/**.
if strings.HasSuffix(antPattern, string(os.PathSeparator)) {
return antPattern + "**"
}
return antPattern
}