Skip to content

Commit 1d98c75

Browse files
committed
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.
1 parent 6452e9f commit 1d98c75

File tree

9 files changed

+26
-19
lines changed

9 files changed

+26
-19
lines changed

pkg/cli/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
const apiErrorMsg = "failed to create API"
2828

29-
func (c CLI) newCreateAPICmd() *cobra.Command {
29+
func (c *CLI) newCreateAPICmd() *cobra.Command {
3030
cmd := &cobra.Command{
3131
Use: "api",
3232
Short: "Scaffold a Kubernetes API",

pkg/cli/cli.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ func (c *CLI) addExtraCommands() error {
449449
}
450450

451451
// printDeprecationWarnings prints the deprecation warnings of the resolved plugins.
452-
func (c CLI) printDeprecationWarnings() {
452+
func (c *CLI) printDeprecationWarnings() {
453453
for _, p := range c.resolvedPlugins {
454454
if p != nil && p.(plugin.Deprecated) != nil && len(p.(plugin.Deprecated).DeprecationWarning()) > 0 {
455455
_, _ = fmt.Fprintf(os.Stderr, noticeColor, fmt.Sprintf(deprecationFmt, p.(plugin.Deprecated).DeprecationWarning()))
@@ -458,7 +458,7 @@ func (c CLI) printDeprecationWarnings() {
458458
}
459459

460460
// metadata returns CLI's metadata.
461-
func (c CLI) metadata() plugin.CLIMetadata {
461+
func (c *CLI) metadata() plugin.CLIMetadata {
462462
return plugin.CLIMetadata{
463463
CommandName: c.commandName,
464464
}
@@ -467,11 +467,18 @@ func (c CLI) metadata() plugin.CLIMetadata {
467467
// Run executes the CLI utility.
468468
//
469469
// If an error is found, command help and examples will be printed.
470-
func (c CLI) Run() error {
470+
func (c *CLI) Run() error {
471471
return c.cmd.Execute()
472472
}
473473

474474
// Command returns the underlying root command.
475+
//
476+
// Deprecated: Use (c *CLI).CommandPtr() instead.
475477
func (c CLI) Command() *cobra.Command {
478+
return (&c).CommandPtr()
479+
}
480+
481+
// CommandPtr returns the underlying root command.
482+
func (c *CLI) CommandPtr() *cobra.Command {
476483
return c.cmd
477484
}

pkg/cli/completion.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/spf13/cobra"
2424
)
2525

26-
func (c CLI) newBashCmd() *cobra.Command {
26+
func (c *CLI) newBashCmd() *cobra.Command {
2727
return &cobra.Command{
2828
Use: "bash",
2929
Short: "Load bash completions",
@@ -42,7 +42,7 @@ MacOS:
4242
}
4343
}
4444

45-
func (c CLI) newZshCmd() *cobra.Command {
45+
func (c *CLI) newZshCmd() *cobra.Command {
4646
return &cobra.Command{
4747
Use: "zsh",
4848
Short: "Load zsh completions",
@@ -61,7 +61,7 @@ $ %[1]s completion zsh > "${fpath[1]}/_%[1]s"
6161
}
6262
}
6363

64-
func (c CLI) newFishCmd() *cobra.Command {
64+
func (c *CLI) newFishCmd() *cobra.Command {
6565
return &cobra.Command{
6666
Use: "fish",
6767
Short: "Load fish completions",
@@ -77,7 +77,7 @@ $ %[1]s completion fish > ~/.config/fish/completions/%[1]s.fish
7777
}
7878
}
7979

80-
func (CLI) newPowerShellCmd() *cobra.Command {
80+
func (c *CLI) newPowerShellCmd() *cobra.Command {
8181
return &cobra.Command{
8282
Use: "powershell",
8383
Short: "Load powershell completions",
@@ -87,7 +87,7 @@ func (CLI) newPowerShellCmd() *cobra.Command {
8787
}
8888
}
8989

90-
func (c CLI) newCompletionCmd() *cobra.Command {
90+
func (c *CLI) newCompletionCmd() *cobra.Command {
9191
cmd := &cobra.Command{
9292
Use: "completion",
9393
Short: "Load completions for the specified shell",

pkg/cli/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"github.com/spf13/cobra"
2121
)
2222

23-
func (CLI) newCreateCmd() *cobra.Command {
23+
func (c *CLI) newCreateCmd() *cobra.Command {
2424
return &cobra.Command{
2525
Use: "create",
2626
SuggestFor: []string{"new"},

pkg/cli/edit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
const editErrorMsg = "failed to edit project"
2828

29-
func (c CLI) newEditCmd() *cobra.Command {
29+
func (c *CLI) newEditCmd() *cobra.Command {
3030
cmd := &cobra.Command{
3131
Use: "edit",
3232
Short: "Update the project configuration",

pkg/cli/init.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030

3131
const initErrorMsg = "failed to initialize project"
3232

33-
func (c CLI) newInitCmd() *cobra.Command {
33+
func (c *CLI) newInitCmd() *cobra.Command {
3434
cmd := &cobra.Command{
3535
Use: "init",
3636
Short: "Initialize a new project",
@@ -75,7 +75,7 @@ For further help about a specific plugin, set --plugins.
7575
return cmd
7676
}
7777

78-
func (c CLI) getInitHelpExamples() string {
78+
func (c *CLI) getInitHelpExamples() string {
7979
var sb strings.Builder
8080
for _, version := range c.getAvailableProjectVersions() {
8181
rendered := fmt.Sprintf(` # Help for initializing a project with version %[2]s
@@ -88,7 +88,7 @@ func (c CLI) getInitHelpExamples() string {
8888
return strings.TrimSuffix(sb.String(), "\n\n")
8989
}
9090

91-
func (c CLI) getAvailableProjectVersions() (projectVersions []string) {
91+
func (c *CLI) getAvailableProjectVersions() (projectVersions []string) {
9292
versionSet := make(map[config.Version]struct{})
9393
for _, p := range c.plugins {
9494
// Only return versions of non-deprecated plugins.

pkg/cli/root.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const (
3131

3232
var supportedPlatforms = []string{"darwin", "linux"}
3333

34-
func (c CLI) newRootCmd() *cobra.Command {
34+
func (c *CLI) newRootCmd() *cobra.Command {
3535
cmd := &cobra.Command{
3636
Use: c.commandName,
3737
Long: c.description,
@@ -55,7 +55,7 @@ func (c CLI) newRootCmd() *cobra.Command {
5555
}
5656

5757
// rootExamples builds the examples string for the root command before resolving plugins
58-
func (c CLI) rootExamples() string {
58+
func (c *CLI) rootExamples() string {
5959
str := fmt.Sprintf(`The first step is to initialize your project:
6060
%[1]s init [--plugins=<PLUGIN KEYS> [--project-version=<PROJECT VERSION>]]
6161
@@ -84,7 +84,7 @@ configuration please run:
8484
}
8585

8686
// getPluginTable returns an ASCII table of the available plugins and their supported project versions.
87-
func (c CLI) getPluginTable() string {
87+
func (c *CLI) getPluginTable() string {
8888
var (
8989
maxPluginKeyLength = len(pluginKeysHeader)
9090
pluginKeys = make([]string, 0, len(c.plugins))

pkg/cli/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/spf13/cobra"
2323
)
2424

25-
func (c CLI) newVersionCmd() *cobra.Command {
25+
func (c *CLI) newVersionCmd() *cobra.Command {
2626
cmd := &cobra.Command{
2727
Use: "version",
2828
Short: fmt.Sprintf("Print the %s version", c.commandName),

pkg/cli/webhook.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
const webhookErrorMsg = "failed to create webhook"
2828

29-
func (c CLI) newCreateWebhookCmd() *cobra.Command {
29+
func (c *CLI) newCreateWebhookCmd() *cobra.Command {
3030
cmd := &cobra.Command{
3131
Use: "webhook",
3232
Short: "Scaffold a webhook for an API resource",

0 commit comments

Comments
 (0)