Skip to content

Commit 1053df1

Browse files
estrozfabianvf
authored andcommitted
pkg/scaffold/customrender.go: interface CustomRenderer for non-templated scaffolds (operator-framework#850)
pkg/scaffold/scaffold.go: type-check input.File for CustomRenderer implementation
1 parent 35b283e commit 1053df1

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

pkg/scaffold/customrender.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2018 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package scaffold
16+
17+
// CustomRenderer is the interface for writing any scaffold file that does
18+
// not use a template.
19+
type CustomRenderer interface {
20+
CustomRender() ([]byte, error)
21+
}

pkg/scaffold/scaffold.go

+20-14
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,12 @@ func (s *Scaffold) doFile(e input.File) error {
117117
}
118118
}
119119

120-
return s.doTemplate(i, e, absFilePath)
120+
return s.doRender(i, e, absFilePath)
121121
}
122122

123123
const goFileExt = ".go"
124124

125-
// doTemplate executes the template at absPath for a file using the input
126-
func (s *Scaffold) doTemplate(i input.Input, e input.File, absPath string) error {
127-
temp, err := newTemplate(e).Parse(i.TemplateBody)
128-
if err != nil {
129-
return err
130-
}
131-
125+
func (s *Scaffold) doRender(i input.Input, e input.File, absPath string) error {
132126
var mode os.FileMode = fileutil.DefaultFileMode
133127
if i.IsExec {
134128
mode = fileutil.DefaultExecFileMode
@@ -145,18 +139,30 @@ func (s *Scaffold) doTemplate(i input.Input, e input.File, absPath string) error
145139
}()
146140
}
147141

148-
out := &bytes.Buffer{}
149-
err = temp.Execute(out, e)
150-
if err != nil {
151-
return err
142+
var b []byte
143+
if c, ok := e.(CustomRenderer); ok {
144+
// CustomRenderers have a non-template method of file rendering.
145+
if b, err = c.CustomRender(); err != nil {
146+
return err
147+
}
148+
} else {
149+
// All other files are rendered via their templates.
150+
temp, err := newTemplate(e).Parse(i.TemplateBody)
151+
if err != nil {
152+
return err
153+
}
154+
155+
out := &bytes.Buffer{}
156+
if err = temp.Execute(out, e); err != nil {
157+
return err
158+
}
159+
b = out.Bytes()
152160
}
153-
b := out.Bytes()
154161

155162
// gofmt the imports
156163
if filepath.Ext(absPath) == goFileExt {
157164
b, err = imports.Process(absPath, b, nil)
158165
if err != nil {
159-
fmt.Printf("%s\n", out.Bytes())
160166
return err
161167
}
162168
}

0 commit comments

Comments
 (0)