Skip to content

Commit c5fd438

Browse files
authored
Merge pull request #1375 from Adirio/scaffold-enhancement/file-update
Update files as part of the Scaffold.Execute
2 parents 6987a7f + 6f191bf commit c5fd438

File tree

95 files changed

+2244
-1403
lines changed

Some content is hidden

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

95 files changed

+2244
-1403
lines changed

pkg/model/file/interfaces.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package file
18+
19+
import (
20+
"sigs.k8s.io/kubebuilder/pkg/model/resource"
21+
)
22+
23+
// Builder defines the basic methods that any file builder must implement
24+
type Builder interface {
25+
// GetPath returns the path to the file location
26+
GetPath() string
27+
// GetIfExistsAction returns the behavior when creating a file that already exists
28+
GetIfExistsAction() IfExistsAction
29+
}
30+
31+
// RequiresValidation is a file builder that requires validation
32+
type RequiresValidation interface {
33+
Builder
34+
// Validate returns true if the template has valid values
35+
Validate() error
36+
}
37+
38+
// Template is file builder based on a file template
39+
type Template interface {
40+
Builder
41+
// GetBody returns the template body
42+
GetBody() string
43+
// SetTemplateDefaults returns the TemplateMixin for creating a scaffold file
44+
SetTemplateDefaults() error
45+
}
46+
47+
// Inserter is a file builder that inserts code fragments in marked positions
48+
type Inserter interface {
49+
Builder
50+
// GetMarkers returns the different markers where code fragments will be inserted
51+
GetMarkers() []Marker
52+
// GetCodeFragments returns a map that binds markers to code fragments
53+
GetCodeFragments() CodeFragmentsMap
54+
}
55+
56+
// HasDomain allows the domain to be used on a template
57+
type HasDomain interface {
58+
// InjectDomain sets the template domain
59+
InjectDomain(string)
60+
}
61+
62+
// HasRepository allows the repository to be used on a template
63+
type HasRepository interface {
64+
// InjectRepository sets the template repository
65+
InjectRepository(string)
66+
}
67+
68+
// HasMultiGroup allows the multi-group flag to be used on a template
69+
type HasMultiGroup interface {
70+
// InjectMultiGroup sets the template multi-group flag
71+
InjectMultiGroup(bool)
72+
}
73+
74+
// HasBoilerplate allows a boilerplate to be used on a template
75+
type HasBoilerplate interface {
76+
// InjectBoilerplate sets the template boilerplate
77+
InjectBoilerplate(string)
78+
}
79+
80+
// HasResource allows a resource to be used on a template
81+
type HasResource interface {
82+
// InjectResource sets the template resource
83+
InjectResource(*resource.Resource)
84+
}

pkg/model/file/marker.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package file
18+
19+
import (
20+
"fmt"
21+
"path/filepath"
22+
)
23+
24+
const prefix = "+kubebuilder:scaffold:"
25+
26+
var commentsByExt = map[string]string{
27+
// TODO(v3): machine-readable comments should not have spaces by Go convention. However,
28+
// this is a backwards incompatible change, and thus should be done for next project version.
29+
".go": "// ",
30+
".yaml": "# ",
31+
}
32+
33+
// Marker represents a comment LoC that will be used to insert code fragments by update operations
34+
type Marker struct {
35+
comment string
36+
value string
37+
}
38+
39+
// NewMarkerFor creates a new marker customized for the specific file
40+
func NewMarkerFor(path string, value string) Marker {
41+
ext := filepath.Ext(path)
42+
if comment, found := commentsByExt[ext]; found {
43+
return Marker{comment, value}
44+
}
45+
46+
panic(fmt.Errorf("unknown file extension: '%s', expected '.go' or '.yaml'", ext))
47+
}
48+
49+
// String implements Stringer
50+
func (m Marker) String() string {
51+
return m.comment + prefix + m.value
52+
}
53+
54+
type CodeFragments []string
55+
56+
type CodeFragmentsMap map[Marker]CodeFragments

pkg/model/file/mixins.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package file
18+
19+
import (
20+
"sigs.k8s.io/kubebuilder/pkg/model/resource"
21+
)
22+
23+
// PathMixin provides file builders with a path field
24+
type PathMixin struct {
25+
// Path is the of the file
26+
Path string
27+
}
28+
29+
// GetPath implements Builder
30+
func (t *PathMixin) GetPath() string {
31+
return t.Path
32+
}
33+
34+
// IfExistsActionMixin provides file builders with a if-exists-action field
35+
type IfExistsActionMixin struct {
36+
// IfExistsAction determines what to do if the file exists
37+
IfExistsAction IfExistsAction
38+
}
39+
40+
// GetIfExistsAction implements Builder
41+
func (t *IfExistsActionMixin) GetIfExistsAction() IfExistsAction {
42+
return t.IfExistsAction
43+
}
44+
45+
// TemplateMixin is the mixin that should be embedded in Template builders
46+
type TemplateMixin struct {
47+
PathMixin
48+
IfExistsActionMixin
49+
50+
// TemplateBody is the template body to execute
51+
TemplateBody string
52+
}
53+
54+
func (t *TemplateMixin) GetBody() string {
55+
return t.TemplateBody
56+
}
57+
58+
// InserterMixin is the mixin that should be embedded in Inserter builders
59+
type InserterMixin struct {
60+
PathMixin
61+
}
62+
63+
// GetIfExistsAction implements Builder
64+
func (t *InserterMixin) GetIfExistsAction() IfExistsAction {
65+
// Inserter builders always need to overwrite previous files
66+
return Overwrite
67+
}
68+
69+
// DomainMixin provides templates with a injectable domain field
70+
type DomainMixin struct {
71+
// Domain is the domain for the APIs
72+
Domain string
73+
}
74+
75+
// InjectDomain implements HasDomain
76+
func (m *DomainMixin) InjectDomain(domain string) {
77+
if m.Domain == "" {
78+
m.Domain = domain
79+
}
80+
}
81+
82+
// RepositoryMixin provides templates with a injectable repository field
83+
type RepositoryMixin struct {
84+
// Repo is the go project package path
85+
Repo string
86+
}
87+
88+
// InjectRepository implements HasRepository
89+
func (m *RepositoryMixin) InjectRepository(repository string) {
90+
if m.Repo == "" {
91+
m.Repo = repository
92+
}
93+
}
94+
95+
// MultiGroupMixin provides templates with a injectable multi-group flag field
96+
type MultiGroupMixin struct {
97+
// MultiGroup is the multi-group flag
98+
MultiGroup bool
99+
}
100+
101+
// InjectMultiGroup implements HasMultiGroup
102+
func (m *MultiGroupMixin) InjectMultiGroup(flag bool) {
103+
m.MultiGroup = flag
104+
}
105+
106+
// BoilerplateMixin provides templates with a injectable boilerplate field
107+
type BoilerplateMixin struct {
108+
// Boilerplate is the contents of a Boilerplate go header file
109+
Boilerplate string
110+
}
111+
112+
// InjectBoilerplate implements HasBoilerplate
113+
func (m *BoilerplateMixin) InjectBoilerplate(boilerplate string) {
114+
if m.Boilerplate == "" {
115+
m.Boilerplate = boilerplate
116+
}
117+
}
118+
119+
// ResourceMixin provides templates with a injectable resource field
120+
type ResourceMixin struct {
121+
Resource *resource.Resource
122+
}
123+
124+
// InjectResource implements HasResource
125+
func (m *ResourceMixin) InjectResource(res *resource.Resource) {
126+
if m.Resource == nil {
127+
m.Resource = res
128+
}
129+
}

pkg/model/file/template.go

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)