-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_utils.go
61 lines (54 loc) · 1.42 KB
/
file_utils.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
package main
import (
"errors"
"os"
"os/user"
"path/filepath"
"strings"
cp "github.com/otiai10/copy"
)
// to expand tilde in directories
func expand(inputPath string) string {
usr, err := user.Current()
CheckIfError(err)
dir := usr.HomeDir
if inputPath == "~" {
// In case of "~", which won't be caught by the "else if"
return dir
} else if strings.HasPrefix(inputPath, "~/") {
// Use strings.HasPrefix so we don't match paths like
// "/something/~/something/"
return filepath.Join(dir, inputPath[2:])
}
return inputPath
}
func DoesFileExist(filePath string) (bool, error) {
if _, err := os.Stat(filePath); err == nil {
return true, nil
} else if errors.Is(err, os.ErrNotExist) {
return false, nil
} else {
return true, err
}
}
func isDirectory(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return false, err
}
return fileInfo.IsDir(), err
}
func CopyAllObjectsToRepo(configRepo ConfigRepository) {
repoPath := expand(configRepo.LocalPath)
for _, objectToSave := range configRepo.ObjectsToSave {
absolutePathObjectToSave := expand(objectToSave.Path)
isDir, err := isDirectory(absolutePathObjectToSave)
CheckIfError(err)
targetPath := filepath.Join(repoPath, objectToSave.RelativePathInRepo)
if !isDir {
targetPath = filepath.Join(targetPath, filepath.Base(absolutePathObjectToSave))
}
err = cp.Copy(absolutePathObjectToSave, targetPath)
CheckIfError(err)
}
}