-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations.go
173 lines (141 loc) · 3.49 KB
/
operations.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package ef
import (
"bufio"
"io/ioutil"
"os"
"path/filepath"
"github.com/otiai10/copy"
)
// Lists all files inside the directory (if the file is a directory, otherwise throws an error).
func (f *File) List() ([]File, error) {
res := []File{}
dir, err := f.IsDir()
if err != nil {
return res, err
}
if !dir {
return res, &ErrInvalidFile{}
}
files, err := ioutil.ReadDir(f.Path)
if err != nil {
return res, err
}
for _, c := range files {
res = append(res, NewFile(f.Path, c.Name()))
}
return res, nil
}
// Reads and returns the content of the file as bytes.
func (f *File) Read() ([]byte, error) {
dir, err := f.IsDir()
if err != nil {
return []byte{}, err
}
if dir {
return []byte{}, &ErrInvalidFile{}
}
return os.ReadFile(f.Path)
}
// Reads and returns the content of the file as a string.
func (f *File) ReadString() (string, error) {
d, err := f.Read()
return string(d), err
}
// Reads and returns the content of a file as a list of strings representing the lines.
func (f *File) ReadLines() ([]string, error) {
file, err := os.Open(f.Path)
if err != nil {
return []string{}, err
}
s := bufio.NewScanner(file)
s.Split(bufio.ScanLines)
var lines []string = []string{}
for s.Scan() {
lines = append(lines, s.Text())
}
return lines, err
}
// Writes bytes into a file. Overwrites any existing data.
func (f *File) Write(data []byte) error {
return os.WriteFile(f.Path, data, 0600)
}
// Writes a string into a file. Overwrites any existing data.
func (f *File) WriteString(data string) error {
return os.WriteFile(f.Path, []byte(data), 0600)
}
// Writes a list of strings into a file as lines. Overwrites any existing data.
func (f *File) WriteLines(data []string) error {
file, err := os.OpenFile(f.Path, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
defer w.Flush()
for _, l := range data {
_, err := w.WriteString(l + "\n")
if err != nil {
return err
}
}
return nil
}
// Copies the file to a new destination. Works with both files and directories.
func (f *File) Copy(destin string) error {
stat, err := os.Stat(f.Path)
if err != nil {
return err
}
err = os.MkdirAll(filepath.Dir(destin), 0700)
if err != nil {
return err
}
if stat.Mode().IsRegular() {
data, err := os.ReadFile(f.Path)
if err != nil {
return err
}
return os.WriteFile(destin, data, stat.Mode().Perm())
}
if stat.IsDir() {
return copy.Copy(f.Path, destin)
}
return &ErrInvalidFile{}
}
// Copies the file to a new destination. Works with both files and directories.
func (f *File) Move(destin string) error {
return os.Rename(f.Path, destin)
}
// Appends bytes to a file. Is it doesn't exists, creates it.
func (f *File) Append(data []byte) error {
file, err := os.OpenFile(f.Path, os.O_APPEND|os.O_CREATE, 0600)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
defer w.Flush()
_, err = w.Write(data)
return err
}
// Appends a string to a file. Is it doesn't exists, creates it.
func (f *File) AppendString(data string) error {
return f.Append([]byte(data))
}
// Appends a list of strings representing lines to a file. Is it doesn't exists, creates it.
func (f *File) AppendLines(data []string) error {
file, err := os.OpenFile(f.Path, os.O_APPEND|os.O_CREATE, 0600)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
defer w.Flush()
for _, l := range data {
_, err := w.WriteString(l + "\n")
if err != nil {
return err
}
}
return nil
}