-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
63 lines (53 loc) · 1.4 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
package utils
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/jxsl13/module-migration/defaults"
)
func Exists(filePath string) (fs.FileInfo, bool, error) {
fi, err := os.Lstat(filePath)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil, false, nil
}
return nil, false, err
}
return fi, true, nil
}
func Copy(ctx context.Context, src, targetDir string) error {
src = filepath.Clean(src)
targetDir = filepath.Clean(targetDir)
fi, found, err := Exists(targetDir)
if err != nil {
return err
}
if !found {
return fmt.Errorf("additional file or directory target directory %s could not be found: %w", targetDir, err)
}
if !fi.IsDir() {
return fmt.Errorf("target directory %s is not a directory", targetDir)
}
fi, found, err = Exists(src)
if err != nil {
return err
}
if !found {
return fmt.Errorf("additional file or directory %s could not be found: %w", src, err)
}
if fi.IsDir() {
_, err = ExecuteQuietPathApplicationWithOutput(ctx, "", "cp", "-Rf", src, targetDir+defaults.FilePathSeparator)
if err != nil {
return fmt.Errorf("failed to copy directory %s into %s: %w", src, targetDir, err)
}
} else {
_, err = ExecuteQuietPathApplicationWithOutput(ctx, "", "cp", "-f", src, targetDir+defaults.FilePathSeparator)
if err != nil {
return fmt.Errorf("failed to copy file %s into %s: %w", src, targetDir, err)
}
}
return nil
}