-
Notifications
You must be signed in to change notification settings - Fork 927
/
app_files.go
150 lines (122 loc) · 2.81 KB
/
app_files.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
package cf
import (
"crypto/sha1"
"fileutils"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
func AppFilesInDir(dir string) (appFiles []AppFile, err error) {
err = walkAppFiles(dir, func(fileName string, fullPath string) {
fileInfo, err := os.Lstat(fullPath)
if err != nil {
return
}
size := fileInfo.Size()
h := sha1.New()
file, err := os.Open(fullPath)
if err != nil {
return
}
_, err = io.Copy(h, file)
if err != nil {
return
}
sha1Bytes := h.Sum(nil)
sha1 := fmt.Sprintf("%x", sha1Bytes)
appFiles = append(appFiles, AppFile{
Path: fileName,
Sha1: sha1,
Size: size,
})
})
return
}
func CopyFiles(appFiles []AppFile, fromDir, toDir string) (err error) {
if err != nil {
return
}
for _, file := range appFiles {
fromPath := filepath.Join(fromDir, file.Path)
toPath := filepath.Join(toDir, file.Path)
err = fileutils.CopyFilePaths(fromPath, toPath)
if err != nil {
return
}
}
return
}
type walkAppFileFunc func(fileName, fullPath string)
func walkAppFiles(dir string, onEachFile walkAppFileFunc) (err error) {
exclusions := readCfIgnore(dir)
walkFunc := func(fullPath string, f os.FileInfo, inErr error) (err error) {
err = inErr
if err != nil {
return
}
if f.IsDir() {
return
}
fileName, _ := filepath.Rel(dir, fullPath)
if fileShouldBeIgnored(exclusions, fileName) {
return
}
onEachFile(fileName, fullPath)
return
}
err = filepath.Walk(dir, walkFunc)
return
}
func fileShouldBeIgnored(exclusions []string, relativePath string) bool {
for _, exclusion := range exclusions {
if exclusion == relativePath {
return true
}
}
return false
}
func readCfIgnore(dir string) (exclusions []string) {
cfIgnore, err := os.Open(filepath.Join(dir, ".cfignore"))
if err != nil {
return
}
ignores := strings.Split(fileutils.ReadFile(cfIgnore), "\n")
ignores = append([]string{".cfignore"}, ignores...)
for _, pattern := range ignores {
pattern = strings.TrimSpace(pattern)
if pattern == "" {
continue
}
pattern = filepath.Clean(pattern)
patternExclusions := exclusionsForPattern(dir, pattern)
exclusions = append(exclusions, patternExclusions...)
}
return
}
func exclusionsForPattern(dir string, pattern string) (exclusions []string) {
starting_dir := dir
findPatternMatches := func(dir string, f os.FileInfo, inErr error) (err error) {
err = inErr
if err != nil {
return
}
absolutePaths := []string{}
if f.IsDir() && f.Name() == pattern {
absolutePaths, _ = filepath.Glob(filepath.Join(dir, "*"))
} else {
absolutePaths, _ = filepath.Glob(filepath.Join(dir, pattern))
}
for _, p := range absolutePaths {
relpath, _ := filepath.Rel(starting_dir, p)
exclusions = append(exclusions, relpath)
}
return
}
err := filepath.Walk(dir, findPatternMatches)
if err != nil {
return
}
return
}