Skip to content

Commit f0c25d0

Browse files
authored
Merge pull request #1252 from Adirio/scaffold-enhancement/path-utils
Named arguments for path formatting
2 parents 105f315 + ec5c4ad commit f0c25d0

34 files changed

+102
-146
lines changed

pkg/model/resource/options.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,11 @@ func (opts *Options) safeImport(unsafe string) string {
168168
func (opts *Options) NewV1Resource(c *config.Config, doResource bool) *Resource {
169169
res := opts.newResource()
170170

171+
replacer := res.Replacer()
172+
171173
// NOTE: while directories can have "-" and ".", v1 needs that directory to be a Go package
172174
// and that is the reason why we remove them for the directory
173-
pkg := path.Join(c.Repo, "pkg", "apis", res.GroupPackageName, opts.Version)
175+
pkg := replacer.Replace(path.Join(c.Repo, "pkg", "apis", "%[group-package-name]", "%[version]"))
174176
domain := c.Domain
175177

176178
// pkg and domain may need to be changed in case we are referring to a builtin core resource:
@@ -180,11 +182,11 @@ func (opts *Options) NewV1Resource(c *config.Config, doResource bool) *Resource
180182
// - In any other case, default to => project resource
181183
// TODO: need to support '--resource-pkg-path' flag for specifying resourcePath
182184
if !doResource {
183-
file := filepath.Join("pkg", "apis", res.GroupPackageName, opts.Version,
184-
fmt.Sprintf("%s_types.go", strings.ToLower(opts.Kind)))
185+
file := replacer.Replace(filepath.Join(
186+
"pkg", "apis", "%[group-package-name]", "%[version]", "%[kind]_types.go"))
185187
if _, err := os.Stat(file); os.IsNotExist(err) {
186188
if coreDomain, found := coreGroups[opts.Group]; found {
187-
pkg = path.Join("k8s.io", "api", opts.Group, opts.Version)
189+
pkg = replacer.Replace(path.Join("k8s.io", "api", "%[group]", "%[version]"))
188190
domain = coreDomain
189191
}
190192
}
@@ -205,9 +207,11 @@ func (opts *Options) NewV1Resource(c *config.Config, doResource bool) *Resource
205207
func (opts *Options) NewResource(c *config.Config, doResource bool) *Resource {
206208
res := opts.newResource()
207209

208-
pkg := path.Join(c.Repo, "api", opts.Version)
210+
replacer := res.Replacer()
211+
212+
pkg := replacer.Replace(path.Join(c.Repo, "api", "%[version]"))
209213
if c.MultiGroup {
210-
pkg = path.Join(c.Repo, "apis", res.Group, opts.Version)
214+
pkg = replacer.Replace(path.Join(c.Repo, "apis", "%[group]", "%[version]"))
211215
}
212216
domain := c.Domain
213217

@@ -220,7 +224,7 @@ func (opts *Options) NewResource(c *config.Config, doResource bool) *Resource {
220224
if !doResource {
221225
if !c.HasResource(opts.GVK()) {
222226
if coreDomain, found := coreGroups[opts.Group]; found {
223-
pkg = path.Join("k8s.io", "api", opts.Group, opts.Version)
227+
pkg = replacer.Replace(path.Join("k8s.io", "api", "%[group]", "%[version]"))
224228
domain = coreDomain
225229
}
226230
}

pkg/model/resource/resource.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ limitations under the License.
1717
package resource
1818

1919
import (
20+
"fmt"
21+
"strings"
22+
2023
"sigs.k8s.io/kubebuilder/pkg/model/config"
2124
)
2225

@@ -62,3 +65,19 @@ func (r *Resource) GVK() config.GVK {
6265
Kind: r.Kind,
6366
}
6467
}
68+
69+
func wrapKey(key string) string {
70+
return fmt.Sprintf("%%[%s]", key)
71+
}
72+
73+
func (r Resource) Replacer() *strings.Replacer {
74+
var replacements []string
75+
76+
replacements = append(replacements, wrapKey("group"), r.Group)
77+
replacements = append(replacements, wrapKey("group-package-name"), r.GroupPackageName)
78+
replacements = append(replacements, wrapKey("version"), r.Version)
79+
replacements = append(replacements, wrapKey("kind"), strings.ToLower(r.Kind))
80+
replacements = append(replacements, wrapKey("plural"), strings.ToLower(r.Plural))
81+
82+
return strings.NewReplacer(replacements...)
83+
}

pkg/scaffold/api.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ package scaffold
1818

1919
import (
2020
"fmt"
21-
"path/filepath"
22-
"strings"
2321

2422
"sigs.k8s.io/kubebuilder/internal/config"
2523
"sigs.k8s.io/kubebuilder/pkg/model"
@@ -90,11 +88,6 @@ func (s *apiScaffolder) newUniverse() *model.Universe {
9088

9189
func (s *apiScaffolder) scaffoldV1() error {
9290
if s.doResource {
93-
fmt.Println(filepath.Join("pkg", "apis", s.resource.GroupPackageName, s.resource.Version,
94-
fmt.Sprintf("%s_types.go", strings.ToLower(s.resource.Kind))))
95-
fmt.Println(filepath.Join("pkg", "apis", s.resource.GroupPackageName, s.resource.Version,
96-
fmt.Sprintf("%s_types_test.go", strings.ToLower(s.resource.Kind))))
97-
9891
if err := machinery.NewScaffold().Execute(
9992
s.newUniverse(),
10093
&crdv1.Register{},
@@ -117,11 +110,6 @@ func (s *apiScaffolder) scaffoldV1() error {
117110
}
118111

119112
if s.doController {
120-
fmt.Println(filepath.Join("pkg", "controller", strings.ToLower(s.resource.Kind),
121-
fmt.Sprintf("%s_controller.go", strings.ToLower(s.resource.Kind))))
122-
fmt.Println(filepath.Join("pkg", "controller", strings.ToLower(s.resource.Kind),
123-
fmt.Sprintf("%s_controller_test.go", strings.ToLower(s.resource.Kind))))
124-
125113
if err := machinery.NewScaffold().Execute(
126114
s.newUniverse(),
127115
&controllerv1.Controller{},
@@ -145,14 +133,6 @@ func (s *apiScaffolder) scaffoldV2() error {
145133
}
146134
}
147135

148-
if s.config.MultiGroup {
149-
fmt.Println(filepath.Join("apis", s.resource.Group, s.resource.Version,
150-
fmt.Sprintf("%s_types.go", strings.ToLower(s.resource.Kind))))
151-
} else {
152-
fmt.Println(filepath.Join("api", s.resource.Version,
153-
fmt.Sprintf("%s_types.go", strings.ToLower(s.resource.Kind))))
154-
}
155-
156136
if err := machinery.NewScaffold(s.plugins...).Execute(
157137
s.newUniverse(),
158138
&templatesv2.Types{},
@@ -183,14 +163,6 @@ func (s *apiScaffolder) scaffoldV2() error {
183163
}
184164

185165
if s.doController {
186-
if s.config.MultiGroup {
187-
fmt.Println(filepath.Join("controllers", s.resource.Group,
188-
fmt.Sprintf("%s_controller.go", strings.ToLower(s.resource.Kind))))
189-
} else {
190-
fmt.Println(filepath.Join("controllers",
191-
fmt.Sprintf("%s_controller.go", strings.ToLower(s.resource.Kind))))
192-
}
193-
194166
if err := machinery.NewScaffold(s.plugins...).Execute(
195167
s.newUniverse(),
196168
&controllerv2.SuiteTest{},

pkg/scaffold/internal/templates/v1/controller/add.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ limitations under the License.
1717
package controller
1818

1919
import (
20-
"fmt"
2120
"path/filepath"
22-
"strings"
2321

2422
"sigs.k8s.io/kubebuilder/pkg/model/file"
2523
)
@@ -37,9 +35,9 @@ type AddController struct {
3735
// SetTemplateDefaults implements input.Template
3836
func (f *AddController) SetTemplateDefaults() error {
3937
if f.Path == "" {
40-
f.Path = filepath.Join("pkg", "controller", fmt.Sprintf(
41-
"add_%s.go", strings.ToLower(f.Resource.Kind)))
38+
f.Path = filepath.Join("pkg", "controller", "add_%[kind].go")
4239
}
40+
f.Path = f.Resource.Replacer().Replace(f.Path)
4341

4442
f.TemplateBody = addControllerTemplate
4543

pkg/scaffold/internal/templates/v1/controller/controller.go

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

1919
import (
20+
"fmt"
2021
"path/filepath"
21-
"strings"
2222

2323
"sigs.k8s.io/kubebuilder/pkg/model/file"
2424
)
@@ -35,10 +35,10 @@ type Controller struct {
3535
// SetTemplateDefaults implements input.Template
3636
func (f *Controller) SetTemplateDefaults() error {
3737
if f.Path == "" {
38-
f.Path = filepath.Join("pkg", "controller",
39-
strings.ToLower(f.Resource.Kind),
40-
strings.ToLower(f.Resource.Kind)+"_controller.go")
38+
f.Path = filepath.Join("pkg", "controller", "%[kind]", "%[kind]_controller.go")
4139
}
40+
f.Path = f.Resource.Replacer().Replace(f.Path)
41+
fmt.Println(f.Path)
4242

4343
f.TemplateBody = controllerTemplate
4444

pkg/scaffold/internal/templates/v1/controller/controllersuitetest.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package controller
1818

1919
import (
2020
"path/filepath"
21-
"strings"
2221

2322
"sigs.k8s.io/kubebuilder/pkg/model/file"
2423
)
@@ -36,9 +35,9 @@ type SuiteTest struct {
3635
// SetTemplateDefaults implements input.Template
3736
func (f *SuiteTest) SetTemplateDefaults() error {
3837
if f.Path == "" {
39-
f.Path = filepath.Join("pkg", "controller",
40-
strings.ToLower(f.Resource.Kind), strings.ToLower(f.Resource.Kind)+"_controller_suite_test.go")
38+
f.Path = filepath.Join("pkg", "controller", "%[kind]", "%[kind]_controller_suite_test.go")
4139
}
40+
f.Path = f.Resource.Replacer().Replace(f.Path)
4241

4342
f.TemplateBody = controllerSuiteTestTemplate
4443

pkg/scaffold/internal/templates/v1/controller/controllertest.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ limitations under the License.
1717
package controller
1818

1919
import (
20+
"fmt"
2021
"path/filepath"
21-
"strings"
2222

2323
"sigs.k8s.io/kubebuilder/pkg/model/file"
2424
)
@@ -35,9 +35,10 @@ type Test struct {
3535
// SetTemplateDefaults implements input.Template
3636
func (f *Test) SetTemplateDefaults() error {
3737
if f.Path == "" {
38-
f.Path = filepath.Join("pkg", "controller",
39-
strings.ToLower(f.Resource.Kind), strings.ToLower(f.Resource.Kind)+"_controller_test.go")
38+
f.Path = filepath.Join("pkg", "controller", "%[kind]", "%[kind]_controller_test.go")
4039
}
40+
f.Path = f.Resource.Replacer().Replace(f.Path)
41+
fmt.Println(f.Path)
4142

4243
f.TemplateBody = controllerTestTemplate
4344

pkg/scaffold/internal/templates/v1/crd/addtoscheme.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package crd
1818

1919
import (
20-
"fmt"
2120
"path/filepath"
2221

2322
"sigs.k8s.io/kubebuilder/pkg/model/file"
@@ -35,9 +34,9 @@ type AddToScheme struct {
3534
// SetTemplateDefaults implements input.Template
3635
func (f *AddToScheme) SetTemplateDefaults() error {
3736
if f.Path == "" {
38-
f.Path = filepath.Join("pkg", "apis", fmt.Sprintf(
39-
"addtoscheme_%s_%s.go", f.Resource.GroupPackageName, f.Resource.Version))
37+
f.Path = filepath.Join("pkg", "apis", "addtoscheme_%[group-package-name]_%[version].go")
4038
}
39+
f.Path = f.Resource.Replacer().Replace(f.Path)
4140

4241
f.TemplateBody = addResourceTemplate
4342

pkg/scaffold/internal/templates/v1/crd/crd_sample.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ limitations under the License.
1717
package crd
1818

1919
import (
20-
"fmt"
2120
"path/filepath"
22-
"strings"
2321

2422
"sigs.k8s.io/kubebuilder/pkg/model/file"
2523
)
@@ -36,9 +34,9 @@ type CRDSample struct {
3634
// SetTemplateDefaults implements input.Template
3735
func (f *CRDSample) SetTemplateDefaults() error {
3836
if f.Path == "" {
39-
f.Path = filepath.Join("config", "samples", fmt.Sprintf(
40-
"%s_%s_%s.yaml", f.Resource.GroupPackageName, f.Resource.Version, strings.ToLower(f.Resource.Kind)))
37+
f.Path = filepath.Join("config", "samples", "%[group-package-name]_%[version]_%[kind].yaml")
4138
}
39+
f.Path = f.Resource.Replacer().Replace(f.Path)
4240

4341
f.TemplateBody = crdSampleTemplate
4442

pkg/scaffold/internal/templates/v1/crd/doc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ type Doc struct {
3535
// SetTemplateDefaults implements input.Template
3636
func (f *Doc) SetTemplateDefaults() error {
3737
if f.Path == "" {
38-
f.Path = filepath.Join("pkg", "apis", f.Resource.GroupPackageName, f.Resource.Version, "doc.go")
38+
f.Path = filepath.Join("pkg", "apis", "%[group-package-name]", "%[version]", "doc.go")
3939
}
40+
f.Path = f.Resource.Replacer().Replace(f.Path)
4041

4142
f.TemplateBody = docGoTemplate
4243

0 commit comments

Comments
 (0)