forked from andeya/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
437 lines (404 loc) · 10.6 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package goutil
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
// SelfPath gets compiled executable file absolute path.
func SelfPath() string {
path, _ := filepath.Abs(os.Args[0])
return path
}
// SelfDir gets compiled executable file directory.
func SelfDir() string {
return filepath.Dir(SelfPath())
}
// RelPath gets relative path.
func RelPath(targpath string) string {
basepath, _ := filepath.Abs("./")
rel, _ := filepath.Rel(basepath, targpath)
return strings.Replace(rel, `\`, `/`, -1)
}
var curpath = SelfDir()
// SelfChdir switch the working path to my own path.
func SelfChdir() {
if err := os.Chdir(curpath); err != nil {
log.Fatal(err)
}
}
// FileExists reports whether the named file or directory exists.
func FileExists(name string) (existed bool) {
existed, _ = FileExist(name)
return
}
// FileExist reports whether the named file or directory exists.
func FileExist(name string) (existed bool, isDir bool) {
info, err := os.Stat(name)
if err != nil {
return !os.IsNotExist(err), false
}
return true, info.IsDir()
}
// SearchFile Search a file in paths.
// this is often used in search config file in /etc ~/
func SearchFile(filename string, paths ...string) (fullpath string, err error) {
for _, path := range paths {
fullpath = filepath.Join(path, filename)
existed, _ := FileExist(fullpath)
if existed {
return
}
}
err = errors.New(fullpath + " not found in paths")
return
}
// GrepFile like command grep -E
// for example: GrepFile(`^hello`, "hello.txt")
// \n is striped while read
func GrepFile(patten string, filename string) (lines []string, err error) {
re, err := regexp.Compile(patten)
if err != nil {
return
}
fd, err := os.Open(filename)
if err != nil {
return
}
lines = make([]string, 0)
reader := bufio.NewReader(fd)
prefix := ""
isLongLine := false
for {
byteLine, isPrefix, er := reader.ReadLine()
if er != nil && er != io.EOF {
return nil, er
}
if er == io.EOF {
break
}
line := string(byteLine)
if isPrefix {
prefix += line
continue
} else {
isLongLine = true
}
line = prefix + line
if isLongLine {
prefix = ""
}
if re.MatchString(line) {
lines = append(lines, line)
}
}
return lines, nil
}
// WalkDirs traverses the directory, return to the relative path.
// You can specify the suffix.
func WalkDirs(targpath string, suffixes ...string) (dirlist []string) {
if !filepath.IsAbs(targpath) {
targpath, _ = filepath.Abs(targpath)
}
err := filepath.Walk(targpath, func(retpath string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if !f.IsDir() {
return nil
}
if len(suffixes) == 0 {
dirlist = append(dirlist, RelPath(retpath))
return nil
}
_retpath := RelPath(retpath)
for _, suffix := range suffixes {
if strings.HasSuffix(_retpath, suffix) {
dirlist = append(dirlist, _retpath)
}
}
return nil
})
if err != nil {
log.Printf("utils.WalkRelDirs: %v\n", err)
return
}
return
}
// FilepathSplitExt splits the filename into a pair (root, ext) such that root + ext == filename,
// and ext is empty or begins with a period and contains at most one period.
// Leading periods on the basename are ignored; splitext('.cshrc') returns ('', '.cshrc').
func FilepathSplitExt(filename string, slashInsensitive ...bool) (root, ext string) {
insensitive := false
if len(slashInsensitive) > 0 {
insensitive = slashInsensitive[0]
}
if insensitive {
filename = FilepathSlashInsensitive(filename)
}
for i := len(filename) - 1; i >= 0 && !os.IsPathSeparator(filename[i]); i-- {
if filename[i] == '.' {
return filename[:i], filename[i:]
}
}
return filename, ""
}
// FilepathStem returns the stem of filename.
// Example:
// FilepathStem("/root/dir/sub/file.ext") // output "file"
// NOTE:
// If slashInsensitive is empty, default is false.
func FilepathStem(filename string, slashInsensitive ...bool) string {
insensitive := false
if len(slashInsensitive) > 0 {
insensitive = slashInsensitive[0]
}
if insensitive {
filename = FilepathSlashInsensitive(filename)
}
base := filepath.Base(filename)
for i := len(base) - 1; i >= 0; i-- {
if base[i] == '.' {
return base[:i]
}
}
return base
}
// FilepathSlashInsensitive ignore the difference between the slash and the backslash,
// and convert to the same as the current system.
func FilepathSlashInsensitive(path string) string {
if filepath.Separator == '/' {
return strings.Replace(path, "\\", "/", -1)
}
return strings.Replace(path, "/", "\\", -1)
}
// FilepathContains checks if the basepath path contains the subpaths.
func FilepathContains(basepath string, subpaths []string) error {
basepath, err := filepath.Abs(basepath)
if err != nil {
return err
}
for _, p := range subpaths {
p, err = filepath.Abs(p)
if err != nil {
return err
}
rel, err := filepath.Rel(basepath, p)
if err != nil {
return err
}
if strings.HasPrefix(rel, "..") {
return fmt.Errorf("%s is not include %s", basepath, p)
}
}
return nil
}
// FilepathAbsolute returns the absolute paths.
func FilepathAbsolute(paths []string) ([]string, error) {
return StringsConvert(paths, func(p string) (string, error) {
return filepath.Abs(p)
})
}
// FilepathAbsoluteMap returns the absolute paths map.
func FilepathAbsoluteMap(paths []string) (map[string]string, error) {
return StringsConvertMap(paths, func(p string) (string, error) {
return filepath.Abs(p)
})
}
// FilepathRelative returns the relative paths.
func FilepathRelative(basepath string, targpaths []string) ([]string, error) {
basepath, err := filepath.Abs(basepath)
if err != nil {
return nil, err
}
return StringsConvert(targpaths, func(p string) (string, error) {
return filepathRelative(basepath, p)
})
}
// FilepathRelativeMap returns the relative paths map.
func FilepathRelativeMap(basepath string, targpaths []string) (map[string]string, error) {
basepath, err := filepath.Abs(basepath)
if err != nil {
return nil, err
}
return StringsConvertMap(targpaths, func(p string) (string, error) {
return filepathRelative(basepath, p)
})
}
func filepathRelative(basepath, targpath string) (string, error) {
abs, err := filepath.Abs(targpath)
if err != nil {
return "", err
}
rel, err := filepath.Rel(basepath, abs)
if err != nil {
return "", err
}
if strings.HasPrefix(rel, "..") {
return "", fmt.Errorf("%s is not include %s", basepath, abs)
}
return rel, nil
}
// FilepathDistinct removes the same path and return in the original order.
// If toAbs is true, return the result to absolute paths.
func FilepathDistinct(paths []string, toAbs bool) ([]string, error) {
m := make(map[string]bool, len(paths))
ret := make([]string, 0, len(paths))
for _, p := range paths {
abs, err := filepath.Abs(p)
if err != nil {
return nil, err
}
if m[abs] {
continue
}
m[abs] = true
if toAbs {
ret = append(ret, abs)
} else {
ret = append(ret, p)
}
}
return ret, nil
}
// FilepathToSlash returns the result of replacing each separator character
// in path with a slash ('/') character. Multiple separators are
// replaced by multiple slashes.
func FilepathToSlash(paths []string) []string {
ret, _ := StringsConvert(paths, func(p string) (string, error) {
return filepath.ToSlash(p), nil
})
return ret
}
// FilepathFromSlash returns the result of replacing each slash ('/') character
// in path with a separator character. Multiple slashes are replaced
// by multiple separators.
func FilepathFromSlash(paths []string) []string {
ret, _ := StringsConvert(paths, func(p string) (string, error) {
return filepath.FromSlash(p), nil
})
return ret
}
// FilepathSame checks if the two paths are the same.
func FilepathSame(path1, path2 string) (bool, error) {
if path1 == path2 {
return true, nil
}
p1, err := filepath.Abs(path1)
if err != nil {
return false, err
}
p2, err := filepath.Abs(path2)
if err != nil {
return false, err
}
return p1 == p2, nil
}
// MkdirAll creates a directory named path,
// along with any necessary parents, and returns nil,
// or else returns an error.
// The permission bits perm (before umask) are used for all
// directories that MkdirAll creates.
// If path is already a directory, MkdirAll does nothing
// and returns nil.
// If perm is empty, default use 0755.
func MkdirAll(path string, perm ...os.FileMode) error {
var fm os.FileMode = 0755
if len(perm) > 0 {
fm = perm[0]
}
return os.MkdirAll(path, fm)
}
// WriteFile writes file, and automatically creates the directory if necessary.
// NOTE:
// If perm is empty, automatically determine the file permissions based on extension.
func WriteFile(filename string, data []byte, perm ...os.FileMode) error {
filename = filepath.FromSlash(filename)
err := MkdirAll(filepath.Dir(filename))
if err != nil {
return err
}
if len(perm) > 0 {
return ioutil.WriteFile(filename, data, perm[0])
}
var ext string
if idx := strings.LastIndex(filename, "."); idx != -1 {
ext = filename[idx:]
}
switch ext {
case ".sh", ".py", ".rb", ".bat", ".com", ".vbs", ".htm", ".run", ".App", ".exe", ".reg":
return ioutil.WriteFile(filename, data, 0755)
default:
return ioutil.WriteFile(filename, data, 0644)
}
}
// RewriteFile rewrites the file.
func RewriteFile(filename string, fn func(content []byte) (newContent []byte, err error)) error {
f, err := os.OpenFile(filename, os.O_RDWR, 0777)
if err != nil {
return err
}
defer f.Close()
content, err := ioutil.ReadAll(f)
if err != nil {
return err
}
newContent, err := fn(content)
if err != nil {
return err
}
if bytes.Equal(content, newContent) {
return nil
}
f.Seek(0, 0)
f.Truncate(0)
_, err = f.Write(newContent)
return err
}
// RewriteToFile rewrites the file to newfilename.
// If newfilename already exists and is not a directory, replaces it.
func RewriteToFile(filename, newfilename string, fn func(content []byte) (newContent []byte, err error)) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
if err != nil {
return err
}
info, err := f.Stat()
if err != nil {
return err
}
cnt, err := ioutil.ReadAll(f)
if err != nil {
return err
}
newContent, err := fn(cnt)
if err != nil {
return err
}
return WriteFile(newfilename, newContent, info.Mode())
}
// ReplaceFile replaces the bytes selected by [start, end] with the new content.
func ReplaceFile(filename string, start, end int, newContent string) error {
if start < 0 || (end >= 0 && start > end) {
return nil
}
return RewriteFile(filename, func(content []byte) ([]byte, error) {
if end < 0 || end > len(content) {
end = len(content)
}
if start > end {
start = end
}
return bytes.Replace(content, content[start:end], StringToBytes(newContent), 1), nil
})
}