-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.go
More file actions
202 lines (168 loc) · 5.15 KB
/
start.go
File metadata and controls
202 lines (168 loc) · 5.15 KB
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
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type ReplaceFunc func(srcSubPath string, srcContents string) (subPath string, contents string, err error)
type FileSource interface {
SubPath() string
IsDir() bool
Reader() (io.ReadCloser, error)
}
type FileSourceFunc func(fileSource FileSource) error
type SourceAccess interface {
EachSource(callback FileSourceFunc) error
}
type StartParams struct {
Keywords string
KeySeparator string
Arguments []string
IncludeSuffixes string
ExcludeSuffixes string
}
func StartMain(sp StartParams) error {
srcPath := sp.Arguments[0]
destPath := sp.Arguments[1]
keyMap := stringsToMap(sp.Keywords, sp.KeySeparator)
includeSuffixes := toList(sp.IncludeSuffixes, sp.KeySeparator)
excludeSuffixes := toList(sp.ExcludeSuffixes, sp.KeySeparator)
_, err := os.Stat(destPath)
if os.IsNotExist(err) {
sa := newSourceAccess(srcPath)
return copyEachFileSource(sa, destPath, includeSuffixes, excludeSuffixes, newReplaceFunc(keyMap))
} else if err != nil {
return err
} else {
fmt.Fprintln(os.Stderr, "Error: dest path:", destPath, " already exists")
return os.ErrExist
}
}
func newSourceAccess(srcPath string) SourceAccess {
if strings.Index(srcPath, "http://") == 0 || strings.Index(srcPath, "https://") == 0 {
return newGithubAccess(srcPath)
} else {
return newFileAccess(srcPath)
}
}
func copyEachFileSource(sa SourceAccess, destPath string, includeSuffixes []string, excludeSuffixes []string, handler ReplaceFunc) error {
return sa.EachSource(func(fileSource FileSource) error {
var isDestDir bool
var contentBytes []byte
var subPath, contents string
if fileSource.IsDir() {
subPath, _, err := handler(fileSource.SubPath(), "")
if err != nil {
return err
}
return os.MkdirAll(normalizePath(destPath, true) + subPath, 0777)
}
reader, err := fileSource.Reader()
if err != nil {
return err
}
defer reader.Close()
contentBytes, err = ioutil.ReadAll(reader)
if err != nil {
return err
}
if isMatchSuffixes(includeSuffixes, fileSource.SubPath()) &&
!isMatchSuffixes(excludeSuffixes, fileSource.SubPath()) {
subPath, contents, err = handler(fileSource.SubPath(), string(contentBytes))
if err != nil {
return err
}
contentBytes = nil
} else {
subPath = fileSource.SubPath()
}
// This is expected to be created before calling here.
// Or, ignore error for a dest file is used.
isDestDir, err = isDirectory(destPath)
newFilePath := normalizePath(destPath, isDestDir) + subPath
out, outErr := os.Create(newFilePath)
if outErr != nil {
return outErr
}
if contentBytes != nil {
_, err = out.Write(contentBytes)
} else {
_, err = out.WriteString(contents)
}
if err == nil {
fmt.Println("Create", newFilePath)
}
return err
})
}
func isMatchSuffixes(suffixes []string, name string) bool {
for _, suffix := range suffixes {
if suffix == "*" || strings.HasSuffix(name, suffix) {
return true
}
}
return false
}
func newReplaceFunc(keywords map[string]string) ReplaceFunc {
return func (srcSubPath string, srcContents string) (subPath string, contents string, err error) {
subPath = srcSubPath
contents = srcContents
for key, val := range keywords {
subPath = strings.Replace(subPath, key, val, -1)
contents = strings.Replace(contents, key, val, -1)
}
err = nil
return
}
}
func toSubPath(basePath string, fullPath string) string {
return fullPath[len(basePath):]
}
func normalizePath(path string, isDir bool) string {
path = filepath.Clean(path)
if isDir && !strings.HasSuffix(path, pathSep()) {
path = path + pathSep()
}
return path
}
func pathSep() string {
return string([]byte{os.PathSeparator})
}
func isDirectory(path string) (isDir bool, err error) {
fInfo, err := os.Stat(path)
if err != nil {
return false, err
}
return fInfo.IsDir(), nil
}
func stringsToMap(keywords string, sep string) (keyMap map[string]string) {
var key, value string
keyMap = map[string]string{}
if len(keywords) == 0 {
return
}
pairs := strings.Split(keywords, sep)
for _, kv := range pairs {
index := strings.IndexByte(kv, '=')
if index >= 0 {
key = kv[0:index]
value = kv[index + 1:]
} else {
key = kv
value = ""
}
keyMap[key] = value
}
return
}
func toList(listString string, sep string) []string {
list := strings.Split(listString, sep)
result := make([]string, len(list), len(list))
for i, v := range list {
result[i] = strings.TrimSpace(v)
}
return result
}