-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.go
285 lines (242 loc) · 5.06 KB
/
helpers.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
280
281
282
283
284
285
/*
Copyright © 2023 Joe Mooring <joe.mooring@veriphor.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package helpers implements utility routines for working with the filesystem.
package helpers
import (
"errors"
"fmt"
"io"
"io/fs"
"log"
"math"
"os"
"path/filepath"
"strings"
)
// IsFile reports whether path is a regular file, returning an error if path
// does not exist.
func IsFile(path string) (bool, error) {
fi, err := os.Stat(path)
if err != nil {
return false, err
}
if fi.Mode().IsRegular() {
return true, nil
}
return false, nil
}
// IsDir reports whether path is a directory, returning an error if path does
// not exist.
func IsDir(path string) (bool, error) {
fi, err := os.Stat(path)
if err != nil {
return false, err
}
if fi.Mode().IsDir() {
return true, nil
}
return false, nil
}
// Exists reports whether path exists.
func Exists(path string) (bool, error) {
_, err := os.Stat(path)
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
// IsEmpty reports if the given file or directory is empty, returning an error
// if path does not exist.
func IsEmpty(path string) (bool, error) {
fi, err := os.Stat(path)
if err != nil {
return false, err
}
if fi.IsDir() {
f, err := os.Open(path)
if err != nil {
return false, err
}
defer f.Close()
list, err := f.Readdir(0)
if err != nil {
return false, err
}
return len(list) == 0, nil
}
return fi.Size() == 0, nil
}
// CopyFile copies a file from src to dst, overwriting an existing file if
// present. Returns an error if src does not exist, or if src is a directory.
func CopyFile(src string, dst string) error {
fi, err := os.Stat(src)
if err != nil {
return err
}
if fi.IsDir() {
return fmt.Errorf("%s is not a file", src)
}
s, err := os.Open(src)
if err != nil {
return err
}
defer s.Close()
err = os.MkdirAll(filepath.Dir(dst), 0o777)
if err != nil {
return err
}
d, err := os.Create(dst)
if err != nil {
return err
}
defer func() {
if err := d.Close(); err != nil {
log.Fatal(err)
}
}()
os.Chmod(dst, fi.Mode())
_, err = io.Copy(d, s)
if err != nil {
return err
}
return nil
}
// CopyDirectoryContent copies the content of the src directory to the dst
// directory. Returns an error if src does not exist, or if src is not a
// directory.
func CopyDirectoryContent(src string, dst string) error {
fi, err := os.Stat(src)
if err != nil {
return err
}
if !fi.IsDir() {
return fmt.Errorf("%s is not a directory", src)
}
err = filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if path == src {
return nil
}
target := filepath.Join(dst, strings.TrimPrefix(path, src))
err = os.MkdirAll(filepath.Dir(target), 0o777)
if err != nil {
return err
}
if d.IsDir() {
return nil
}
source, err := os.Open(path)
if err != nil {
return err
}
defer source.Close()
destination, err := os.Create(target)
if err != nil {
return err
}
defer func() {
if err := destination.Close(); err != nil {
log.Fatal(err)
}
}()
_, err = io.Copy(destination, source)
if err != nil {
return err
}
fi, err := d.Info()
if err != nil {
return err
}
srcPerm := fi.Mode().Perm()
err = destination.Chmod(srcPerm)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return nil
}
// RemoveDirectoryContent removes all content from dir, preserving the
// directory itself. Returns an error if dir does not exist, or if dir is not a
// directory.
func RemoveDirectoryContent(dir string) error {
fi, err := os.Stat(dir)
if err != nil {
return err
}
if !fi.IsDir() {
return fmt.Errorf("%s is not a directory", dir)
}
entries, err := os.ReadDir(dir)
if err != nil {
return err
}
for _, e := range entries {
err = os.RemoveAll(filepath.Join(dir, e.Name()))
if err != nil {
return err
}
}
return nil
}
// IsInt reports whether i is an integer.
func IsInt(i any) bool {
var v float64
switch s := i.(type) {
case int:
return true
case int64:
return true
case int32:
return true
case int16:
return true
case int8:
return true
case uint:
return true
case uint64:
return true
case uint32:
return true
case uint16:
return true
case uint8:
return true
case float32:
v = float64(s)
case float64:
v = s
default:
return false
}
if v == 0 || math.Mod(v, 1) == 0 {
return true
}
return false
}
// IsString reports whether i is a string.
func IsString(i any) bool {
if _, ok := i.(string); ok {
return true
}
return false
}