-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
80 lines (72 loc) · 2.27 KB
/
fs.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
package fs
import (
"context"
"fmt"
)
// Filef is a shortcut for File(fmt.Sprintf(format, args...))
func Filef(format string, args ...any) File {
return File(fmt.Sprintf(format, args...))
}
// CleanFilePath returns a File from uri with cleaned path and a file system prefix
func CleanFilePath(uri string) File {
return GetFileSystem(uri).JoinCleanFile(uri)
}
// JoinCleanFilePath returns a File from joined and cleaned uriParts with a file system prefix
func JoinCleanFilePath(uriParts ...string) File {
return GetFileSystem(uriParts...).JoinCleanFile(uriParts...)
}
// Move moves and/or renames source to destination.
// source and destination can be files or directories.
// If source is a directory, it will be moved with all its contents.
// If source and destination are using the same FileSystem,
// then FileSystem.Move will be used, else source will be
// copied recursively first to destination and then deleted.
func Move(ctx context.Context, source, destination File) error {
if source == "" || destination == "" {
return ErrEmptyPath
}
srcFS, srcPath := source.ParseRawURI()
destFS, destPath := destination.ParseRawURI()
if srcFS == destFS {
if moveFS, ok := srcFS.(MoveFileSystem); ok {
return moveFS.Move(srcPath, destPath)
}
}
err := CopyRecursive(ctx, source, destination)
if err != nil {
return err
}
return source.RemoveRecursive()
}
// Remove removes all files with fileURIs.
// If a file does not exist, then it is skipped and not reported as error.
func Remove(fileURIs ...string) error {
for _, uri := range fileURIs {
err := File(uri).Remove()
if RemoveErrDoesNotExist(err) != nil {
return err
}
}
return nil
}
// RemoveFile removes a single file.
// It's just a wrapper for calling file.Remove(),
// useful mostly as callback for methods that list files
// to delete all files of a certain pattern.
// Or as a more elegant way to remove a file passed as string literal path:
//
// fs.RemoveFile("/my/hardcoded.path")
func RemoveFile(file File) error {
return file.Remove()
}
// RemoveFiles removes all files.
// If a file does not exist, then it is skipped and not reported as error.
func RemoveFiles(files ...File) error {
for _, file := range files {
err := file.Remove()
if RemoveErrDoesNotExist(err) != nil {
return err
}
}
return nil
}