-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
scaffold.go
515 lines (431 loc) · 12.8 KB
/
scaffold.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package machinery
import (
"bufio"
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/spf13/afero"
"golang.org/x/tools/imports"
"sigs.k8s.io/kubebuilder/v3/pkg/config"
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
)
const (
createOrUpdate = os.O_WRONLY | os.O_CREATE | os.O_TRUNC
defaultDirectoryPermission os.FileMode = 0700
defaultFilePermission os.FileMode = 0600
)
var options = imports.Options{
Comments: true,
TabIndent: true,
TabWidth: 8,
FormatOnly: true,
}
// Scaffold uses templates to scaffold new files
type Scaffold struct {
// fs allows to mock the file system for tests
fs afero.Fs
// permissions for new directories and files
dirPerm os.FileMode
filePerm os.FileMode
// injector is used to provide several fields to the templates
injector injector
}
// ScaffoldOption allows to provide optional arguments to the Scaffold
type ScaffoldOption func(*Scaffold)
// NewScaffold returns a new Scaffold with the provided plugins
func NewScaffold(fs Filesystem, options ...ScaffoldOption) *Scaffold {
s := &Scaffold{
fs: fs.FS,
dirPerm: defaultDirectoryPermission,
filePerm: defaultFilePermission,
}
for _, option := range options {
option(s)
}
return s
}
// WithDirectoryPermissions sets the permissions for new directories
func WithDirectoryPermissions(dirPerm os.FileMode) ScaffoldOption {
return func(s *Scaffold) {
s.dirPerm = dirPerm
}
}
// WithFilePermissions sets the permissions for new files
func WithFilePermissions(filePerm os.FileMode) ScaffoldOption {
return func(s *Scaffold) {
s.filePerm = filePerm
}
}
// WithConfig provides the project configuration to the Scaffold
func WithConfig(cfg config.Config) ScaffoldOption {
return func(s *Scaffold) {
s.injector.config = cfg
if cfg != nil && cfg.GetRepository() != "" {
imports.LocalPrefix = cfg.GetRepository()
}
}
}
// WithBoilerplate provides the boilerplate to the Scaffold
func WithBoilerplate(boilerplate string) ScaffoldOption {
return func(s *Scaffold) {
s.injector.boilerplate = boilerplate
}
}
// WithResource provides the resource to the Scaffold
func WithResource(resource *resource.Resource) ScaffoldOption {
return func(s *Scaffold) {
s.injector.resource = resource
}
}
// Execute writes to disk the provided files
func (s *Scaffold) Execute(builders ...Builder) error {
// Initialize the files
files := make(map[string]*File, len(builders))
for _, builder := range builders {
// Inject common fields
s.injector.injectInto(builder)
// Validate file builders
if reqValBuilder, requiresValidation := builder.(RequiresValidation); requiresValidation {
if err := reqValBuilder.Validate(); err != nil {
return ValidateError{err}
}
}
// Build models for Template builders
if t, isTemplate := builder.(Template); isTemplate {
if err := s.buildFileModel(t, files); err != nil {
return err
}
}
// Build models for Inserter builders
if i, isInserter := builder.(Inserter); isInserter {
if err := s.updateFileModel(i, files); err != nil {
return err
}
}
}
// Persist the files to disk
for _, f := range files {
if err := s.writeFile(f); err != nil {
return err
}
}
return nil
}
// buildFileModel scaffolds a single file
func (Scaffold) buildFileModel(t Template, models map[string]*File) error {
// Set the template default values
if err := t.SetTemplateDefaults(); err != nil {
return SetTemplateDefaultsError{err}
}
path := t.GetPath()
// Handle already existing models
if _, found := models[path]; found {
switch t.GetIfExistsAction() {
case SkipFile:
return nil
case Error:
return ModelAlreadyExistsError{path}
case OverwriteFile:
default:
return UnknownIfExistsActionError{path, t.GetIfExistsAction()}
}
}
b, err := doTemplate(t)
if err != nil {
return err
}
models[path] = &File{
Path: path,
Contents: string(b),
IfExistsAction: t.GetIfExistsAction(),
}
return nil
}
// doTemplate executes the template for a file using the input
func doTemplate(t Template) ([]byte, error) {
// Create a new template.Template using the type of the Template as the name
temp := template.New(fmt.Sprintf("%T", t))
leftDelim, rightDelim := t.GetDelim()
if leftDelim != "" && rightDelim != "" {
temp.Delims(leftDelim, rightDelim)
}
// Set the function map to be used
fm := DefaultFuncMap()
if templateWithFuncMap, hasCustomFuncMap := t.(UseCustomFuncMap); hasCustomFuncMap {
fm = templateWithFuncMap.GetFuncMap()
}
temp.Funcs(fm)
// Set the template body
if _, err := temp.Parse(t.GetBody()); err != nil {
return nil, err
}
// Execute the template
out := &bytes.Buffer{}
if err := temp.Execute(out, t); err != nil {
return nil, err
}
b := out.Bytes()
// TODO(adirio): move go-formatting to write step
// gofmt the imports
if filepath.Ext(t.GetPath()) == ".go" {
var err error
if b, err = imports.Process(t.GetPath(), b, &options); err != nil {
return nil, err
}
}
return b, nil
}
// updateFileModel updates a single file
func (s Scaffold) updateFileModel(i Inserter, models map[string]*File) error {
m, err := s.loadPreviousModel(i, models)
if err != nil {
return err
}
// Get valid code fragments
codeFragments := getValidCodeFragments(i)
// Remove code fragments that already were applied
err = filterExistingValues(m.Contents, codeFragments)
if err != nil {
return err
}
// If no code fragment to insert, we are done
if len(codeFragments) == 0 {
return nil
}
content, err := insertStrings(m.Contents, codeFragments)
if err != nil {
return err
}
// TODO(adirio): move go-formatting to write step
formattedContent := content
if ext := filepath.Ext(i.GetPath()); ext == ".go" {
formattedContent, err = imports.Process(i.GetPath(), content, nil)
if err != nil {
return err
}
}
m.Contents = string(formattedContent)
m.IfExistsAction = OverwriteFile
models[m.Path] = m
return nil
}
// loadPreviousModel gets the previous model from the models map or the actual file
func (s Scaffold) loadPreviousModel(i Inserter, models map[string]*File) (*File, error) {
path := i.GetPath()
// Lets see if we already have a model for this file
if m, found := models[path]; found {
// Check if there is already an scaffolded file
exists, err := afero.Exists(s.fs, path)
if err != nil {
return nil, ExistsFileError{err}
}
// If there is a model but no scaffolded file we return the model
if !exists {
return m, nil
}
// If both a model and a file are found, check which has preference
switch m.IfExistsAction {
case SkipFile:
// File has preference
fromFile, err := s.loadModelFromFile(path)
if err != nil {
return m, nil
}
return fromFile, nil
case Error:
// Writing will result in an error, so we can return error now
return nil, FileAlreadyExistsError{path}
case OverwriteFile:
// Model has preference
return m, nil
default:
return nil, UnknownIfExistsActionError{path, m.IfExistsAction}
}
}
// There was no model
return s.loadModelFromFile(path)
}
// loadModelFromFile gets the previous model from the actual file
func (s Scaffold) loadModelFromFile(path string) (f *File, err error) {
reader, err := s.fs.Open(path)
if err != nil {
return nil, OpenFileError{err}
}
defer func() {
if closeErr := reader.Close(); err == nil && closeErr != nil {
err = CloseFileError{closeErr}
}
}()
content, err := afero.ReadAll(reader)
if err != nil {
return nil, ReadFileError{err}
}
return &File{Path: path, Contents: string(content)}, nil
}
// getValidCodeFragments obtains the code fragments from a file.Inserter
func getValidCodeFragments(i Inserter) CodeFragmentsMap {
// Get the code fragments
codeFragments := i.GetCodeFragments()
// Validate the code fragments
validMarkers := i.GetMarkers()
for marker := range codeFragments {
valid := false
for _, validMarker := range validMarkers {
if marker == validMarker {
valid = true
break
}
}
if !valid {
delete(codeFragments, marker)
}
}
return codeFragments
}
// filterExistingValues removes code fragments that already exist in the content.
func filterExistingValues(content string, codeFragmentsMap CodeFragmentsMap) error {
for marker, codeFragments := range codeFragmentsMap {
codeFragmentsOut := codeFragments[:0]
for _, codeFragment := range codeFragments {
exists, err := codeFragmentExists(content, codeFragment)
if err != nil {
return err
}
if !exists {
codeFragmentsOut = append(codeFragmentsOut, codeFragment)
}
}
if len(codeFragmentsOut) == 0 {
delete(codeFragmentsMap, marker)
} else {
codeFragmentsMap[marker] = codeFragmentsOut
}
}
return nil
}
// codeFragmentExists checks if the codeFragment exists in the content.
func codeFragmentExists(content, codeFragment string) (exists bool, err error) {
// Trim space on each line in order to match different levels of indentation.
var sb strings.Builder
for _, line := range strings.Split(codeFragment, "\n") {
_, _ = sb.WriteString(strings.TrimSpace(line))
_ = sb.WriteByte('\n')
}
codeFragmentTrimmed := strings.TrimSpace(sb.String())
scanLines := 1 + strings.Count(codeFragmentTrimmed, "\n")
scanFunc := func(contentGroup string) bool {
if contentGroup == codeFragmentTrimmed {
exists = true
return false
}
return true
}
if err := scanMultiline(content, scanLines, scanFunc); err != nil {
return false, err
}
return exists, nil
}
// scanMultiline scans a string while buffering the specified number of scanLines. It calls scanFunc
// for every group of lines. The content passed to scanFunc will have trimmed whitespace. It
// continues scanning the content as long as scanFunc returns true.
func scanMultiline(content string, scanLines int, scanFunc func(contentGroup string) bool) error {
scanner := bufio.NewScanner(strings.NewReader(content))
// Optimized simple case.
if scanLines == 1 {
for scanner.Scan() {
if !scanFunc(strings.TrimSpace(scanner.Text())) {
return scanner.Err()
}
}
return scanner.Err()
}
// Complex case.
bufferedLines := make([]string, scanLines)
bufferedLinesIndex := 0
var sb strings.Builder
for scanner.Scan() {
// Trim space on each line in order to match different levels of indentation.
bufferedLines[bufferedLinesIndex] = strings.TrimSpace(scanner.Text())
bufferedLinesIndex = (bufferedLinesIndex + 1) % scanLines
sb.Reset()
for i := 0; i < scanLines; i++ {
_, _ = sb.WriteString(bufferedLines[(bufferedLinesIndex+i)%scanLines])
_ = sb.WriteByte('\n')
}
if !scanFunc(strings.TrimSpace(sb.String())) {
return scanner.Err()
}
}
return scanner.Err()
}
func insertStrings(content string, codeFragmentsMap CodeFragmentsMap) ([]byte, error) {
out := new(bytes.Buffer)
scanner := bufio.NewScanner(strings.NewReader(content))
for scanner.Scan() {
line := scanner.Text()
for marker, codeFragments := range codeFragmentsMap {
if marker.EqualsLine(line) {
for _, codeFragment := range codeFragments {
_, _ = out.WriteString(codeFragment) // bytes.Buffer.WriteString always returns nil errors
}
}
}
_, _ = out.WriteString(line + "\n") // bytes.Buffer.WriteString always returns nil errors
}
if err := scanner.Err(); err != nil {
return nil, err
}
return out.Bytes(), nil
}
func (s Scaffold) writeFile(f *File) (err error) {
// Check if the file to write already exists
exists, err := afero.Exists(s.fs, f.Path)
if err != nil {
return ExistsFileError{err}
}
if exists {
switch f.IfExistsAction {
case OverwriteFile:
// By not returning, the file is written as if it didn't exist
case SkipFile:
// By returning nil, the file is not written but the process will carry on
return nil
case Error:
// By returning an error, the file is not written and the process will fail
return FileAlreadyExistsError{f.Path}
}
}
// Create the directory if needed
if err := s.fs.MkdirAll(filepath.Dir(f.Path), s.dirPerm); err != nil {
return CreateDirectoryError{err}
}
// Create or truncate the file
writer, err := s.fs.OpenFile(f.Path, createOrUpdate, s.filePerm)
if err != nil {
return CreateFileError{err}
}
defer func() {
if closeErr := writer.Close(); err == nil && closeErr != nil {
err = CloseFileError{err}
}
}()
if _, err := writer.Write([]byte(f.Contents)); err != nil {
return WriteFileError{err}
}
return nil
}