-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathutils.go
65 lines (56 loc) · 1.07 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
package main
import (
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
)
func extension(path string) string {
return strings.TrimLeft(strings.ToLower(filepath.Ext(path)), ".")
}
func fileInfo(path string) os.FileInfo {
fi, err := os.Stat(path)
if err != nil {
panic(err)
}
return fi
}
func lookup(names []string, val string) string {
for _, name := range names {
val, ok := os.LookupEnv(name)
if ok && val != "" {
return val
}
}
return val
}
func remove(path string) {
go func() {
cmd, ok := os.LookupEnv("WALK_REMOVE_CMD")
if !ok {
_ = os.RemoveAll(path)
} else {
_ = exec.Command(cmd, path).Run()
}
}()
}
func leaveOnlyAscii(content []byte) string {
var result []byte
for _, b := range content {
if b == '\t' {
result = append(result, ' ', ' ', ' ', ' ')
} else if b == '\r' {
continue
} else if (b >= 32 && b <= 127) || b == '\n' { // '\n' is kept if newline needs to be retained
result = append(result, b)
}
}
return string(result)
}
func permBit(bit fs.FileMode, c byte) byte {
if bit != 0 {
return c
}
return '-'
}