Skip to content

Commit e80f0a1

Browse files
committed
Template interface (phase 1)
Signed-off-by: Adrian Orive <[email protected]>
1 parent 435f946 commit e80f0a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+652
-457
lines changed

pkg/model/file/template.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,40 @@ import (
2020
"sigs.k8s.io/kubebuilder/pkg/model/resource"
2121
)
2222

23-
// Input is the input for scaffolding a file
24-
type Input struct {
23+
// Template is a scaffoldable file template
24+
type Template interface {
25+
// GetPath returns the path to the file location
26+
GetPath() string
27+
// GetBody returns the template body
28+
GetBody() string
29+
// GetIfExistsAction returns the behavior when creating a file that already exists
30+
GetIfExistsAction() IfExistsAction
31+
// SetTemplateDefaults returns the TemplateMixin for creating a scaffold file
32+
SetTemplateDefaults() error
33+
}
34+
35+
// TemplateMixin is the input for scaffolding a file
36+
type TemplateMixin struct {
2537
// Path is the file to write
2638
Path string
2739

40+
// TemplateBody is the template body to execute
41+
TemplateBody string
42+
2843
// IfExistsAction determines what to do if the file exists
2944
IfExistsAction IfExistsAction
45+
}
3046

31-
// TemplateBody is the template body to execute
32-
TemplateBody string
47+
func (t *TemplateMixin) GetPath() string {
48+
return t.Path
49+
}
50+
51+
func (t *TemplateMixin) GetBody() string {
52+
return t.TemplateBody
53+
}
54+
55+
func (t *TemplateMixin) GetIfExistsAction() IfExistsAction {
56+
return t.IfExistsAction
3357
}
3458

3559
// HasDomain allows the domain to be used on a template
@@ -124,12 +148,6 @@ func (m *ResourceMixin) InjectResource(res *resource.Resource) {
124148
}
125149
}
126150

