From 1d98c750a046ed1d775870e82563d8bf775f7626 Mon Sep 17 00:00:00 2001 From: Kersten Burkhardt Date: Wed, 26 Mar 2025 12:33:26 +0100 Subject: [PATCH] refactor: convert CLI methods to use pointer receivers Updated all CLI method receivers to use pointer semantics (*CLI) instead of value (CLI), ensuring consistent behavior across command setup functions and reducing potential for unintentional value copying. --- pkg/cli/api.go | 2 +- pkg/cli/cli.go | 13 ++++++++++--- pkg/cli/completion.go | 10 +++++----- pkg/cli/create.go | 2 +- pkg/cli/edit.go | 2 +- pkg/cli/init.go | 6 +++--- pkg/cli/root.go | 6 +++--- pkg/cli/version.go | 2 +- pkg/cli/webhook.go | 2 +- 9 files changed, 26 insertions(+), 19 deletions(-) diff --git a/pkg/cli/api.go b/pkg/cli/api.go index d8e600b145a..45dacbf3b49 100644 --- a/pkg/cli/api.go +++ b/pkg/cli/api.go @@ -26,7 +26,7 @@ import ( const apiErrorMsg = "failed to create API" -func (c CLI) newCreateAPICmd() *cobra.Command { +func (c *CLI) newCreateAPICmd() *cobra.Command { cmd := &cobra.Command{ Use: "api", Short: "Scaffold a Kubernetes API", diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 5d069290cdc..9420cd4e555 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -449,7 +449,7 @@ func (c *CLI) addExtraCommands() error { } // printDeprecationWarnings prints the deprecation warnings of the resolved plugins. -func (c CLI) printDeprecationWarnings() { +func (c *CLI) printDeprecationWarnings() { for _, p := range c.resolvedPlugins { if p != nil && p.(plugin.Deprecated) != nil && len(p.(plugin.Deprecated).DeprecationWarning()) > 0 { _, _ = fmt.Fprintf(os.Stderr, noticeColor, fmt.Sprintf(deprecationFmt, p.(plugin.Deprecated).DeprecationWarning())) @@ -458,7 +458,7 @@ func (c CLI) printDeprecationWarnings() { } // metadata returns CLI's metadata. -func (c CLI) metadata() plugin.CLIMetadata { +func (c *CLI) metadata() plugin.CLIMetadata { return plugin.CLIMetadata{ CommandName: c.commandName, } @@ -467,11 +467,18 @@ func (c CLI) metadata() plugin.CLIMetadata { // Run executes the CLI utility. // // If an error is found, command help and examples will be printed. -func (c CLI) Run() error { +func (c *CLI) Run() error { return c.cmd.Execute() } // Command returns the underlying root command. +// +// Deprecated: Use (c *CLI).CommandPtr() instead. func (c CLI) Command() *cobra.Command { + return (&c).CommandPtr() +} + +// CommandPtr returns the underlying root command. +func (c *CLI) CommandPtr() *cobra.Command { return c.cmd } diff --git a/pkg/cli/completion.go b/pkg/cli/completion.go index 41029d1a012..4ea548d3873 100644 --- a/pkg/cli/completion.go +++ b/pkg/cli/completion.go @@ -23,7 +23,7 @@ import ( "github.com/spf13/cobra" ) -func (c CLI) newBashCmd() *cobra.Command { +func (c *CLI) newBashCmd() *cobra.Command { return &cobra.Command{ Use: "bash", Short: "Load bash completions", @@ -42,7 +42,7 @@ MacOS: } } -func (c CLI) newZshCmd() *cobra.Command { +func (c *CLI) newZshCmd() *cobra.Command { return &cobra.Command{ Use: "zsh", Short: "Load zsh completions", @@ -61,7 +61,7 @@ $ %[1]s completion zsh > "${fpath[1]}/_%[1]s" } } -func (c CLI) newFishCmd() *cobra.Command { +func (c *CLI) newFishCmd() *cobra.Command { return &cobra.Command{ Use: "fish", Short: "Load fish completions", @@ -77,7 +77,7 @@ $ %[1]s completion fish > ~/.config/fish/completions/%[1]s.fish } } -func (CLI) newPowerShellCmd() *cobra.Command { +func (c *CLI) newPowerShellCmd() *cobra.Command { return &cobra.Command{ Use: "powershell", Short: "Load powershell completions", @@ -87,7 +87,7 @@ func (CLI) newPowerShellCmd() *cobra.Command { } } -func (c CLI) newCompletionCmd() *cobra.Command { +func (c *CLI) newCompletionCmd() *cobra.Command { cmd := &cobra.Command{ Use: "completion", Short: "Load completions for the specified shell", diff --git a/pkg/cli/create.go b/pkg/cli/create.go index f698b96ceea..0e447f11fef 100644 --- a/pkg/cli/create.go +++ b/pkg/cli/create.go @@ -20,7 +20,7 @@ import ( "github.com/spf13/cobra" ) -func (CLI) newCreateCmd() *cobra.Command { +func (c *CLI) newCreateCmd() *cobra.Command { return &cobra.Command{ Use: "create", SuggestFor: []string{"new"}, diff --git a/pkg/cli/edit.go b/pkg/cli/edit.go index c66ec185464..60131b78e1b 100644 --- a/pkg/cli/edit.go +++ b/pkg/cli/edit.go @@ -26,7 +26,7 @@ import ( const editErrorMsg = "failed to edit project" -func (c CLI) newEditCmd() *cobra.Command { +func (c *CLI) newEditCmd() *cobra.Command { cmd := &cobra.Command{ Use: "edit", Short: "Update the project configuration", diff --git a/pkg/cli/init.go b/pkg/cli/init.go index 1ad2a678357..a420a593d9b 100644 --- a/pkg/cli/init.go +++ b/pkg/cli/init.go @@ -30,7 +30,7 @@ import ( const initErrorMsg = "failed to initialize project" -func (c CLI) newInitCmd() *cobra.Command { +func (c *CLI) newInitCmd() *cobra.Command { cmd := &cobra.Command{ Use: "init", Short: "Initialize a new project", @@ -75,7 +75,7 @@ For further help about a specific plugin, set --plugins. return cmd } -func (c CLI) getInitHelpExamples() string { +func (c *CLI) getInitHelpExamples() string { var sb strings.Builder for _, version := range c.getAvailableProjectVersions() { rendered := fmt.Sprintf(` # Help for initializing a project with version %[2]s @@ -88,7 +88,7 @@ func (c CLI) getInitHelpExamples() string { return strings.TrimSuffix(sb.String(), "\n\n") } -func (c CLI) getAvailableProjectVersions() (projectVersions []string) { +func (c *CLI) getAvailableProjectVersions() (projectVersions []string) { versionSet := make(map[config.Version]struct{}) for _, p := range c.plugins { // Only return versions of non-deprecated plugins. diff --git a/pkg/cli/root.go b/pkg/cli/root.go index c73191196ec..2b26435c7fe 100644 --- a/pkg/cli/root.go +++ b/pkg/cli/root.go @@ -31,7 +31,7 @@ const ( var supportedPlatforms = []string{"darwin", "linux"} -func (c CLI) newRootCmd() *cobra.Command { +func (c *CLI) newRootCmd() *cobra.Command { cmd := &cobra.Command{ Use: c.commandName, Long: c.description, @@ -55,7 +55,7 @@ func (c CLI) newRootCmd() *cobra.Command { } // rootExamples builds the examples string for the root command before resolving plugins -func (c CLI) rootExamples() string { +func (c *CLI) rootExamples() string { str := fmt.Sprintf(`The first step is to initialize your project: %[1]s init [--plugins= [--project-version=]] @@ -84,7 +84,7 @@ configuration please run: } // getPluginTable returns an ASCII table of the available plugins and their supported project versions. -func (c CLI) getPluginTable() string { +func (c *CLI) getPluginTable() string { var ( maxPluginKeyLength = len(pluginKeysHeader) pluginKeys = make([]string, 0, len(c.plugins)) diff --git a/pkg/cli/version.go b/pkg/cli/version.go index f721c41ff0f..c15e06bf8a7 100644 --- a/pkg/cli/version.go +++ b/pkg/cli/version.go @@ -22,7 +22,7 @@ import ( "github.com/spf13/cobra" ) -func (c CLI) newVersionCmd() *cobra.Command { +func (c *CLI) newVersionCmd() *cobra.Command { cmd := &cobra.Command{ Use: "version", Short: fmt.Sprintf("Print the %s version", c.commandName), diff --git a/pkg/cli/webhook.go b/pkg/cli/webhook.go index 09b563dc7f7..2ba956b856a 100644 --- a/pkg/cli/webhook.go +++ b/pkg/cli/webhook.go @@ -26,7 +26,7 @@ import ( const webhookErrorMsg = "failed to create webhook" -func (c CLI) newCreateWebhookCmd() *cobra.Command { +func (c *CLI) newCreateWebhookCmd() *cobra.Command { cmd := &cobra.Command{ Use: "webhook", Short: "Scaffold a webhook for an API resource",