This repository has been archived by the owner on Jul 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem.go
279 lines (219 loc) · 4.67 KB
/
filesystem.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package fs
import (
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
copy2 "github.com/otiai10/copy"
)
var Separator = fmt.Sprintf("%c", os.PathSeparator)
// Exists check if path exists.
func Exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
// file may or may not exist. See err for details.
// Therefore, do *NOT* use !os.IsNotExist(err) to test for file existence
return false, err
}
func Get(path string) (b []byte, err error) {
return os.ReadFile(path)
}
func MustGet(path string) []byte {
b, err := os.ReadFile(path)
if err != nil {
panic(err)
}
return b
}
func GetString(path string) (content string, err error) {
b, err := Get(path)
if err != nil {
return "", err
}
return string(b), nil
}
func MustGetString(path string) string {
b, err := Get(path)
if err != nil {
panic(err)
}
return string(b)
}
func Put(path string, content []byte) error {
return ioutil.WriteFile(path, content, 0o644)
}
func PutString(path string, content string) error {
return Put(path, []byte(content))
}
func Append(path string, content []byte) error {
f, err := os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o600)
if err != nil {
return err
}
defer func(f *os.File) {
_ = f.Close()
}(f)
_, err = f.Write(content)
if err != nil {
return err
}
return nil
}
func Prepend(path string, content []byte) error {
s, err := Get(path)
if err != nil {
return err
}
content = append(content, s...)
err = Put(path, content)
return err
}
func Chmod(path string, mode fs.FileMode) error {
return os.Chmod(path, mode)
}
func Delete(paths ...string) error {
for _, path := range paths {
err := os.Remove(path)
if err != nil {
return err
}
}
return nil
}
func Move(from string, to string) error {
return os.Rename(from, to)
}
func Copy(src, dst string) error {
// Read all content of src to data
data, err := ioutil.ReadFile(src)
if err != nil {
return err
}
// Write data to dst
err = ioutil.WriteFile(dst, data, 0o644)
return err
}
func Link(src, dst string) error {
return os.Symlink(src, dst)
}
func Name(path string) string {
return filepath.Base(path)
}
func Basename(path string) string {
base := Name(path)
return base[0:strings.Index(base, ".")]
}
func Dirname(path string) string {
return filepath.Dir(path)
}
func Extension(path string) string {
base := Name(path)
if !strings.Contains(base, ".") {
return ""
}
return base[strings.Index(base, ".")+1:]
}
func Size(path string) (int64, error) {
fi, err := os.Stat(path)
if err != nil {
return 0, err
}
size := fi.Size()
return size, nil
}
func LastModified(path string) (t time.Time, err error) {
fi, err := os.Stat(path)
if err != nil {
return
}
t = fi.ModTime()
return
}
func IsDirectory(path string) (bool, error) {
fi, err := os.Stat(path)
if err != nil {
return false, err
}
return fi.IsDir(), nil
}
func IsReadable(path string) (bool, error) {
file, err := os.OpenFile(path, os.O_RDONLY, 0o666)
_ = file.Close()
return err == nil, err
}
func IsWritable(path string) (bool, error) {
file, err := os.OpenFile(path, os.O_WRONLY, 0o666)
_ = file.Close()
return err == nil, err
}
func IsFile(path string) (bool, error) {
return Exists(path)
}
// Files List the files under the folder, excluding directories.
func Files(dir string) (files []string, err error) {
fileInfos, err := ioutil.ReadDir(dir)
if err != nil {
return
}
for _, f := range fileInfos {
if !f.IsDir() {
files = append(files, f.Name())
}
}
return
}
func AllFiles(dir string) (files []string, err error) {
fileInfos, err := ioutil.ReadDir(dir)
if err != nil {
return
}
for _, f := range fileInfos {
if f.IsDir() {
allFiles, e := AllFiles(strings.Join([]string{dir, f.Name()}, Separator))
if e != nil {
err = e
return
}
newFiles := make([]string, len(allFiles))
for i, file := range allFiles {
newFiles[i] = strings.Join([]string{f.Name(), file}, Separator)
}
files = append(files, newFiles...)
} else {
files = append(files, f.Name())
}
}
return
}
func Directories(dir string) (dirs []string, err error) {
fileInfos, err := ioutil.ReadDir(dir)
if err != nil {
return
}
for _, f := range fileInfos {
if f.IsDir() {
dirs = append(dirs, f.Name())
}
}
return
}
func MakeDirectory(path string, mode os.FileMode) error {
return os.Mkdir(path, mode)
}
func MakeDirectories(path string, mode os.FileMode) error {
return os.MkdirAll(path, mode)
}
func DeleteDirectory(path string) error {
return os.RemoveAll(path)
}
func MoveDirectory(src, dst string) error {
return os.Rename(src, dst)
}
func CopyDirectory(dir, dest string) error {
return copy2.Copy(dir, dest)
}