forked from cloudfoundry/cli
/
app_files.go
111 lines (94 loc) · 2.21 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
package app_files
import (
"crypto/sha1"
"fmt"
"github.com/cloudfoundry/cli/cf/models"
cffileutils "github.com/cloudfoundry/cli/fileutils"
"github.com/cloudfoundry/gofileutils/fileutils"
"io/ioutil"
"os"
"path/filepath"
)
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
}
appFile := models.AppFileFields{
Path: filepath.ToSlash(fileName),
Size: fileInfo.Size(),
}
if fileInfo.IsDir() {
appFile.Sha1 = "0"
appFile.Size = 0
} else {
hash := sha1.New()
err = fileutils.CopyPathToWriter(fullPath, hash)
if err != nil {
return
}
appFile.Sha1 = fmt.Sprintf("%x", hash.Sum(nil))
}
appFiles = append(appFiles, appFile)
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.CopyPathToPath(fromPath, toPath)
if err != nil {
return
}
}
return
}
func CountFiles(directory string) uint64 {
var count uint64
WalkAppFiles(directory, func(_, _ string) error {
count++
return nil
})
return count
}
func WalkAppFiles(dir string, onEachFile func(string, string) error) (err error) {
cfIgnore := loadIgnoreFile(dir)
walkFunc := func(fullPath string, f os.FileInfo, inErr error) (err error) {
err = inErr
if err != nil {
return
}
if fullPath == dir {
return
}
if !cffileutils.IsRegular(f) && !f.IsDir() {
return
}
fileRelativePath, _ := filepath.Rel(dir, fullPath)
fileRelativeUnixPath := filepath.ToSlash(fileRelativePath)
if !cfIgnore.FileShouldBeIgnored(fileRelativeUnixPath) {
err = onEachFile(fileRelativePath, fullPath)
}
return
}
err = filepath.Walk(dir, walkFunc)
return
}
func loadIgnoreFile(dir string) CfIgnore {
fileContents, err := ioutil.ReadFile(filepath.Join(dir, ".cfignore"))
if err == nil {
return NewCfIgnore(string(fileContents))
} else {
return NewCfIgnore("")
}
}