-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.go
208 lines (168 loc) · 4.19 KB
/
filesystem.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// filesystem operations
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// todo: this really doesn't need to be here.
func copyFile(from string) error {
if err := cp(from, convertSrcToDestPath(from)); err != nil {
return err
}
consoleSuccess(fmt.Sprintf("\t%s\n", convertSrcToDestPath(from)))
return nil
}
// cp: copy a file from src to dst
func cp(src string, dst string) error {
s, err := os.Open(src)
defer s.Close()
if err != nil {
return err
}
d, err := os.Create(dst)
defer d.Close()
if err != nil {
return err
}
if _, err := io.Copy(d, s); err != nil {
return err
}
return err
}
// check if file exists
func fileExists(filename string) bool {
if _, err := os.Stat(filename); err == nil {
return true
}
return false
}
func (cfg *config) Sites() ([]string, error) {
var sites []string
files, err := ioutil.ReadDir(cfg.SiteDir)
for _, file := range files {
if file.IsDir() {
dot := filepath.Base(file.Name())[0]
if dot != '.' && dot != '_' {
println(filepath.Base(file.Name()))
sites = append(sites, filepath.Base(file.Name()))
}
}
}
return sites, err
}
// RecursiveGlob : returns all files and directories within a given directory, recursively
func RecursiveGlob(dirpath string) ([]string, error) {
var paths []string
err := filepath.Walk(dirpath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
filename := filepath.Base(path)
dot := filepath.Base(path)[0]
if dot != '.' && filename != ".DS_Store" {
paths = append(paths, path)
}
} else {
if err := makeDirIfMissing(path); err != nil {
return err
}
}
return nil
})
return paths, err
}
func makeDirIfMissing(dir string) error {
var err error
if _, err = os.Stat(dir); err != nil {
if os.IsNotExist(err) {
consoleSuccess("\t" + dir)
os.MkdirAll(dir, os.ModePerm)
}
return nil
}
return err
}
func filepathToSitename(filepath string) string {
return strings.Split(filepath, "/")[1]
}
// PartialGlob : returns a string array of directories within a path.
func PartialGlob(dirpath string, extMask string) ([]string, error) {
var files []string
if allFiles, err := RecursiveGlob(dirpath); err == nil {
for _, name := range allFiles {
dot := filepath.Base(name)[0]
ext := filepath.Ext(name)
if dot != '.' && ext == extMask {
files = append(files, name)
}
}
} else {
return nil, err
}
return files, nil
}
// FileTypeGlob : returns a string array of files within a path.
func FileTypeGlob(dirpath string, extMask string) ([]string, error) {
var files []string
if allFiles, err := RecursiveGlob(dirpath); err == nil {
for _, name := range allFiles {
dot := filepath.Base(name)[0]
ext := filepath.Ext(name)
if dot != '.' && dot != '_' && name != ".DS_Store" && ext == extMask {
files = append(files, name)
}
}
} else {
return nil, err
}
return files, nil
}
func convertSrcToDestPath(filename string) string {
return strings.Replace(filename, "sites", "public", 1)
}
func findLayoutFile(filename string) string {
return filepath.Dir(filename) + "/_layout"
}
func trimExt(filename string) string {
return strings.TrimSuffix(filename, filepath.Ext(filename))
}
func removeFileIfExists(filename string) {
os.RemoveAll("public/error.html")
}
func writeStringToFile(path string, content string) error {
var err error
if err = makeDirIfMissing(filepath.Dir(path)); err != nil {
return err
}
fo, err := os.Create(path)
defer fo.Close()
if err != nil {
return err
}
_, err = fmt.Fprintf(fo, content)
return err
}
func helperCopyFile(rel string, sitename string) string {
src := findFile(rel, sitename)
dest := filepath.Clean(filepath.Join("public", sitename, rel))
dir, _ := filepath.Split(dest)
makeDirIfMissing(dir)
if err := cp(src, dest); err != nil {
createError(src, err)
} else {
consoleSuccess(fmt.Sprintf("\t%s\n", dest))
}
return rel + "?checksum=" + checksum(src)
}
func findFile(from string, sitename string) string {
var sharedFile = filepath.Clean(filepath.Join("sites", "_shared", from))
if fileExists(sharedFile) {
return sharedFile
}
return filepath.Clean(filepath.Join("sites", sitename, from))
}