forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_files.go
164 lines (135 loc) · 3.47 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package cf
import (
"cf/models"
"crypto/sha1"
"fileutils"
"fmt"
"glob"
"os"
"path"
"path/filepath"
"strings"
)
var DefaultIgnoreFiles = []string{
".cfignore",
".gitignore",
".git",
".svn",
"_darcs",
}
type Globs []glob.Glob
func AppFilesInDir(dir string) (appFiles []models.AppFileFields, err error) {
dir, err = filepath.Abs(dir)
if err != nil {
return
}
err = WalkAppFiles(dir, func(fileName string, fullPath string) (err error) {
fileInfo, err := os.Lstat(fullPath)
if err != nil {
return
}
size := fileInfo.Size()
h := sha1.New()
err = fileutils.CopyPathToWriter(fullPath, h)
if err != nil {
return
}
sha1Bytes := h.Sum(nil)
sha1 := fmt.Sprintf("%x", sha1Bytes)
appFiles = append(appFiles, models.AppFileFields{
Path: fileName,
Sha1: sha1,
Size: size,
})
return
})
return
}
func CopyFiles(appFiles []models.AppFileFields, 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
}
fileutils.SetExecutableBitsWithPaths(toPath, fromPath)
}
return
}
func CountFiles(directory string) uint64 {
var count uint64
WalkAppFiles(directory, func(_, _ string) error {
count++
return nil
})
return count
}
type walkAppFileFunc func(fileName, fullPath string) (err error)
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
}
if !f.Mode().IsRegular() {
return
}
fileRelativePath, _ := filepath.Rel(dir, fullPath)
fileRelativeUnixPath := filepath.ToSlash(fileRelativePath)
if fileShouldBeIgnored(exclusions, fileRelativeUnixPath) {
return
}
err = onEachFile(fileRelativePath, fullPath)
return
}
err = filepath.Walk(dir, walkFunc)
return
}
func fileShouldBeIgnored(exclusions Globs, relPath string) bool {
for _, exclusion := range exclusions {
if strings.HasPrefix(exclusion.Pattern, "/") && !strings.HasPrefix(relPath, "/") {
relPath = "/" + relPath
}
if exclusion.Match(relPath) {
return true
}
}
return false
}
func readCfIgnore(dir string) (exclusions Globs) {
cfIgnore, err := os.Open(filepath.Join(dir, ".cfignore"))
if err != nil {
return
}
ignores := strings.Split(fileutils.ReadFile(cfIgnore), "\n")
ignores = append(DefaultIgnoreFiles, ignores...)
for _, pattern := range ignores {
pattern = strings.TrimSpace(pattern)
if pattern == "" {
continue
}
pattern = path.Clean(pattern)
patternExclusions := exclusionsForPattern(pattern)
exclusions = append(exclusions, patternExclusions...)
}
return
}
func exclusionsForPattern(cfignorePattern string) (exclusions Globs) {
exclusions = append(exclusions, glob.MustCompileGlob(cfignorePattern))
exclusions = append(exclusions, glob.MustCompileGlob(path.Join(cfignorePattern, "*")))
exclusions = append(exclusions, glob.MustCompileGlob(path.Join(cfignorePattern, "**", "*")))
if !strings.HasPrefix(cfignorePattern, "/") {
exclusions = append(exclusions, glob.MustCompileGlob(path.Join("**", cfignorePattern)))
exclusions = append(exclusions, glob.MustCompileGlob(path.Join("**", cfignorePattern, "*")))
exclusions = append(exclusions, glob.MustCompileGlob(path.Join("**", cfignorePattern, "**", "*")))
}
return
}