Skip to content

Commit a7b9f41

Browse files
author
Ted Young
committed
extract generic app file utils into fileutils package
1 parent b8eb4b2 commit a7b9f41

File tree

3 files changed

+133
-130
lines changed

3 files changed

+133
-130
lines changed

src/cf/app_files.go

Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package cf
22

33
import (
44
"crypto/sha1"
5+
"fileutils"
56
"fmt"
67
"io"
78
"os"
89
"path/filepath"
10+
"strings"
911
)
1012

1113
func AppFilesInDir(dir string) (appFiles []AppFile, err error) {
@@ -47,76 +49,102 @@ func CopyFiles(appFiles []AppFile, fromDir, toDir string) (err error) {
4749
for _, file := range appFiles {
4850
fromPath := filepath.Join(fromDir, file.Path)
4951
toPath := filepath.Join(toDir, file.Path)
50-
err = copyFile(fromPath, toPath)
52+
err = fileutils.CopyFilePaths(fromPath, toPath)
5153
if err != nil {
5254
return
5355
}
5456
}
5557
return
5658
}
5759

58-
func copyFile(fromPath, toPath string) (err error) {
59-
err = os.MkdirAll(filepath.Dir(toPath), os.ModeDir|os.ModeTemporary|os.ModePerm)
60-
if err != nil {
61-
return
62-
}
60+
type walkAppFileFunc func(fileName, fullPath string)
6361

64-
src, err := os.Open(fromPath)
65-
if err != nil {
66-
return
67-
}
68-
defer src.Close()
62+
func walkAppFiles(dir string, onEachFile walkAppFileFunc) (err error) {
63+
exclusions := readCfIgnore(dir)
64+
65+
walkFunc := func(fullPath string, f os.FileInfo, inErr error) (err error) {
66+
err = inErr
67+
if err != nil {
68+
return
69+
}
70+
71+
if f.IsDir() {
72+
return
73+
}
74+
75+
fileName, _ := filepath.Rel(dir, fullPath)
76+
if fileShouldBeIgnored(exclusions, fileName) {
77+
return
78+
}
79+
80+
onEachFile(fileName, fullPath)
6981

70-
dst, err := os.Create(toPath)
71-
if err != nil {
7282
return
7383
}
74-
defer dst.Close()
7584

76-
_, err = io.Copy(dst, src)
85+
err = filepath.Walk(dir, walkFunc)
7786
return
7887
}
7988

80-
func IsDirEmpty(dir string) (isEmpty bool, err error) {
81-
dirFile, err := os.Open(dir)
89+
func fileShouldBeIgnored(exclusions []string, relativePath string) bool {
90+
for _, exclusion := range exclusions {
91+
if exclusion == relativePath {
92+
return true
93+
}
94+
}
95+
return false
96+
}
97+
98+
func readCfIgnore(dir string) (exclusions []string) {
99+
cfIgnore, err := os.Open(filepath.Join(dir, ".cfignore"))
82100
if err != nil {
83101
return
84102
}
85103

86-
_, readErr := dirFile.Readdirnames(1)
87-
if readErr != nil {
88-
isEmpty = true
89-
} else {
90-
isEmpty = false
104+
ignores := strings.Split(fileutils.ReadFile(cfIgnore), "\n")
105+
ignores = append([]string{".cfignore"}, ignores...)
106+
107+
for _, pattern := range ignores {
108+
pattern = strings.TrimSpace(pattern)
109+
if pattern == "" {
110+
continue
111+
}
112+
pattern = filepath.Clean(pattern)
113+
patternExclusions := exclusionsForPattern(dir, pattern)
114+
exclusions = append(exclusions, patternExclusions...)
91115
}
116+
92117
return
93118
}
94119

95-
type walkAppFileFunc func(fileName, fullPath string)
96-
97-
func walkAppFiles(dir string, onEachFile walkAppFileFunc) (err error) {
98-
exclusions := readCfIgnore(dir)
120+
func exclusionsForPattern(dir string, pattern string) (exclusions []string) {
121+
starting_dir := dir
99122

100-
walkFunc := func(fullPath string, f os.FileInfo, inErr error) (err error) {
123+
findPatternMatches := func(dir string, f os.FileInfo, inErr error) (err error) {
101124
err = inErr
102125
if err != nil {
103126
return
104127
}
105128

106-
if f.IsDir() {
107-
return
129+
absolutePaths := []string{}
130+
if f.IsDir() && f.Name() == pattern {
131+
absolutePaths, _ = filepath.Glob(filepath.Join(dir, "*"))
132+
} else {
133+
absolutePaths, _ = filepath.Glob(filepath.Join(dir, pattern))
108134
}
109135

110-
fileName, _ := filepath.Rel(dir, fullPath)
111-
if fileShouldBeIgnored(exclusions, fileName) {
112-
return
113-
}
136+
for _, p := range absolutePaths {
137+
relpath, _ := filepath.Rel(starting_dir, p)
114138

115-
onEachFile(fileName, fullPath)
139+
exclusions = append(exclusions, relpath)
140+
}
141+
return
142+
}
116143

144+
err := filepath.Walk(dir, findPatternMatches)
145+
if err != nil {
117146
return
118147
}
119148

120-
err = filepath.Walk(dir, walkFunc)
121149
return
122150
}

src/cf/zipper.go

Lines changed: 5 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package cf
22

33
import (
44
"archive/zip"
5-
"bytes"
65
"errors"
6+
"fileutils"
77
"io"
88
"os"
99
"path/filepath"
10-
"strings"
1110
)
1211

1312
type Zipper interface {
@@ -20,9 +19,9 @@ var doNotZipExtensions = []string{".zip", ".war", ".jar"}
2019

2120
func (zipper ApplicationZipper) Zip(dirOrZipFile string, targetFile *os.File) (err error) {
2221
if shouldNotZip(filepath.Ext(dirOrZipFile)) {
23-
err = copyZipFile(dirOrZipFile, targetFile)
22+
err = fileutils.CopyPathToFile(dirOrZipFile, targetFile)
2423
} else {
25-
err = createZipFile(dirOrZipFile, targetFile)
24+
err = writeZipFile(dirOrZipFile, targetFile)
2625
}
2726
return
2827
}
@@ -36,23 +35,8 @@ func shouldNotZip(extension string) (result bool) {
3635
return
3736
}
3837

39-
func copyZipFile(originalFilePath string, targetFile *os.File) (err error) {
40-
originalFile, err := os.Open(originalFilePath)
41-
if err != nil {
42-
return
43-
}
44-
defer originalFile.Close()
45-
46-
_, err = io.Copy(targetFile, originalFile)
47-
if err != nil {
48-
return
49-
}
50-
_, err = targetFile.Seek(0, os.SEEK_SET)
51-
return
52-
}
53-
54-
func createZipFile(dir string, targetFile *os.File) (err error) {
55-
isEmpty, err := IsDirEmpty(dir)
38+
func writeZipFile(dir string, targetFile *os.File) (err error) {
39+
isEmpty, err := fileutils.IsDirEmpty(dir)
5640
if err != nil {
5741
return
5842
}
@@ -88,77 +72,3 @@ func createZipFile(dir string, targetFile *os.File) (err error) {
8872

8973
return
9074
}
91-
92-
func fileShouldBeIgnored(exclusions []string, relativePath string) bool {
93-
for _, exclusion := range exclusions {
94-
if exclusion == relativePath {
95-
return true
96-
}
97-
}
98-
return false
99-
}
100-
101-
func readCfIgnore(dir string) (exclusions []string) {
102-
cfIgnore, err := os.Open(filepath.Join(dir, ".cfignore"))
103-
if err != nil {
104-
return
105-
}
106-
107-
ignores := strings.Split(readFile(cfIgnore), "\n")
108-
ignores = append([]string{".cfignore"}, ignores...)
109-
110-
for _, pattern := range ignores {
111-
pattern = strings.TrimSpace(pattern)
112-
if pattern == "" {
113-
continue
114-
}
115-
pattern = filepath.Clean(pattern)
116-
patternExclusions := exclusionsForPattern(dir, pattern)
117-
exclusions = append(exclusions, patternExclusions...)
118-
}
119-
120-
return
121-
}
122-
123-
func exclusionsForPattern(dir string, pattern string) (exclusions []string) {
124-
starting_dir := dir
125-
126-
findPatternMatches := func(dir string, f os.FileInfo, inErr error) (err error) {
127-
err = inErr
128-
if err != nil {
129-
return
130-
}
131-
132-
absolutePaths := []string{}
133-
if f.IsDir() && f.Name() == pattern {
134-
absolutePaths, _ = filepath.Glob(filepath.Join(dir, "*"))
135-
} else {
136-
absolutePaths, _ = filepath.Glob(filepath.Join(dir, pattern))
137-
}
138-
139-
for _, p := range absolutePaths {
140-
relpath, _ := filepath.Rel(starting_dir, p)
141-
142-
exclusions = append(exclusions, relpath)
143-
}
144-
return
145-
}
146-
147-
err := filepath.Walk(dir, findPatternMatches)
148-
if err != nil {
149-
return
150-
}
151-
152-
return
153-
}
154-
155-
func readFile(file *os.File) string {
156-
buf := &bytes.Buffer{}
157-
_, err := io.Copy(buf, file)
158-
159-
if err != nil {
160-
return ""
161-
}
162-
163-
return string(buf.Bytes())
164-
}

src/fileutils/file_utils.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package fileutils
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"os"
7+
"path/filepath"
8+
)
9+
10+
11+
func CopyFilePaths(fromPath, toPath string) (err error) {
12+
err = os.MkdirAll(filepath.Dir(toPath), os.ModeDir | os.ModeTemporary | os.ModePerm)
13+
if err != nil {
14+
return
15+
}
16+
17+
dst, err := os.Create(toPath)
18+
if err != nil {
19+
return
20+
}
21+
defer dst.Close()
22+
23+
return CopyPathToFile(fromPath,dst)
24+
}
25+
26+
func IsDirEmpty(dir string) (isEmpty bool, err error) {
27+
dirFile, err := os.Open(dir)
28+
if err != nil {
29+
return
30+
}
31+
32+
_, readErr := dirFile.Readdirnames(1)
33+
if readErr != nil {
34+
isEmpty = true
35+
} else {
36+
isEmpty = false
37+
}
38+
return
39+
}
40+
41+
func CopyPathToFile(originalFilePath string, targetFile *os.File) (err error) {
42+
originalFile, err := os.Open(originalFilePath)
43+
if err != nil {
44+
return
45+
}
46+
defer originalFile.Close()
47+
48+
_, err = io.Copy(targetFile, originalFile)
49+
if err != nil {
50+
return
51+
}
52+
_, err = targetFile.Seek(0, os.SEEK_SET)
53+
return
54+
}
55+
56+
func ReadFile(file *os.File) string {
57+
buf := &bytes.Buffer{}
58+
_, err := io.Copy(buf, file)
59+
60+
if err != nil {
61+
return ""
62+
}
63+
64+
return string(buf.Bytes())
65+
}

0 commit comments

Comments
 (0)