-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanaita.go
317 lines (291 loc) · 8.55 KB
/
manaita.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/kevinburke/ssh_config"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/iancoleman/strcase"
"github.com/yuin/goldmark"
meta "github.com/yuin/goldmark-meta"
"github.com/yuin/goldmark/parser"
)
const (
DefaultScaffoldFileName = "SCAFFOLD.md"
DeprecatedDefaultScaffoldFileName = "CODEGEN.md" // deprecated
YamlMetaStartCode = "---"
DestFileNameStartCode = "# "
CodeTemplateStartCode = "```"
GoStyleRawContentDefaultBrand = "master" // NOTE: master always redirects to default branch only not when master exists but other is default branch.
GoStyleRawContentBaseURL = "https://raw.githubusercontent.com/%s/%s/%s/%s"
GitCloneHTTPBaseURL = "https://github.com/%s/%s.git"
GitCloneSSHBaseURL = "[email protected]:%s/%s.git"
)
var (
c = flag.String("c", DefaultScaffoldFileName, "specify markdown scaffold file path. default name is 'SCAFFOLD.md'")
p = flag.String("p", "", "specify parameters for scaffold template. these must be defined on markdown e.g. '-p foo=bar,fizz=buzz'")
// source patterns
githubGoGetStyleSourceRegexp, _ = regexp.Compile("^github\\.com/([\\w\\-.]+)/([\\w\\-.]+)/([\\w\\-./]+)(@[\\w\\-./]+)?$")
httpSourceRegexp, _ = regexp.Compile("^https?://.+")
funcMap = template.FuncMap{
"ToUpper": strings.ToUpper,
"ToLower": strings.ToLower,
"ToSnake": strcase.ToSnake,
"ToScreamingSnake": strcase.ToScreamingSnake,
"ToKebab": strcase.ToKebab,
"ToScreamingKebab": strcase.ToScreamingKebab,
"ToCamel": strcase.ToCamel,
"ToLowerCamel": strcase.ToLowerCamel,
}
errlog = log.New(os.Stderr, "ERROR: ", 0)
)
func main() {
flag.Parse()
envMap := envToMap()
currentDir, _ := os.Getwd()
givenParamMap := paramToMap(*p)
// detect source file path type
var reader io.Reader
if ms := githubGoGetStyleSourceRegexp.FindStringSubmatch(*c); len(ms) >= 4 {
// go get style github source
// trying to get from public raw content url
owner := ms[1]
repo := ms[2]
file := ms[3]
branch := GoStyleRawContentDefaultBrand
var specifiedBrand bool
if len(ms) == 5 && ms[4] != "" {
specifiedBrand = true
branch = strings.Replace(ms[4], "@", "", 1)
}
resp, err := http.Get(fmt.Sprintf(GoStyleRawContentBaseURL, owner, repo, branch, file))
defer resp.Body.Close()
reader = resp.Body
if err != nil || resp.StatusCode != http.StatusOK {
// fallback to git clone (SSH)
// Filesystem abstraction based on memory
fs := memfs.New()
// Git objects storer based on memory
storer := memory.NewStorage()
githubIdentityFile := ssh_config.Get("github.com", "IdentityFile")
publicKeys, err := ssh.NewPublicKeysFromFile("git", githubIdentityFile, "")
if err != nil {
errlog.Println("git clone ssh error: %s", err)
return
}
// Clones the repository into the worktree (fs) and stores all the .git
sshPath := fmt.Sprintf(GitCloneSSHBaseURL, owner, repo)
cloneOptions := &git.CloneOptions{
URL: sshPath,
Auth: publicKeys,
Tags: git.AllTags,
}
if specifiedBrand {
// branch
cloneOptions.ReferenceName = plumbing.NewBranchReferenceName(branch)
_, err = git.Clone(storer, fs, cloneOptions)
// FIXME: tag cannot be cloned correctly... maybe library problem..
//if err != nil {
// // tag
// cloneOptions.ReferenceName = plumbing.NewTagReferenceName(branch)
// _, err = git.Clone(storer, fs, cloneOptions)
//}
} else {
// default branch
_, err = git.Clone(storer, fs, cloneOptions)
}
if err != nil {
errlog.Printf("cannot git clone '%s': %s\n", sshPath, err)
return
}
// Prints the content of the CHANGELOG file from the cloned repository
scaffoldFile, err := fs.Open(file)
if err != nil {
errlog.Println(fmt.Errorf("cannot open file '%s'", file))
return
}
reader = scaffoldFile
}
} else if ms := httpSourceRegexp.FindAllString(*c, -1); len(ms) == 1 {
// http source
resp, err := http.Get(ms[0])
if err != nil {
errlog.Println(fmt.Errorf("cannot get '%s'", ms[0]))
return
}
if resp.StatusCode != http.StatusOK {
errlog.Println(fmt.Errorf("cannot get '%s': status code: %d", ms[0], resp.StatusCode))
return
}
defer resp.Body.Close()
reader = resp.Body
} else {
// trying to search local file
scaffoldFileName := fmt.Sprintf("%s/%s", currentDir, *c)
scaffoldFile, err := os.Open(scaffoldFileName)
if err != nil {
scaffoldFile, err = os.Open(DeprecatedDefaultScaffoldFileName)
}
if err != nil {
errlog.Println(fmt.Errorf("cannot find '%s'", DefaultScaffoldFileName))
return
}
defer scaffoldFile.Close()
reader = scaffoldFile
}
sc := bufio.NewScanner(reader)
var foundMeta bool
var endMeta bool
var metaStr string
var metaData map[string]interface{}
paramMap := make(map[string]string)
var code, dest, destFileName string
var searchCode bool
var foundCode bool
for sc.Scan() {
loc := sc.Text()
if !endMeta {
if strings.HasPrefix(loc, YamlMetaStartCode) {
if foundMeta {
endMeta = true
metaStr += loc + "\n"
// yaml-meta
markdown := goldmark.New(
goldmark.WithExtensions(
meta.Meta,
),
)
var buf bytes.Buffer
context := parser.NewContext()
if err := markdown.Convert([]byte(metaStr), &buf, parser.WithContext(context)); err != nil {
panic(err)
}
metaData = meta.Get(context)
// validate params
if metaParams, ok := metaData["Params"]; ok {
metaParamsSlice := metaParams.([]interface{})
LOOP:
for _, m := range metaParamsSlice {
for pk, pv := range givenParamMap {
if m == pk {
paramMap[pk] = pv
continue LOOP
}
}
panic(fmt.Sprintf("param '%s' is required", m))
}
LOOP2:
for pk, pv := range givenParamMap {
for _, m := range metaParamsSlice {
if m == pk {
paramMap[pk] = pv
continue LOOP2
}
}
fmt.Printf("param '%s' is not defined on markdown header. so it's not used.\n", pk)
}
}
continue
}
foundMeta = true
}
if foundMeta {
metaStr += loc + "\n"
continue
}
// only allowed metadata at first line
endMeta = true
}
if strings.Contains(loc, DestFileNameStartCode) {
// dest filename
destFileName = strings.Replace(loc, DestFileNameStartCode, "", -1)
destFileName = strings.Trim(destFileName, " ")
// compile filename
tmpl := template.Must(template.New("").Funcs(funcMap).Parse(destFileName))
var compiledDest bytes.Buffer
err := tmpl.Execute(&compiledDest, map[string]interface{}{
"Env": envMap,
"Params": paramMap,
})
if err != nil {
errlog.Println(fmt.Errorf("cannot compile template"))
return
}
destFileName = compiledDest.String()
dest = filepath.Join(currentDir, destFileName)
searchCode = true
}
if searchCode {
if strings.Contains(loc, CodeTemplateStartCode) {
if foundCode {
// end
// recursively make directories
os.MkdirAll(filepath.Dir(dest), os.ModePerm)
destFile, err := os.Create(dest)
if err != nil {
errlog.Println(fmt.Errorf("cannot Create '%s'", dest))
return
}
defer destFile.Close()
fmt.Println(destFileName)
tmpl := template.Must(template.New("").Funcs(funcMap).Parse(code))
err = tmpl.Execute(destFile, map[string]interface{}{
"Env": envMap,
"Params": paramMap,
})
if err != nil {
errlog.Println(fmt.Errorf("cannot write code to file '%s'", dest))
return
}
searchCode = false
foundCode = false
code = ""
dest = ""
destFileName = ""
continue
}
// start
foundCode = true
continue
}
if foundCode {
code += sc.Text() + "\n"
}
}
}
}
func envToMap() map[string]string {
envMap := make(map[string]string)
for _, v := range os.Environ() {
splitV := strings.Split(v, "=")
envMap[splitV[0]] = strings.Join(splitV[1:], "=")
}
return envMap
}
func paramToMap(p string) map[string]string {
paramMap := make(map[string]string)
if p == "" {
return paramMap
}
for _, v := range strings.Split(p, ",") {
splitV := strings.Split(v, "=")
if len(splitV) < 2 {
panic("-p params should be like 'FOO=BAR,AAA=BBB'.")
}
paramMap[splitV[0]] = splitV[1]
}
return paramMap
}