/
file.go
70 lines (59 loc) · 1.57 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
package miso
import (
"fmt"
"io"
"os"
"path/filepath"
)
const (
// Default File Mode
DefFileMode = 0666
)
// Check if file exists
func FileExists(path string) (bool, error) {
_, e := os.Stat(path)
if e == nil {
return true, nil
}
if os.IsNotExist(e) {
return false, nil
}
return false, e
}
// Read all content from file.
func ReadFileAll(path string) ([]byte, error) {
f, err := ReadWriteFile(path)
if err != nil {
return nil, fmt.Errorf("failed to open file %v, %w", path, err)
}
defer f.Close()
buf, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("failed to read from file %v, %w", path, err)
}
return buf, nil
}
// Open file with 0666 permission.
func OpenFile(name string, flag int) (*os.File, error) {
return os.OpenFile(name, flag, DefFileMode)
}
// Create appendable file with 0666 permission.
func AppendableFile(name string) (*os.File, error) {
return OpenFile(name, os.O_CREATE|os.O_APPEND|os.O_WRONLY)
}
// Create readable & writable file with 0666 permission.
func ReadWriteFile(name string) (*os.File, error) {
return OpenFile(name, os.O_CREATE|os.O_RDWR)
}
// MkdirAll with 0755 perm.
func MkdirAll(path string) error {
return os.MkdirAll(path, 0755)
}
// MkdirAll but only for the parent directory of the path, perm 0755 is used.
//
// The path should always point to a specific file under some directories,
// as this method always attempts to extract parent dir of the file.
// It the path fails to fulfill this requirement, the output might be unexpected.
func MkdirParentAll(path string) error {
return MkdirAll(filepath.Dir(path))
}