Skip to content

Commit 2167fb2

Browse files
authored
internal/pkg/agent/cmd: remove redundant addCommandIfNotNil func (#7502)
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.
1 parent 0a63787 commit 2167fb2

File tree

2 files changed

+15
-41
lines changed

2 files changed

+15
-41
lines changed

internal/pkg/agent/cmd/cmd_test.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ package cmd
66

77
import (
88
"testing"
9-
10-
"github.com/spf13/cobra"
11-
"github.com/stretchr/testify/require"
129
)
1310

1411
func TestAgent(t *testing.T) {
@@ -33,18 +30,3 @@ func TestAgent(t *testing.T) {
3330
// assert.True(t, strings.Contains(string(contents), "Hello I am running"))
3431
// })
3532
}
36-
37-
func TestAddCommandIfNotNil(t *testing.T) {
38-
cmd := &cobra.Command{}
39-
40-
parent := &cobra.Command{}
41-
addCommandIfNotNil(parent, cmd)
42-
require.Equal(t, 1, len(parent.Commands()))
43-
44-
parent = &cobra.Command{}
45-
addCommandIfNotNil(parent, nil)
46-
require.Equal(t, 0, len(parent.Commands()))
47-
48-
// this should not panic
49-
addCommandIfNotNil(nil, cmd)
50-
}

internal/pkg/agent/cmd/common.go

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,21 @@ func NewCommandWithArgs(args []string, streams *cli.IOStreams) *cobra.Command {
8080
cmd.AddCommand(basecmd.NewDefaultCommandsWithArgs(args, streams)...)
8181
cmd.AddCommand(run)
8282

83-
addCommandIfNotNil(cmd, newInstallCommandWithArgs(args, streams))
84-
addCommandIfNotNil(cmd, newUninstallCommandWithArgs(args, streams))
85-
addCommandIfNotNil(cmd, newUpgradeCommandWithArgs(args, streams))
86-
addCommandIfNotNil(cmd, newEnrollCommandWithArgs(args, streams))
87-
addCommandIfNotNil(cmd, newInspectCommandWithArgs(args, streams))
88-
addCommandIfNotNil(cmd, newPrivilegedCommandWithArgs(args, streams))
89-
addCommandIfNotNil(cmd, newUnprivilegedCommandWithArgs(args, streams))
90-
addCommandIfNotNil(cmd, newWatchCommandWithArgs(args, streams))
91-
addCommandIfNotNil(cmd, newContainerCommand(args, streams))
92-
addCommandIfNotNil(cmd, newStatusCommand(args, streams))
93-
addCommandIfNotNil(cmd, newDiagnosticsCommand(args, streams))
94-
addCommandIfNotNil(cmd, newComponentCommandWithArgs(args, streams))
95-
addCommandIfNotNil(cmd, newLogsCommandWithArgs(args, streams))
96-
addCommandIfNotNil(cmd, newOtelCommandWithArgs(args, streams))
97-
addCommandIfNotNil(cmd, newApplyFlavorCommandWithArgs(args, streams))
83+
cmd.AddCommand(newInstallCommandWithArgs(args, streams))
84+
cmd.AddCommand(newUninstallCommandWithArgs(args, streams))
85+
cmd.AddCommand(newUpgradeCommandWithArgs(args, streams))
86+
cmd.AddCommand(newEnrollCommandWithArgs(args, streams))
87+
cmd.AddCommand(newInspectCommandWithArgs(args, streams))
88+
cmd.AddCommand(newPrivilegedCommandWithArgs(args, streams))
89+
cmd.AddCommand(newUnprivilegedCommandWithArgs(args, streams))
90+
cmd.AddCommand(newWatchCommandWithArgs(args, streams))
91+
cmd.AddCommand(newContainerCommand(args, streams))
92+
cmd.AddCommand(newStatusCommand(args, streams))
93+
cmd.AddCommand(newDiagnosticsCommand(args, streams))
94+
cmd.AddCommand(newComponentCommandWithArgs(args, streams))
95+
cmd.AddCommand(newLogsCommandWithArgs(args, streams))
96+
cmd.AddCommand(newOtelCommandWithArgs(args, streams))
97+
cmd.AddCommand(newApplyFlavorCommandWithArgs(args, streams))
9898

9999
// windows special hidden sub-command (only added on Windows)
100100
reexec := newReExecWindowsCommand(args, streams)
@@ -106,11 +106,3 @@ func NewCommandWithArgs(args []string, streams *cli.IOStreams) *cobra.Command {
106106

107107
return cmd
108108
}
109-
110-
func addCommandIfNotNil(parent, cmd *cobra.Command) {
111-
if cmd == nil || parent == nil {
112-
return
113-
}
114-
115-
parent.AddCommand(cmd)
116-
}

0 commit comments

Comments
 (0)