-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
121 lines (109 loc) · 2.8 KB
/
file.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
package helper
import (
"os"
"path"
"strings"
)
// Get full path of current directory.
// - Return string pointer.
func CurrentPath() *string {
p, _ := os.Getwd()
return &p
}
// Get current directory name. Not full path.
// - Return string pointer.
func CurrentDirBase() *string {
p := path.Base(*CurrentPath())
return &p
}
// Return full path of path provided.
// - If <workPathP> is empty/nil, use current path.
// - <workPathP> not modified.
// - Return string pointer.
func FullPath(workPathP *string) *string {
var p string
if workPathP == nil || *workPathP == "" {
// Empty return current path
p = *CurrentPath()
} else if (*workPathP)[0] == '/' {
// Path start with / already full path, return it
p = *workPathP
} else {
// Add to current path
p = path.Join(*CurrentPath(), *workPathP)
}
return &p
}
// Return full path of path provided.
// - If <workPath> is empty, use current path.
// - Return string pointer.
func FullPathStr(workPath string) *string {
return FullPath(&workPath)
}
// Check workPath is regular file
func IsRegularFile(workPath string) bool {
fileInfo, err := os.Stat(workPath)
if err != nil {
if Debug {
ReportDebug(fileInfo, "IsRegularFile:fileInfo", false, false)
Errs.Add(err)
}
return false
}
return fileInfo.Mode().IsRegular()
}
// Check workPath is directory
func IsDir(workPath string) bool {
fileInfo, err := os.Stat(workPath)
if err != nil {
if Debug {
Errs.Add(err)
}
return false
}
return fileInfo.IsDir()
}
// Check two paths have same parent directory
func SameDir(path1, path2 string) bool {
return path.Dir(path1) == path.Dir(path2)
}
// Check file has supplied extension
func FileHasExt(name, ext string) bool {
return strings.ToLower(path.Ext(name)) == strings.ToLower(ext)
}
// Return filename/path with extension removed
func FileRemoveExt(filename string) string {
return strings.TrimSuffix(filename, path.Ext(filename))
}
// Search for file in a directory
// - filename path info removed automatically, only base is used
// - case insensitive search
// - return actually filename if found
// - return empty if not found or error
// - error is added to Errs
func FileInDir(dir, filename string) string {
var fileBase string = strings.ToLower(path.Base(filename))
dirEntry, err := os.ReadDir(dir)
if err != nil {
Errs.Add(err)
return ""
}
for _, f := range dirEntry {
if fileBase == strings.ToLower(f.Name()) {
// case insensitive matched, return real name
return f.Name()
}
}
return ""
}
// Apply following to filename:
// - remove extension
// - to lowercase
// - remove -,_
func FileSimplifyName(filename string) string {
filename = FileRemoveExt(filename)
filename = strings.ToLower(filename)
filename = strings.ReplaceAll(filename, "-", "")
filename = strings.ReplaceAll(filename, "_", "")
return filename
}