-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfs.go
58 lines (50 loc) · 929 Bytes
/
fs.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
package utils
import (
"io/ioutil"
"os"
)
// FS - filesystem
type FS struct{}
// NewFS - returns new FS
func NewFS() FS {
return FS{}
}
// Write - write to file
func (fs FS) Write(data *[]byte, path string) error {
err := os.Truncate(path, 0)
if err != nil {
return err
}
f, err := os.OpenFile(path, os.O_RDWR, 0644)
if err != nil {
panic(err.Error())
}
defer f.Close()
f.Write(*data)
return nil
}
// Read - read file
func (fs FS) Read(path string) (*[]byte, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return &data, nil
}
// FileExist returns bool value of whether file exists
func (fs FS) FileExist(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
return false
}
// CreateFile creates empty file
func (fs FS) CreateFile(path string) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return nil
}