Skip to content

Commit c6e1ee1

Browse files
authored
Merge pull request #1340 from Adirio/scaffold-enhancement/file
Move file related models to pkg/model/file
2 parents 6df16f3 + f3a1adc commit c6e1ee1

File tree

90 files changed

+599
-853
lines changed

Some content is hidden

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

90 files changed

+599
-853
lines changed

cmd/api.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import (
2020
"bufio"
2121
"errors"
2222
"fmt"
23+
"io/ioutil"
2324
"log"
2425
"os"
26+
"path/filepath"
2527
"strings"
2628

2729
"github.com/spf13/cobra"
@@ -202,6 +204,12 @@ func (o *apiOptions) validate(c *config.Config) error {
202204
}
203205

204206
func (o *apiOptions) scaffolder(c *config.Config) (scaffold.Scaffolder, error) {
207+
// Load the boilerplate
208+
bp, err := ioutil.ReadFile(filepath.Join("hack", "boilerplate.go.txt")) // nolint:gosec
209+
if err != nil {
210+
return nil, fmt.Errorf("unable to load boilerplate: %v", err)
211+
}
212+
205213
// Create the actual resource from the resource options
206214
var res *resource.Resource
207215
switch {
@@ -226,7 +234,7 @@ func (o *apiOptions) scaffolder(c *config.Config) (scaffold.Scaffolder, error) {
226234
return nil, fmt.Errorf("unknown pattern %q", o.pattern)
227235
}
228236

229-
return scaffold.NewAPIScaffolder(c, res, o.doResource, o.doController, plugins), nil
237+
return scaffold.NewAPIScaffolder(c, string(bp), res, o.doResource, o.doController, plugins), nil
230238
}
231239

232240
func (o *apiOptions) postScaffold(_ *config.Config) error {

cmd/webhook.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ package main
1919
import (
2020
"errors"
2121
"fmt"
22+
"io/ioutil"
2223
"log"
2324
"os"
25+
"path/filepath"
2426

2527
"github.com/spf13/cobra"
2628

@@ -110,7 +112,13 @@ func (o *webhookV1Options) validate(c *config.Config) error {
110112
return nil
111113
}
112114

113-
func (o *webhookV1Options) scaffolder(c *config.Config) (scaffold.Scaffolder, error) { // nolint:unparam
115+
func (o *webhookV1Options) scaffolder(c *config.Config) (scaffold.Scaffolder, error) {
116+
// Load the boilerplate
117+
bp, err := ioutil.ReadFile(filepath.Join("hack", "boilerplate.go.txt")) // nolint:gosec
118+
if err != nil {
119+
return nil, fmt.Errorf("unable to load boilerplate: %v", err)
120+
}
121+
114122
// Create the actual resource from the resource options
115123
var res *resource.Resource
116124
switch {
@@ -122,7 +130,7 @@ func (o *webhookV1Options) scaffolder(c *config.Config) (scaffold.Scaffolder, er
122130
return nil, fmt.Errorf("unknown project version %v", c.Version)
123131
}
124132

125-
return scaffold.NewV1WebhookScaffolder(&c.Config, res, o.server, o.webhookType, o.operations), nil
133+
return scaffold.NewV1WebhookScaffolder(&c.Config, string(bp), res, o.server, o.webhookType, o.operations), nil
126134
}
127135

128136
func (o *webhookV1Options) postScaffold(_ *config.Config) error {
@@ -213,6 +221,12 @@ func (o *webhookV2Options) validate(c *config.Config) error {
213221
}
214222

215223
func (o *webhookV2Options) scaffolder(c *config.Config) (scaffold.Scaffolder, error) { // nolint:unparam
224+
// Load the boilerplate
225+
bp, err := ioutil.ReadFile(filepath.Join("hack", "boilerplate.go.txt")) // nolint:gosec
226+
if err != nil {
227+
return nil, fmt.Errorf("unable to load boilerplate: %v", err)
228+
}
229+
216230
// Create the actual resource from the resource options
217231
var res *resource.Resource
218232
switch {
@@ -224,7 +238,7 @@ func (o *webhookV2Options) scaffolder(c *config.Config) (scaffold.Scaffolder, er
224238
return nil, fmt.Errorf("unknown project version %v", c.Version)
225239
}
226240

227-
return scaffold.NewV2WebhookScaffolder(&c.Config, res, o.defaulting, o.validation, o.conversion), nil
241+
return scaffold.NewV2WebhookScaffolder(&c.Config, string(bp), res, o.defaulting, o.validation, o.conversion), nil
228242
}
229243

230244
func (o *webhookV2Options) postScaffold(_ *config.Config) error {

pkg/model/file.go renamed to pkg/model/file/file.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,20 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package model
17+
package file
1818

19-
import (
20-
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
19+
// IfExistsAction determines what to do if the scaffold file already exists
20+
type IfExistsAction int
21+
22+
const (
23+
// Skip skips the file and moves to the next one
24+
Skip IfExistsAction = iota
25+
26+
// Error returns an error and stops processing
27+
Error
28+
29+
// Overwrite truncates and overwrites the existing file
30+
Overwrite
2131
)
2232

2333
// File describes a file that will be written
@@ -28,7 +38,6 @@ type File struct {
2838
// Contents is the generated output
2939
Contents string `json:"contents,omitempty"`
3040

31-
// TODO: Move input.IfExistsAction into model
3241
// IfExistsAction determines what to do if the file exists
33-
IfExistsAction input.IfExistsAction `json:"ifExistsAction,omitempty"`
42+
IfExistsAction IfExistsAction `json:"ifExistsAction,omitempty"`
3443
}

pkg/scaffold/input/input.go renamed to pkg/model/file/template.go

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package input
18-
19-
// IfExistsAction determines what to do if the scaffold file already exists
20-
type IfExistsAction int
21-
22-
const (
23-
// Skip skips the file and moves to the next one
24-
Skip IfExistsAction = iota
25-
26-
// Error returns an error and stops processing
27-
Error
28-
29-
// Overwrite truncates and overwrites the existing file
30-
Overwrite
31-
)
17+
package file
3218

3319
// Input is the input for scaffolding a file
3420
type Input struct {
@@ -44,21 +30,12 @@ type Input struct {
4430
// Boilerplate is the contents of a Boilerplate go header file
4531
Boilerplate string
4632

47-
// BoilerplatePath is the path to a Boilerplate go header file
48-
BoilerplatePath string
49-
50-
// Version is the project version
51-
Version string
52-
5333
// Domain is the domain for the APIs
5434
Domain string
5535

5636
// Repo is the go project package
5737
Repo string
5838

59-
// ProjectPath is the relative path to the project root
60-
ProjectPath string
61-
6239
// MultiGroup is the multi-group boolean from the PROJECT file
6340
MultiGroup bool
6441
}
@@ -102,32 +79,6 @@ func (i *Input) SetBoilerplate(b string) {
10279
}
10380
}
10481

105-
// BoilerplatePath allows boilerplate file path to be set on an object
106-
type BoilerplatePath interface {
107-
// SetBoilerplatePath sets the boilerplate file path
108-
SetBoilerplatePath(string)
109-
}
110-
111-
// SetBoilerplatePath sets the boilerplate file path
112-
func (i *Input) SetBoilerplatePath(bp string) {
113-
if i.BoilerplatePath == "" {
114-
i.BoilerplatePath = bp
115-
}
116-
}
117-
118-
// Version allows the project version to be set on an object
119-
type Version interface {
120-
// SetVersion sets the project version
121-
SetVersion(string)
122-
}
123-
124-
// SetVersion sets the project version
125-
func (i *Input) SetVersion(v string) {
126-
if i.Version == "" {
127-
i.Version = v
128-
}
129-
}
130-
13182
// MultiGroup allows the project version to be set on an object
13283
type MultiGroup interface {
13384
// SetVersion sets the project version
@@ -139,37 +90,15 @@ func (i *Input) SetMultiGroup(v bool) {
13990
i.MultiGroup = v
14091
}
14192

142-
// ProjecPath allows the project path to be set on an object
143-
type ProjecPath interface {
144-
// SetProjectPath sets the project file location
145-
SetProjectPath(string)
146-
}
147-
148-
// SetProjectPath sets the project path
149-
func (i *Input) SetProjectPath(p string) {
150-
if i.ProjectPath == "" {
151-
i.ProjectPath = p
152-
}
153-
}
154-
155-
// File is a scaffoldable file
156-
type File interface {
93+
// Template is a scaffoldable file template
94+
type Template interface {
15795
// GetInput returns the Input for creating a scaffold file
15896
GetInput() (Input, error)
15997
}
16098

16199
// RequiresValidation is a file that requires validation
162100
type RequiresValidation interface {
163-
File
101+
Template
164102
// Validate returns true if the template has valid values
165103
Validate() error
166104
}
167-
168-
// Options are the options for executing scaffold templates
169-
type Options struct {
170-
// BoilerplatePath is the path to the boilerplate file
171-
BoilerplatePath string
172-
173-
// Path is the path to the project
174-
ProjectPath string
175-
}

pkg/model/universe.go

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ limitations under the License.
1717
package model
1818

1919
import (
20-
"io/ioutil"
21-
2220
"sigs.k8s.io/kubebuilder/pkg/model/config"
21+
"sigs.k8s.io/kubebuilder/pkg/model/file"
2322
"sigs.k8s.io/kubebuilder/pkg/model/resource"
2423
)
2524

@@ -35,66 +34,65 @@ type Universe struct {
3534
Resource *resource.Resource `json:"resource,omitempty"`
3635

3736
// Files contains the model of the files that are being scaffolded
38-
Files []*File `json:"files,omitempty"`
37+
Files []*file.File `json:"files,omitempty"`
3938
}
4039

4140
// NewUniverse creates a new Universe
42-
func NewUniverse(options ...UniverseOption) (*Universe, error) {
41+
func NewUniverse(options ...UniverseOption) *Universe {
4342
universe := &Universe{}
4443

4544
// Apply options
4645
for _, option := range options {
47-
if err := option(universe); err != nil {
48-
return nil, err
49-
}
46+
option(universe)
5047
}
5148

52-
return universe, nil
49+
return universe
5350
}
5451

5552
// UniverseOption configure Universe
56-
type UniverseOption func(*Universe) error
53+
type UniverseOption func(*Universe)
5754

5855
// WithConfig stores the already loaded project configuration
5956
func WithConfig(projectConfig *config.Config) UniverseOption {
60-
return func(universe *Universe) error {
57+
return func(universe *Universe) {
6158
universe.Config = projectConfig
62-
return nil
63-
}
64-
}
65-
66-
// WithBoilerplateFrom loads the boilerplate from the provided path
67-
func WithBoilerplateFrom(path string) UniverseOption {
68-
return func(universe *Universe) error {
69-
boilerplate, err := ioutil.ReadFile(path)
70-
if err != nil {
71-
return err
72-
}
73-
74-
universe.Boilerplate = string(boilerplate)
75-
return nil
7659
}
7760
}
7861

7962
// WithBoilerplate stores the already loaded project configuration
8063
func WithBoilerplate(boilerplate string) UniverseOption {
81-
return func(universe *Universe) error {
64+
return func(universe *Universe) {
8265
universe.Boilerplate = boilerplate
83-
return nil
8466
}
8567
}
8668

8769
// WithoutBoilerplate is used for files that do not require a boilerplate
88-
func WithoutBoilerplate(universe *Universe) error {
89-
universe.Boilerplate = "-"
90-
return nil
70+
func WithoutBoilerplate(universe *Universe) {
71+
universe.Boilerplate = ""
9172
}
9273

9374
// WithResource stores the provided resource
9475
func WithResource(resource *resource.Resource) UniverseOption {
95-
return func(universe *Universe) error {
76+
return func(universe *Universe) {
9677
universe.Resource = resource
78+
}
79+
}
9780

98-
return nil
81+
func (u Universe) InjectInto(t file.Template) {
82+
// Inject project configuration
83+
if u.Config != nil {
84+
if templateWithDomain, ok := t.(file.Domain); ok {
85+
templateWithDomain.SetDomain(u.Config.Domain)
86+
}
87+
if templateWithRepository, ok := t.(file.Repo); ok {
88+
templateWithRepository.SetRepo(u.Config.Repo)
89+
}
90+
if templateWithMultiGroup, ok := t.(file.MultiGroup); ok {
91+
templateWithMultiGroup.SetMultiGroup(u.Config.MultiGroup)
92+
}
93+
}
94+
// Inject boilerplate
95+
if templateWithBoilerplate, ok := t.(file.Boilerplate); ok {
96+
templateWithBoilerplate.SetBoilerplate(u.Boilerplate)
9997
}
10098
}

0 commit comments

Comments
 (0)