Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/pkg/agent/cmd: remove redundant addCommandIfNotNil func #7502

Merged
merged 1 commit into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions internal/pkg/agent/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ package cmd

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
)

func TestAgent(t *testing.T) {
Expand All @@ -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)
}
38 changes: 15 additions & 23 deletions internal/pkg/agent/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Loading