127-
// Template is a scaffoldable file template
128-
type Template interface {
129-
// GetInput returns the Input for creating a scaffold file
130-
GetInput() (Input, error)
131-
}
132-
133151
// RequiresValidation is a file that requires validation
134152
type RequiresValidation interface {
135153
Template

pkg/scaffold/init.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ func (s *initScaffolder) scaffoldV1() error {
8989
if err := machinery.NewScaffold().Execute(
9090
s.newUniverse(""),
9191
&templatesv1.Boilerplate{
92-
Input: file.Input{Path: s.boilerplatePath},
93-
License: s.license,
94-
Owner: s.owner,
92+
TemplateMixin: file.TemplateMixin{Path: s.boilerplatePath},
93+
License: s.license,
94+
Owner: s.owner,
9595
},
9696
); err != nil {
9797
return err
@@ -129,9 +129,9 @@ func (s *initScaffolder) scaffoldV2() error {
129129
if err := machinery.NewScaffold().Execute(
130130
s.newUniverse(""),
131131
&templatesv2.Boilerplate{
132-
Input: file.Input{Path: s.boilerplatePath},
133-
License: s.license,
134-
Owner: s.owner,
132+
TemplateMixin: file.TemplateMixin{Path: s.boilerplatePath},
133+
License: s.license,
134+
Owner: s.owner,
135135
},
136136
); err != nil {
137137
return err

pkg/scaffold/internal/machinery/scaffold.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,17 @@ func buildFileModel(universe *model.Universe, t file.Template) (*file.File, erro
102102
}
103103

104104
// Get the template input params
105-
i, err := t.GetInput()
105+
err := t.SetTemplateDefaults()
106106
if err != nil {
107107
return nil, err
108108
}
109109

110110
m := &file.File{
111-
Path: i.Path,
112-
IfExistsAction: i.IfExistsAction,
111+
Path: t.GetPath(),
112+
IfExistsAction: t.GetIfExistsAction(),
113113
}
114114

115-
b, err := doTemplate(i, t)
115+
b, err := doTemplate(t)
116116
if err != nil {
117117
return nil, err
118118
}
@@ -151,8 +151,8 @@ func (s *scaffold) writeFile(f *file.File) error {
151151
}
152152

153153
// doTemplate executes the template for a file using the input
154-
func doTemplate(i file.Input, t file.Template) ([]byte, error) {
155-
temp, err := newTemplate(t).Parse(i.TemplateBody)
154+
func doTemplate(t file.Template) ([]byte, error) {
155+
temp, err := newTemplate(t).Parse(t.GetBody())
156156
if err != nil {
157157
return nil, err
158158
}
@@ -165,8 +165,8 @@ func doTemplate(i file.Input, t file.Template) ([]byte, error) {
165165
b := out.Bytes()
166166

167167
// gofmt the imports
168-
if filepath.Ext(i.Path) == ".go" {
169-
b, err = imports.Process(i.Path, b, &options)
168+
if filepath.Ext(t.GetPath()) == ".go" {
169+
b, err = imports.Process(t.GetPath(), b, &options)
170170
if err != nil {
171171
return nil, err
172172
}

pkg/scaffold/internal/machinery/scaffold_test.go

Lines changed: 50 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ var _ = Describe("Scaffold", func() {
124124
model.WithConfig(&config.Config{}),
125125
),
126126
fakeFile{
127-
input: file.Input{
128-
TemplateBody: fileContent,
129-
},
127+
body: fileContent,
130128
},
131129
)).To(Succeed())
132130
Expect(output.String()).To(Equal(fileContent))
@@ -143,9 +141,7 @@ var _ = Describe("Scaffold", func() {
143141
err := s.Execute(
144142
model.NewUniverse(),
145143
fakeFile{
146-
input: file.Input{
147-
TemplateBody: fileContent,
148-
},
144+
body: fileContent,
149145
},
150146
)
151147
Expect(err).To(HaveOccurred())
@@ -162,17 +158,15 @@ var _ = Describe("Scaffold", func() {
162158
err := s.Execute(
163159
model.NewUniverse(),
164160
fakeFile{
165-
input: file.Input{
166-
TemplateBody: fileContent,
167-
},
161+
body: fileContent,
168162
validateError: testError,
169163
},
170164
)
171165
Expect(err).To(HaveOccurred())
172166
Expect(err.Error()).To(ContainSubstring(testError.Error()))
173167
})
174168

175-
It("should fail if a template GetInput method fails", func() {
169+
It("should fail if a template SetTemplateDefaults method fails", func() {
176170
s = &scaffold{
177171
fs: filesystem.NewMock(
178172
filesystem.MockOutput(&output),
@@ -182,10 +176,8 @@ var _ = Describe("Scaffold", func() {
182176
err := s.Execute(
183177
model.NewUniverse(),
184178
fakeFile{
185-
input: file.Input{
186-
TemplateBody: fileContent,
187-
},
188-
err: testError,
179+
body: fileContent,
180+
err: testError,
189181
},
190182
)
191183
Expect(err).To(HaveOccurred())
@@ -202,9 +194,7 @@ var _ = Describe("Scaffold", func() {
202194
err := s.Execute(
203195
model.NewUniverse(),
204196
fakeFile{
205-
input: file.Input{
206-
TemplateBody: fileContent + "{{ .Field }",
207-
},
197+
body: fileContent + "{{ .Field }",
208198
},
209199
)
210200
Expect(err).To(HaveOccurred())
@@ -221,9 +211,7 @@ var _ = Describe("Scaffold", func() {
221211
err := s.Execute(
222212
model.NewUniverse(),
223213
fakeFile{
224-
input: file.Input{
225-
TemplateBody: fileContent + "{{ .Field }}",
226-
},
214+
body: fileContent + "{{ .Field }}",
227215
},
228216
)
229217
Expect(err).To(HaveOccurred())
@@ -240,10 +228,8 @@ var _ = Describe("Scaffold", func() {
240228
Expect(s.Execute(
241229
model.NewUniverse(),
242230
fakeFile{
243-
input: file.Input{
244-
Path: "file.go",
245-
TemplateBody: "package file",
246-
},
231+
path: "file.go",
232+
body: "package file",
247233
},
248234
)).To(Succeed())
249235
Expect(output.String()).To(Equal("package file\n"))
@@ -259,10 +245,8 @@ var _ = Describe("Scaffold", func() {
259245
err := s.Execute(
260246
model.NewUniverse(),
261247
fakeFile{
262-
input: file.Input{
263-
Path: "file.go",
264-
TemplateBody: fileContent,
265-
},
248+
path: "file.go",
249+
body: fileContent,
266250
},
267251
)
268252
Expect(err).To(HaveOccurred())
@@ -283,9 +267,7 @@ var _ = Describe("Scaffold", func() {
283267
Expect(s.Execute(
284268
model.NewUniverse(),
285269
fakeFile{
286-
input: file.Input{
287-
TemplateBody: fileContent,
288-
},
270+
body: fileContent,
289271
},
290272
)).To(Succeed())
291273
Expect(output.String()).To(BeEmpty())
@@ -295,10 +277,8 @@ var _ = Describe("Scaffold", func() {
295277
Expect(s.Execute(
296278
model.NewUniverse(),
297279
fakeFile{
298-
input: file.Input{
299-
IfExistsAction: file.Overwrite,
300-
TemplateBody: fileContent,
301-
},
280+
body: fileContent,
281+
ifExistsAction: file.Overwrite,
302282
},
303283
)).To(Succeed())
304284
Expect(output.String()).To(Equal(fileContent))
@@ -308,11 +288,9 @@ var _ = Describe("Scaffold", func() {
308288
err := s.Execute(
309289
model.NewUniverse(),
310290
fakeFile{
311-
input: file.Input{
312-
Path: "filename",
313-
IfExistsAction: file.Error,
314-
TemplateBody: fileContent,
315-
},
291+
path: "filename",
292+
body: fileContent,
293+
ifExistsAction: file.Error,
316294
},
317295
)
318296
Expect(err).To(HaveOccurred())
@@ -333,9 +311,7 @@ var _ = Describe("Scaffold", func() {
333311
err := s.Execute(
334312
model.NewUniverse(),
335313
fakeFile{
336-
input: file.Input{
337-
TemplateBody: fileContent,
338-
},
314+
body: fileContent,
339315
},
340316
)
341317
Expect(err).To(HaveOccurred())
@@ -352,9 +328,7 @@ var _ = Describe("Scaffold", func() {
352328
err := s.Execute(
353329
model.NewUniverse(),
354330
fakeFile{
355-
input: file.Input{
356-
TemplateBody: fileContent,
357-
},
331+
body: fileContent,
358332
},
359333
)
360334
Expect(err).To(HaveOccurred())
@@ -371,9 +345,7 @@ var _ = Describe("Scaffold", func() {
371345
err := s.Execute(
372346
model.NewUniverse(),
373347
fakeFile{
374-
input: file.Input{
375-
TemplateBody: fileContent,
376-
},
348+
body: fileContent,
377349
},
378350
)
379351
Expect(err).To(HaveOccurred())
@@ -390,9 +362,7 @@ var _ = Describe("Scaffold", func() {
390362
err := s.Execute(
391363
model.NewUniverse(),
392364
fakeFile{
393-
input: file.Input{
394-
TemplateBody: fileContent,
395-
},
365+
body: fileContent,
396366
},
397367
)
398368
Expect(err).To(HaveOccurred())
@@ -409,9 +379,7 @@ var _ = Describe("Scaffold", func() {
409379
err := s.Execute(
410380
model.NewUniverse(),
411381
fakeFile{
412-
input: file.Input{
413-
TemplateBody: fileContent,
414-
},
382+
body: fileContent,
415383
},
416384
)
417385
Expect(err).To(HaveOccurred())
@@ -421,6 +389,8 @@ var _ = Describe("Scaffold", func() {
421389
})
422390
})
423391

392+
var _ model.Plugin = fakePlugin{}
393+
424394
// fakePlugin is used to mock a model.Plugin in order to test Scaffold
425395
type fakePlugin struct {
426396
err error
@@ -431,20 +401,40 @@ func (f fakePlugin) Pipe(_ *model.Universe) error {
431401
return f.err
432402
}
433403

404+
var _ file.Template = fakeFile{}
405+
434406
// fakeFile is used to mock a file.File in order to test Scaffold
435407
type fakeFile struct {
436-
input file.Input
408+
path string
409+
body string
410+
ifExistsAction file.IfExistsAction
411+
437412
err error
438413
validateError error
439414
}
440415

441-
// GetInput implements file.Template
442-
func (f fakeFile) GetInput() (file.Input, error) {
416+
// GetPath implements file.Template
417+
func (f fakeFile) GetPath() string {
418+
return f.path
419+
}
420+
421+
// GetBody implements file.Template
422+
func (f fakeFile) GetBody() string {
423+
return f.body
424+
}
425+
426+
// GetIfExistsAction implements file.Template
427+
func (f fakeFile) GetIfExistsAction() file.IfExistsAction {
428+
return f.ifExistsAction
429+
}
430+
431+
// SetTemplateDefaults implements file.Template
432+
func (f fakeFile) SetTemplateDefaults() error {
443433
if f.err != nil {
444-
return file.Input{}, f.err
434+
return f.err
445435
}
446436

447-
return f.input, nil
437+
return nil
448438
}
449439

450440
// Validate implements file.RequiresValidation

0 commit comments

Comments
 (0)