-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.go
84 lines (70 loc) · 1.75 KB
/
util.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
package main
import (
"os"
"path"
"regexp"
"strings"
)
var (
EnvVarRegexp = regexp.MustCompile(`\$(\w+)`)
UrlSchemaRegexp = regexp.MustCompile(`^\w+://`)
HttpUrlSchemaRegexp = regexp.MustCompile(`^(\w+\.)+(\w+)(/.*)?$`)
CmdlineComponentRegexp = regexp.MustCompile(`(?:([^'"\s]+)|'([^']+)'|"([^"]+)")`)
)
func EscapeAmpersand(s string) string {
return strings.Replace(s, "&", "&", -1)
}
func EscapeHTML(s string) string {
s = strings.Replace(s, "&", "&", -1)
s = strings.Replace(s, "<", "<", -1)
s = strings.Replace(s, ">", ">", -1)
return s
}
func ExpandEnvVars(query string) string {
matches := EnvVarRegexp.FindAllStringSubmatch(query, -1)
for _, match := range matches {
envVar := match[0]
cleanEnvVar := match[1]
query = strings.Replace(query, envVar, os.Getenv(cleanEnvVar), 1)
}
return query
}
func ExpandPathString(query string) (bool, string) {
if query == "" {
return false, query
}
if query[0] != '/' && query[0] != '~' {
return false, query
}
if query[0] == '~' {
query = HOME + query[1:]
}
return true, path.Clean(query)
}
func IsExecutable(info os.FileInfo) bool {
if info == nil || info.IsDir() || (info.Mode().Perm()&0111) == 0 {
return false
}
return true
}
func IsUrl(query string) bool {
return UrlSchemaRegexp.MatchString(query)
}
func IsHttpUrl(query string) bool {
return HttpUrlSchemaRegexp.MatchString(query)
}
func SplitCommandline(cmdline string) []string {
components := []string{}
matches := CmdlineComponentRegexp.FindAllStringSubmatch(cmdline, -1)
for _, match := range matches {
fstNonEmptyGrp := match[0]
for _, g := range match[1:] {
if g != "" {
fstNonEmptyGrp = g
break
}
}
components = append(components, fstNonEmptyGrp)
}
return components
}