-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
71 lines (61 loc) · 1.27 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
package goutility
import (
"io"
"os"
"io/ioutil"
"path/filepath"
)
// CopyFile copy a file from src to dst
func CopyFile(src string, dst string) (err error) {
// http://stackoverflow.com/a/21067803
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
return
}
err = out.Sync()
return
}
// FileExists check if a file exists
func FileExists(path string) (bool, error) {
// http://stackoverflow.com/a/10510783
absPath, absPathError := filepath.Abs(path)
if absPathError != nil {
return false, absPathError
}
_, err := os.Stat(absPath)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
func ReadFile(filename string) ([]byte, ErrorTypeInterface) {
fileContents, error := ioutil.ReadFile(filename)
if error != nil {
return fileContents, MakeReadFileError(filename, error)
}
return fileContents, nil
}
func WriteFile(filename string, data []byte, perm os.FileMode) ErrorTypeInterface {
error := ioutil.WriteFile(filename, data, perm)
if error != nil {
return MakeWriteFileError(filename, error)
}
return nil
}