-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
182 lines (164 loc) · 3.37 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package utils
import (
"io"
"io/ioutil"
"os"
"path/filepath"
)
// IsExist check path is exists, exist return true, not exist return false
func IsExist(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
// Check if error is "no such file or directory"
if _, ok := err.(*os.PathError); ok {
return false
}
return false
}
// ReadDir read path return files by os.FileInfo
func ReadDir(dirname string) ([]os.FileInfo, error) {
f, err := os.Open(dirname)
if err != nil {
return nil, err
}
list, err := f.Readdir(-1)
f.Close()
if err != nil {
return nil, err
}
return list, nil
}
// MkdirAll check the path isexist or mkdir, and check writable
func MkdirAll(path string) error {
var err error
// check path exist or create
if !IsExist(path) {
err = os.MkdirAll(path, 0755)
if err != nil {
return err
}
}
return nil
}
// GetExt get file extenstion, not contains dot
func GetExt(file string) string {
f := filepath.Ext(file)
if f == "" {
return f
}
return f[1:]
}
// CopyFile copy source file to destination file
func CopyFile(s, d string) error {
// first check the source be link
linfo, err := os.Readlink(s)
if err == nil || len(linfo) > 0 {
// if source is link, create link to destination
return os.Symlink(linfo, d)
}
// normal, create file
sf, err := os.Open(s)
if err != nil {
return err
}
defer sf.Close()
df, err := os.Create(d)
if err != nil {
return err
}
defer df.Close()
_, err = io.Copy(df, sf)
return err
}
// ReadFile read file content
func ReadFile(file string) ([]byte, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
}
// WriteFile read content to the file
func WriteFile(file string, body []byte) (int, error) {
err := MkdirAll(filepath.Dir(file))
if err != nil {
return 0, err
}
f, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
if err != nil {
return 0, err
}
n, err := f.Write(body)
f.Close()
return n, err
}
// Glob 查找指定目录下的文件列表
func Glob(base, pattern string, abs bool) ([]string, error) {
base, err := filepath.Abs(base)
if err != nil {
return nil, err
}
info, err := os.Stat(base)
if err != nil {
return nil, err
}
if !info.IsDir() {
base = filepath.Dir(base)
}
return globRecursion(base, base, pattern, abs)
}
// globRecursion 递归查找
func globRecursion(root, base, pattern string, abs bool) ([]string, error) {
matches, err := filepath.Glob(filepath.Join(base, pattern))
if err != nil {
return nil, err
}
var items []string
for i := range matches {
item := matches[i]
if !abs {
rel, err := filepath.Rel(root, item)
if err != nil {
return nil, err
}
item = rel
}
items = append(items, item)
}
file, err := os.Open(base)
if err != nil {
return nil, err
}
files, err := file.Readdir(-1)
if err != nil {
return nil, err
}
for i := range files {
if files[i].IsDir() {
child, err := globRecursion(root, filepath.Join(base, files[i].Name()), pattern, abs)
if err != nil {
return nil, err
}
items = append(items, child...)
}
}
return items, nil
}
// 删除文件
func Remove(filename string) error {
if IsExist(filename) {
return os.Remove(filename)
}
return nil
}
// 判断是否文件
func IsFile(filename string) bool {
fp, err := os.Stat(filename)
if err != nil {
return false
}
return !fp.IsDir()
}