-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
56 lines (51 loc) · 1.26 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
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
)
type FileManager struct {
DestDir string
}
func (fm *FileManager) ValidateDestDir() error {
dstInfo, err := os.Stat(fm.DestDir)
if err != nil {
if os.IsNotExist(err) {
// if destination does not exist, skip validation since the given
// destination will be created in `fm.MoveFiles`. if created here,
// `os.Rename` operation will fail due to an existing newpath
return nil
}
return err
}
if !dstInfo.IsDir() {
return fmt.Errorf("%s is not a directory", fm.DestDir)
}
return nil
}
func (fm *FileManager) CreateTempDir() (string, error) {
tmpdir, err := os.MkdirTemp("", "gitdl-*")
if err != nil {
return "", err
}
return tmpdir, nil
}
func (fm *FileManager) MoveFiles(sources []string, tempDir string) error {
for _, s := range sources {
_, ford := filepath.Split(s)
src := filepath.Join(tempDir, s)
dst := filepath.Join(fm.DestDir, ford)
if err := os.Rename(src, dst); err != nil {
if errors.Is(err, os.ErrExist) {
// if options.Force:
// — delete existing directory dst
// - return nil to continue with overwrite
// else:
return fmt.Errorf("directory with content at `%s' would be overwritten, skipping…", dst)
}
return err
}
}
return nil
}