-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileio.go
85 lines (69 loc) · 1.98 KB
/
fileio.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
package testutils
import (
"io/fs"
"os"
"path/filepath"
"time"
"github.com/jimsnab/go-simpleutils"
)
type FileIo interface {
ReadFile(filePath string) ([]byte, error)
WriteFile(filePath string, data []byte, perm fs.FileMode) error
Stat(name string) (os.FileInfo, error)
MkdirAll(name string, perm fs.FileMode) error
RemoveAll(name string) error
IsDirectory(name string) (bool, error)
CopyFile(src, dest string) (int64, error)
ReadDir(src string) ([]fs.DirEntry, error)
FileExists(name string) (bool, error)
Walk(root string, fn filepath.WalkFunc) error
Chtimes(name string, atime, mtime time.Time) error
}
type realFileIo struct {
}
func NewFileIo() FileIo {
return &realFileIo{}
}
func (rfi *realFileIo) ReadFile(filePath string) ([]byte, error) {
return os.ReadFile(filePath)
}
func (rfi *realFileIo) WriteFile(filePath string, data []byte, perm fs.FileMode) error {
return os.WriteFile(filePath, data, perm)
}
func (rfi *realFileIo) Stat(name string) (os.FileInfo, error) {
return os.Stat(name)
}
func (rfi *realFileIo) MkdirAll(name string, perm fs.FileMode) error {
return os.MkdirAll(name, perm)
}
func (rfi *realFileIo) RemoveAll(name string) error {
err := os.RemoveAll(name)
if err == nil {
var isDir bool
if isDir, err = simpleutils.IsDirectory(name); err != nil {
return err
}
if isDir {
err = os.ErrExist
}
}
return err
}
func (rfi *realFileIo) IsDirectory(name string) (bool, error) {
return simpleutils.IsDirectory(name)
}
func (rfi *realFileIo) CopyFile(src, dest string) (int64, error) {
return simpleutils.CopyFile(src, dest)
}
func (rfi *realFileIo) ReadDir(src string) (files []fs.DirEntry, err error) {
return os.ReadDir(src)
}
func (rfi *realFileIo) FileExists(name string) (bool, error) {
return simpleutils.FileExists(name)
}
func (rfi *realFileIo) Walk(root string, fn filepath.WalkFunc) error {
return filepath.Walk(root, fn)
}
func (rfi *realFileIo) Chtimes(name string, atime, mtime time.Time) error {
return os.Chtimes(name, atime, mtime)
}