-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile.go
68 lines (59 loc) · 1.04 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
package goist
import (
"encoding/csv"
"os"
"path"
)
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func FileCreate(filename string) (*os.File, error) {
flag := os.O_RDWR | os.O_CREATE | os.O_TRUNC
return os.OpenFile(filename, flag, 0640)
}
func CsvWrite(filename string, records [][]string) (err error) {
exists, errE := PathExists(filename)
if errE != nil {
return errE
}
var f *os.File
if !exists {
f, err = FileCreate(filename)
if err != nil {
return err
}
}
w := csv.NewWriter(f)
err = w.WriteAll(records)
if err != nil {
return
}
w.Flush()
if err = w.Error(); err != nil {
return err
}
return nil
}
func PathClean(dir string) (err error) {
dirEntry, errRead := os.ReadDir(dir)
if errRead != nil {
if os.IsNotExist(errRead) {
return
}
return errRead
}
for _, d := range dirEntry {
err = os.RemoveAll(path.Join(dir, d.Name()))
if err != nil {
return
}
}
return
}