-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
77 lines (63 loc) · 1.31 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
package oax
import (
"io/fs"
"os"
"os/user"
"path/filepath"
"sort"
"strings"
)
func createDirIfNotExist(dirPath string) error {
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
return os.Mkdir(dirPath, 0755)
}
return nil
}
func replaceTildeWithHomedir(path string) (string, error) {
if strings.HasPrefix(path, "~") {
usr, err := user.Current()
if err != nil {
return "", err
}
homeDir := usr.HomeDir
path = strings.Replace(path, "~", homeDir, 1)
}
return path, nil
}
func replaceHomedirWithTilde(path string) (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
homedir := usr.HomeDir
if strings.HasPrefix(path, homedir) {
return strings.Replace(path, homedir, "~", 1), nil
}
return path, nil
}
type FileInfo struct {
FileFullPath string
FileName string
}
func ListFiles(dir string) (fileInfos []FileInfo, err error) {
err = filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
fileInfo := FileInfo{
FileName: filepath.Base(path),
FileFullPath: path,
}
fileInfos = append(fileInfos, fileInfo)
}
return nil
})
if err != nil {
return
}
sort.Slice(fileInfos, func(i, j int) bool {
return fileInfos[i].FileName > fileInfos[j].FileName
})
return
}