From 7224c7724087705b2e49d5667c25fc7796d0fe73 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Fri, 21 Mar 2025 09:02:14 +1030 Subject: [PATCH] internal/pkg/agent/cmd: remove redundant addCommandIfNotNil func None of the subcommand construction functions return a nil *cobra.Command, althought this was apparently the case in the past. So remove the wrapper func and its tests. --- internal/pkg/agent/cmd/cmd_test.go | 18 -------------- internal/pkg/agent/cmd/common.go | 38 ++++++++++++------------------ 2 files changed, 15 insertions(+), 41 deletions(-) diff --git a/internal/pkg/agent/cmd/cmd_test.go b/internal/pkg/agent/cmd/cmd_test.go index 9ccceaed5b4..66033e80947 100644 --- a/internal/pkg/agent/cmd/cmd_test.go +++ b/internal/pkg/agent/cmd/cmd_test.go @@ -6,9 +6,6 @@ package cmd import ( "testing" - - "github.com/spf13/cobra" - "github.com/stretchr/testify/require" ) func TestAgent(t *testing.T) { @@ -33,18 +30,3 @@ func TestAgent(t *testing.T) { // assert.True(t, strings.Contains(string(contents), "Hello I am running")) // }) } - -func TestAddCommandIfNotNil(t *testing.T) { - cmd := &cobra.Command{} - - parent := &cobra.Command{} - addCommandIfNotNil(parent, cmd) - require.Equal(t, 1, len(parent.Commands())) - - parent = &cobra.Command{} - addCommandIfNotNil(parent, nil) - require.Equal(t, 0, len(parent.Commands())) - - // this should not panic - addCommandIfNotNil(nil, cmd) -} diff --git a/internal/pkg/agent/cmd/common.go b/internal/pkg/agent/cmd/common.go index 0bec7dc3540..5c98b07cff9 100644 --- a/internal/pkg/agent/cmd/common.go +++ b/internal/pkg/agent/cmd/common.go @@ -80,21 +80,21 @@ func NewCommandWithArgs(args []string, streams *cli.IOStreams) *cobra.Command { cmd.AddCommand(basecmd.NewDefaultCommandsWithArgs(args, streams)...) cmd.AddCommand(run) - addCommandIfNotNil(cmd, newInstallCommandWithArgs(args, streams)) - addCommandIfNotNil(cmd, newUninstallCommandWithArgs(args, streams)) - addCommandIfNotNil(cmd, newUpgradeCommandWithArgs(args, streams)) - addCommandIfNotNil(cmd, newEnrollCommandWithArgs(args, streams)) - addCommandIfNotNil(cmd, newInspectCommandWithArgs(args, streams)) - addCommandIfNotNil(cmd, newPrivilegedCommandWithArgs(args, streams)) - addCommandIfNotNil(cmd, newUnprivilegedCommandWithArgs(args, streams)) - addCommandIfNotNil(cmd, newWatchCommandWithArgs(args, streams)) - addCommandIfNotNil(cmd, newContainerCommand(args, streams)) - addCommandIfNotNil(cmd, newStatusCommand(args, streams)) - addCommandIfNotNil(cmd, newDiagnosticsCommand(args, streams)) - addCommandIfNotNil(cmd, newComponentCommandWithArgs(args, streams)) - addCommandIfNotNil(cmd, newLogsCommandWithArgs(args, streams)) - addCommandIfNotNil(cmd, newOtelCommandWithArgs(args, streams)) - addCommandIfNotNil(cmd, newApplyFlavorCommandWithArgs(args, streams)) + cmd.AddCommand(newInstallCommandWithArgs(args, streams)) + cmd.AddCommand(newUninstallCommandWithArgs(args, streams)) + cmd.AddCommand(newUpgradeCommandWithArgs(args, streams)) + cmd.AddCommand(newEnrollCommandWithArgs(args, streams)) + cmd.AddCommand(newInspectCommandWithArgs(args, streams)) + cmd.AddCommand(newPrivilegedCommandWithArgs(args, streams)) + cmd.AddCommand(newUnprivilegedCommandWithArgs(args, streams)) + cmd.AddCommand(newWatchCommandWithArgs(args, streams)) + cmd.AddCommand(newContainerCommand(args, streams)) + cmd.AddCommand(newStatusCommand(args, streams)) + cmd.AddCommand(newDiagnosticsCommand(args, streams)) + cmd.AddCommand(newComponentCommandWithArgs(args, streams)) + cmd.AddCommand(newLogsCommandWithArgs(args, streams)) + cmd.AddCommand(newOtelCommandWithArgs(args, streams)) + cmd.AddCommand(newApplyFlavorCommandWithArgs(args, streams)) // windows special hidden sub-command (only added on Windows) reexec := newReExecWindowsCommand(args, streams) @@ -106,11 +106,3 @@ func NewCommandWithArgs(args []string, streams *cli.IOStreams) *cobra.Command { return cmd } - -func addCommandIfNotNil(parent, cmd *cobra.Command) { - if cmd == nil || parent == nil { - return - } - - parent.AddCommand(cmd) -}