-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.go
505 lines (433 loc) · 12.6 KB
/
dir.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package g
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
)
// SkipWalk is used as a return value from the walker function to indicate that
// the file or directory named in the call should be skipped. It is not returned
// as an error by any function.
var SkipWalk = errors.New("skip")
// StopWalk is used as a return value from the walker function to indicate that
// all remaining files and directories should be skipped. It is not returned
// as an error by any function.
var StopWalk = errors.New("stop")
// NewDir returns a new Dir instance with the given path.
func NewDir(path String) *Dir { return &Dir{path: path} }
// Chown changes the ownership of the directory to the specified UID and GID.
// It uses os.Chown to modify ownership and returns a Result[*Dir] indicating success or failure.
func (d *Dir) Chown(uid, gid int) Result[*Dir] {
err := os.Chown(d.path.Std(), uid, gid)
if err != nil {
return Err[*Dir](err)
}
return Ok(d)
}
// Stat retrieves information about the directory represented by the Dir instance.
// It returns a Result[fs.FileInfo] containing details about the directory's metadata.
func (d *Dir) Stat() Result[fs.FileInfo] {
if d.Path().IsErr() {
return Err[fs.FileInfo](d.Path().Err())
}
return ResultOf(os.Stat(d.Path().Ok().Std()))
}
// Lstat retrieves information about the symbolic link represented by the Dir instance.
// It returns a Result[fs.FileInfo] containing details about the symbolic link's metadata.
// Unlike Stat, Lstat does not follow the link and provides information about the link itself.
func (d *Dir) Lstat() Result[fs.FileInfo] {
return ResultOf(os.Lstat(d.Path().Ok().Std()))
}
// IsLink checks if the directory is a symbolic link.
func (d *Dir) IsLink() bool {
stat := d.Lstat()
return stat.IsOk() && stat.Ok().Mode()&os.ModeSymlink != 0
}
// CreateTemp creates a new temporary directory in the specified directory with the
// specified name pattern and returns a Result, which contains a pointer to the Dir
// or an error if the operation fails.
// If no directory is specified, the default directory for temporary directories is used.
// If no name pattern is specified, the default pattern "*" is used.
//
// Parameters:
//
// - args ...String: A variadic parameter specifying the directory and/or name
// pattern for the temporary directory.
//
// Returns:
//
// - *Dir: A pointer to the Dir representing the temporary directory.
//
// Example usage:
//
// d := g.NewDir("")
// tmpdir := d.CreateTemp() // Creates a temporary directory with default settings
// tmpdirWithDir := d.CreateTemp("mydir") // Creates a temporary directory in "mydir" directory
// tmpdirWithPattern := d.CreateTemp("", "tmp") // Creates a temporary directory with "tmp" pattern
func (*Dir) CreateTemp(args ...String) Result[*Dir] {
dir := ""
pattern := "*"
if len(args) != 0 {
if len(args) > 1 {
pattern = args[1].Std()
}
dir = args[0].Std()
}
tmpDir, err := os.MkdirTemp(dir, pattern)
if err != nil {
return Err[*Dir](err)
}
return Ok(NewDir(String(tmpDir)))
}
// Temp returns the default directory to use for temporary files.
//
// On Unix systems, it returns $TMPDIR if non-empty, else /tmp.
// On Windows, it uses GetTempPath, returning the first non-empty
// value from %TMP%, %TEMP%, %USERPROFILE%, or the Windows directory.
// On Plan 9, it returns /tmp.
//
// The directory is neither guaranteed to exist nor have accessible
// permissions.
func (*Dir) Temp() *Dir { return NewDir(String(os.TempDir())) }
// Remove attempts to delete the directory and its contents.
// It returns a Result, which contains either the *Dir or an error.
// If the directory does not exist, Remove returns a successful Result with *Dir set.
// Any error that occurs during removal will be of type *PathError.
func (d *Dir) Remove() Result[*Dir] {
if err := os.RemoveAll(d.ToString().Std()); err != nil {
return Err[*Dir](err)
}
return Ok(d)
}
// Copy copies the contents of the current directory to the destination directory.
//
// Parameters:
//
// - dest (String): The destination directory where the contents of the current directory should be copied.
//
// - followLinks (optional): A boolean indicating whether to follow symbolic links during the walk.
// If true, symbolic links are followed; otherwise, they are skipped.
//
// Returns:
//
// - Result[*Dir]: A Result type containing either a pointer to a new Dir instance representing the destination directory or an error.
//
// Example usage:
//
// sourceDir := g.NewDir("path/to/source")
// destinationDirResult := sourceDir.Copy("path/to/destination")
// if destinationDirResult.IsErr() {
// // Handle error
// }
// destinationDir := destinationDirResult.Ok()
func (d *Dir) Copy(dest String, followLinks ...bool) Result[*Dir] {
follow := true
if len(followLinks) != 0 {
follow = followLinks[0]
}
root := d.Path()
if root.IsErr() {
return Err[*Dir](root.Err())
}
walker := func(f *File) error {
path := f.Path()
if path.IsErr() {
return path.Err()
}
relpath, err := filepath.Rel(root.Ok().Std(), path.Ok().Std())
if err != nil {
return err
}
destpath := NewDir(dest).Join(String(relpath))
if destpath.IsErr() {
return destpath.Err()
}
stat := f.Stat()
if stat.IsErr() {
return stat.Err()
}
if stat.Ok().IsDir() {
if !follow && f.IsLink() {
return SkipWalk
}
return NewDir(destpath.Ok()).CreateAll(stat.Ok().Mode()).Err()
}
return f.Copy(destpath.Ok(), stat.Ok().Mode()).Err()
}
if err := d.Walk(walker); err != nil {
return Err[*Dir](err)
}
return Ok(NewDir(dest))
}
// Create creates a new directory with the specified mode (optional).
//
// Parameters:
//
// - mode (os.FileMode, optional): The file mode for the new directory.
// If not provided, it defaults to DirDefault (0755).
//
// Returns:
//
// - *Dir: A pointer to the Dir instance on which the method was called.
//
// Example usage:
//
// dir := g.NewDir("path/to/directory")
// createdDir := dir.Create(0755) // Optional mode argument
func (d *Dir) Create(mode ...os.FileMode) Result[*Dir] {
dmode := DirDefault
if len(mode) != 0 {
dmode = mode[0]
}
if err := os.Mkdir(d.path.Std(), dmode); err != nil {
return Err[*Dir](err)
}
return Ok(d)
}
// Join joins the current directory path with the given path elements, returning the joined path.
//
// Parameters:
//
// - elem (...String): One or more String values representing path elements to
// be joined with the current directory path.
//
// Returns:
//
// - String: The resulting joined path as an String.
//
// Example usage:
//
// dir := g.NewDir("path/to/directory")
// joinedPath := dir.Join("subdir", "file.txt")
func (d *Dir) Join(elem ...String) Result[String] {
path := d.Path()
if path.IsErr() {
return Err[String](path.Err())
}
paths := SliceOf(elem...).Insert(0, path.Ok()).ToStringSlice()
return Ok(String(filepath.Join(paths...)))
}
// SetPath sets the path of the current directory.
//
// Parameters:
//
// - path (String): The new path to be set for the current directory.
//
// Returns:
//
// - *Dir: A pointer to the updated Dir instance with the new path.
//
// Example usage:
//
// dir := g.NewDir("path/to/directory")
// dir.SetPath("new/path/to/directory")
func (d *Dir) SetPath(path String) *Dir {
d.path = path
return d
}
// CreateAll creates all directories along the given path, with the specified mode (optional).
//
// Parameters:
//
// - mode ...os.FileMode (optional): The file mode to be used when creating the directories.
// If not provided, it defaults to the value of DirDefault constant (0755).
//
// Returns:
//
// - *Dir: A pointer to the Dir instance representing the created directories.
//
// Example usage:
//
// dir := g.NewDir("path/to/directory")
// dir.CreateAll()
// dir.CreateAll(0755)
func (d *Dir) CreateAll(mode ...os.FileMode) Result[*Dir] {
if d.Exist() {
return Ok(d)
}
dmode := DirDefault
if len(mode) != 0 {
dmode = mode[0]
}
path := d.Path()
if path.IsErr() {
return Err[*Dir](path.Err())
}
err := os.MkdirAll(path.Ok().Std(), dmode)
if err != nil {
return Err[*Dir](err)
}
return Ok(d)
}
// Rename renames the current directory to the new path.
//
// Parameters:
//
// - newpath String: The new path for the directory.
//
// Returns:
//
// - *Dir: A pointer to the Dir instance representing the renamed directory.
// If an error occurs, the original Dir instance is returned with the error stored in d.err,
// which can be checked using the Error() method.
//
// Example usage:
//
// dir := g.NewDir("path/to/directory")
// dir.Rename("path/to/new_directory")
func (d *Dir) Rename(newpath String) Result[*Dir] {
ps := String(os.PathSeparator)
_, np := newpath.TrimSuffix(ps).Split(ps).Pop()
if rd := NewDir(np.Join(ps)).CreateAll(); rd.IsErr() {
return rd
}
if err := os.Rename(d.path.Std(), newpath.Std()); err != nil {
return Err[*Dir](err)
}
return Ok(NewDir(newpath))
}
// Move function simply calls [Dir.Rename]
func (d *Dir) Move(newpath String) Result[*Dir] { return d.Rename(newpath) }
// Path returns the absolute path of the current directory.
//
// Returns:
//
// - String: The absolute path of the current directory as an String.
// If an error occurs while converting the path to an absolute path,
// the error is stored in d.err, which can be checked using the Error() method.
//
// Example usage:
//
// dir := g.NewDir("path/to/directory")
// absPath := dir.Path()
func (d *Dir) Path() Result[String] {
path, err := filepath.Abs(d.path.Std())
if err != nil {
return Err[String](err)
}
return Ok(String(path))
}
// Exist checks if the current directory exists.
//
// Returns:
//
// - bool: true if the current directory exists, false otherwise.
//
// Example usage:
//
// dir := g.NewDir("path/to/directory")
// exists := dir.Exist()
func (d *Dir) Exist() bool {
path := d.Path()
if path.IsErr() {
return false
}
_, err := os.Stat(path.Ok().Std())
return !os.IsNotExist(err)
}
// Read reads the content of the current directory and returns a slice of File instances.
//
// Returns:
//
// - []*File: A slice of File instances representing the files and directories
// in the current directory.
//
// Example usage:
//
// dir := g.NewDir("path/to/directory")
// files := dir.Read()
func (d *Dir) Read() Result[Slice[*File]] {
entries, err := os.ReadDir(d.path.Std())
if err != nil {
return Err[Slice[*File]](err)
}
files := NewSlice[*File](0, len(entries))
for _, entry := range entries {
dpath := d.Path()
if dpath.IsErr() {
return Err[Slice[*File]](dpath.Err())
}
file := NewDir(dpath.Ok()).Join(String(entry.Name()))
if file.IsErr() {
return Err[Slice[*File]](file.Err())
}
files = files.Append(NewFile(file.Ok()))
}
return Ok(files)
}
// Glob matches files in the current directory using the path pattern and
// returns a slice of File instances.
//
// Returns:
//
// - []*File: A slice of File instances representing the files that match the
// provided pattern in the current directory.
//
// Example usage:
//
// dir := g.NewDir("path/to/directory/*.txt")
// files := dir.Glob()
func (d *Dir) Glob() Result[Slice[*File]] {
matches, err := filepath.Glob(d.path.Std())
if err != nil {
return Err[Slice[*File]](err)
}
files := NewSlice[*File](0, len(matches))
for _, match := range matches {
file := NewFile(String(match)).Path()
if file.IsErr() {
return Err[Slice[*File]](file.Err())
}
files = files.Append(NewFile(file.Ok()))
}
return Ok(files)
}
// Walk recursively traverses the directory structure and applies the walker function to each file and directory.
//
// Parameters:
//
// - walker: A function that takes a *File as an argument and returns an error.
// It is applied to each file and directory encountered during the walk.
//
// Returns:
//
// - error: An error indicating any issues that occurred during the walk. If no errors occurred, it returns nil.
func (d *Dir) Walk(walker func(f *File) error) error {
entries := d.Read()
if entries.IsErr() {
return entries.Err()
}
for _, entry := range entries.Ok() {
if err := walker(entry); err != nil {
switch {
case errors.Is(err, SkipWalk):
continue
case errors.Is(err, StopWalk):
return nil
default:
return err
}
}
stat := entry.Stat()
if stat.IsErr() {
return stat.Err()
}
if stat.Ok().IsDir() {
entryPath := entry.Path()
if entryPath.IsErr() {
return entryPath.Err()
}
subdir := NewDir(entryPath.Ok())
if err := subdir.Walk(walker); err != nil {
return err
}
}
}
return nil
}
// ToString returns the String representation of the current directory's path.
func (d *Dir) ToString() String { return d.path }
// Print prints the content of the Dir to the standard output (console)
// and returns the Dir unchanged.
func (d *Dir) Print() *Dir { fmt.Println(d); return d }