Skip to content

Commit 4d1a69e

Browse files
authored
Merge pull request #4697 from kersten/fix/shadowing-generate-vars
🌱 (chore): avoid shadowing of 'config', 'store', and 'resource' in cli/alpha/generate
2 parents 2b35f33 + 8f7f3f4 commit 4d1a69e

File tree

1 file changed

+65
-65
lines changed

1 file changed

+65
-65
lines changed

pkg/cli/alpha/internal/generate.go

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,23 @@ func changeWorkingDirectory(outputDir string) error {
148148
}
149149

150150
// Initializes the project with Kubebuilder.
151-
func kubebuilderInit(store store.Store) error {
152-
args := append([]string{"init"}, getInitArgs(store)...)
151+
func kubebuilderInit(s store.Store) error {
152+
args := append([]string{"init"}, getInitArgs(s)...)
153153
return util.RunCmd("kubebuilder init", "kubebuilder", args...)
154154
}
155155

156156
// Edits the project to enable or disable multigroup layout.
157-
func kubebuilderEdit(store store.Store) error {
158-
if store.Config().IsMultiGroup() {
157+
func kubebuilderEdit(s store.Store) error {
158+
if s.Config().IsMultiGroup() {
159159
args := []string{"edit", "--multigroup"}
160160
return util.RunCmd("kubebuilder edit", "kubebuilder", args...)
161161
}
162162
return nil
163163
}
164164

165165
// Creates APIs and Webhooks for the project.
166-
func kubebuilderCreate(store store.Store) error {
167-
resources, err := store.Config().GetResources()
166+
func kubebuilderCreate(s store.Store) error {
167+
resources, err := s.Config().GetResources()
168168
if err != nil {
169169
return fmt.Errorf("failed to get resources: %w", err)
170170
}
@@ -188,9 +188,9 @@ func kubebuilderCreate(store store.Store) error {
188188
}
189189

190190
// Migrates the Grafana plugin.
191-
func migrateGrafanaPlugin(store store.Store, src, des string) error {
191+
func migrateGrafanaPlugin(s store.Store, src, des string) error {
192192
var grafanaPlugin struct{}
193-
err := store.Config().DecodePluginConfig(plugin.KeyFor(v1alpha.Plugin{}), grafanaPlugin)
193+
err := s.Config().DecodePluginConfig(plugin.KeyFor(v1alpha.Plugin{}), grafanaPlugin)
194194
if errors.As(err, &config.PluginKeyNotFoundError{}) {
195195
log.Info("Grafana plugin not found, skipping migration")
196196
return nil
@@ -210,9 +210,9 @@ func migrateGrafanaPlugin(store store.Store, src, des string) error {
210210
}
211211

212212
// Migrates the Deploy Image plugin.
213-
func migrateDeployImagePlugin(store store.Store) error {
213+
func migrateDeployImagePlugin(s store.Store) error {
214214
var deployImagePlugin v1alpha1.PluginConfig
215-
err := store.Config().DecodePluginConfig(plugin.KeyFor(v1alpha1.Plugin{}), &deployImagePlugin)
215+
err := s.Config().DecodePluginConfig(plugin.KeyFor(v1alpha1.Plugin{}), &deployImagePlugin)
216216
if errors.As(err, &config.PluginKeyNotFoundError{}) {
217217
log.Info("Deploy-image plugin not found, skipping migration")
218218
return nil
@@ -230,9 +230,9 @@ func migrateDeployImagePlugin(store store.Store) error {
230230
}
231231

232232
// Creates an API with Deploy Image plugin.
233-
func createAPIWithDeployImage(resource v1alpha1.ResourceData) error {
234-
args := append([]string{"create", "api"}, getGVKFlagsFromDeployImage(resource)...)
235-
args = append(args, getDeployImageOptions(resource)...)
233+
func createAPIWithDeployImage(resourceData v1alpha1.ResourceData) error {
234+
args := append([]string{"create", "api"}, getGVKFlagsFromDeployImage(resourceData)...)
235+
args = append(args, getDeployImageOptions(resourceData)...)
236236
return util.RunCmd("kubebuilder create api", "kubebuilder", args...)
237237
}
238238

@@ -253,9 +253,9 @@ func getInputPath(inputPath string) (string, error) {
253253
}
254254

255255
// Helper function to get Init arguments for Kubebuilder.
256-
func getInitArgs(store store.Store) []string {
256+
func getInitArgs(s store.Store) []string {
257257
var args []string
258-
plugins := store.Config().GetPluginChain()
258+
plugins := s.Config().GetPluginChain()
259259

260260
// Define outdated plugin versions that need replacement
261261
outdatedPlugins := map[string]string{
@@ -277,96 +277,96 @@ func getInitArgs(store store.Store) []string {
277277
if len(plugins) > 0 {
278278
args = append(args, "--plugins", strings.Join(plugins, ","))
279279
}
280-
if domain := store.Config().GetDomain(); domain != "" {
280+
if domain := s.Config().GetDomain(); domain != "" {
281281
args = append(args, "--domain", domain)
282282
}
283-
if repo := store.Config().GetRepository(); repo != "" {
283+
if repo := s.Config().GetRepository(); repo != "" {
284284
args = append(args, "--repo", repo)
285285
}
286286
return args
287287
}
288288

289289
// Gets the GVK flags for a resource.
290-
func getGVKFlags(resource resource.Resource) []string {
290+
func getGVKFlags(res resource.Resource) []string {
291291
var args []string
292-
if resource.Plural != "" {
293-
args = append(args, "--plural", resource.Plural)
292+
if res.Plural != "" {
293+
args = append(args, "--plural", res.Plural)
294294
}
295-
if resource.Group != "" {
296-
args = append(args, "--group", resource.Group)
295+
if res.Group != "" {
296+
args = append(args, "--group", res.Group)
297297
}
298-
if resource.Version != "" {
299-
args = append(args, "--version", resource.Version)
298+
if res.Version != "" {
299+
args = append(args, "--version", res.Version)
300300
}
301-
if resource.Kind != "" {
302-
args = append(args, "--kind", resource.Kind)
301+
if res.Kind != "" {
302+
args = append(args, "--kind", res.Kind)
303303
}
304304
return args
305305
}
306306

307307
// Gets the GVK flags for a Deploy Image resource.
308-
func getGVKFlagsFromDeployImage(resource v1alpha1.ResourceData) []string {
308+
func getGVKFlagsFromDeployImage(resourceData v1alpha1.ResourceData) []string {
309309
var args []string
310-
if resource.Group != "" {
311-
args = append(args, "--group", resource.Group)
310+
if resourceData.Group != "" {
311+
args = append(args, "--group", resourceData.Group)
312312
}
313-
if resource.Version != "" {
314-
args = append(args, "--version", resource.Version)
313+
if resourceData.Version != "" {
314+
args = append(args, "--version", resourceData.Version)
315315
}
316-
if resource.Kind != "" {
317-
args = append(args, "--kind", resource.Kind)
316+
if resourceData.Kind != "" {
317+
args = append(args, "--kind", resourceData.Kind)
318318
}
319319
return args
320320
}
321321

322322
// Gets the options for a Deploy Image resource.
323-
func getDeployImageOptions(resource v1alpha1.ResourceData) []string {
323+
func getDeployImageOptions(resourceData v1alpha1.ResourceData) []string {
324324
var args []string
325-
if resource.Options.Image != "" {
326-
args = append(args, fmt.Sprintf("--image=%s", resource.Options.Image))
325+
if resourceData.Options.Image != "" {
326+
args = append(args, fmt.Sprintf("--image=%s", resourceData.Options.Image))
327327
}
328-
if resource.Options.ContainerCommand != "" {
329-
args = append(args, fmt.Sprintf("--image-container-command=%s", resource.Options.ContainerCommand))
328+
if resourceData.Options.ContainerCommand != "" {
329+
args = append(args, fmt.Sprintf("--image-container-command=%s", resourceData.Options.ContainerCommand))
330330
}
331-
if resource.Options.ContainerPort != "" {
332-
args = append(args, fmt.Sprintf("--image-container-port=%s", resource.Options.ContainerPort))
331+
if resourceData.Options.ContainerPort != "" {
332+
args = append(args, fmt.Sprintf("--image-container-port=%s", resourceData.Options.ContainerPort))
333333
}
334-
if resource.Options.RunAsUser != "" {
335-
args = append(args, fmt.Sprintf("--run-as-user=%s", resource.Options.RunAsUser))
334+
if resourceData.Options.RunAsUser != "" {
335+
args = append(args, fmt.Sprintf("--run-as-user=%s", resourceData.Options.RunAsUser))
336336
}
337337
args = append(args, fmt.Sprintf("--plugins=%s", plugin.KeyFor(v1alpha1.Plugin{})))
338338
return args
339339
}
340340

341341
// Creates an API resource.
342-
func createAPI(resource resource.Resource) error {
343-
args := append([]string{"create", "api"}, getGVKFlags(resource)...)
344-
args = append(args, getAPIResourceFlags(resource)...)
342+
func createAPI(res resource.Resource) error {
343+
args := append([]string{"create", "api"}, getGVKFlags(res)...)
344+
args = append(args, getAPIResourceFlags(res)...)
345345

346346
// Add the external API path flag if the resource is external
347-
if resource.IsExternal() {
348-
args = append(args, "--external-api-path", resource.Path)
349-
args = append(args, "--external-api-domain", resource.Domain)
347+
if res.IsExternal() {
348+
args = append(args, "--external-api-path", res.Path)
349+
args = append(args, "--external-api-domain", res.Domain)
350350
}
351351

352352
return util.RunCmd("kubebuilder create api", "kubebuilder", args...)
353353
}
354354

355355
// Gets flags for API resource creation.
356-
func getAPIResourceFlags(resource resource.Resource) []string {
356+
func getAPIResourceFlags(res resource.Resource) []string {
357357
var args []string
358358

359-
if resource.API == nil || resource.API.IsEmpty() {
359+
if res.API == nil || res.API.IsEmpty() {
360360
args = append(args, "--resource=false")
361361
} else {
362362
args = append(args, "--resource")
363-
if resource.API.Namespaced {
363+
if res.API.Namespaced {
364364
args = append(args, "--namespaced")
365365
} else {
366366
args = append(args, "--namespaced=false")
367367
}
368368
}
369-
if resource.Controller {
369+
if res.Controller {
370370
args = append(args, "--controller")
371371
} else {
372372
args = append(args, "--controller=false")
@@ -375,32 +375,32 @@ func getAPIResourceFlags(resource resource.Resource) []string {
375375
}
376376

377377
// Creates a webhook resource.
378-
func createWebhook(resource resource.Resource) error {
379-
if resource.Webhooks == nil || resource.Webhooks.IsEmpty() {
378+
func createWebhook(res resource.Resource) error {
379+
if res.Webhooks == nil || res.Webhooks.IsEmpty() {
380380
return nil
381381
}
382-
args := append([]string{"create", "webhook"}, getGVKFlags(resource)...)
383-
args = append(args, getWebhookResourceFlags(resource)...)
382+
args := append([]string{"create", "webhook"}, getGVKFlags(res)...)
383+
args = append(args, getWebhookResourceFlags(res)...)
384384
return util.RunCmd("kubebuilder create webhook", "kubebuilder", args...)
385385
}
386386

387387
// Gets flags for webhook creation.
388-
func getWebhookResourceFlags(resource resource.Resource) []string {
388+
func getWebhookResourceFlags(res resource.Resource) []string {
389389
var args []string
390-
if resource.IsExternal() {
391-
args = append(args, "--external-api-path", resource.Path)
392-
args = append(args, "--external-api-domain", resource.Domain)
390+
if res.IsExternal() {
391+
args = append(args, "--external-api-path", res.Path)
392+
args = append(args, "--external-api-domain", res.Domain)
393393
}
394-
if resource.HasValidationWebhook() {
394+
if res.HasValidationWebhook() {
395395
args = append(args, "--programmatic-validation")
396396
}
397-
if resource.HasDefaultingWebhook() {
397+
if res.HasDefaultingWebhook() {
398398
args = append(args, "--defaulting")
399399
}
400-
if resource.HasConversionWebhook() {
400+
if res.HasConversionWebhook() {
401401
args = append(args, "--conversion")
402-
if len(resource.Webhooks.Spoke) > 0 {
403-
for _, spoke := range resource.Webhooks.Spoke {
402+
if len(res.Webhooks.Spoke) > 0 {
403+
for _, spoke := range res.Webhooks.Spoke {
404404
args = append(args, "--spoke", spoke)
405405
}
406406
}

0 commit comments

Comments
 (0)