From a2f831366833e784159d685a5acd7396a56b3a6a Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Wed, 30 Jul 2025 17:01:03 +0700 Subject: [PATCH] refactor: pass rootCmd as parameter to Init*Cmd functions - Update all Init*Cmd function signatures to accept rootCmd parameter: * InitServiceCmd(rootCmd *cobra.Command) * InitClientsCmd(rootCmd *cobra.Command) * InitLogCmd(rootCmd *cobra.Command) * InitUpgradeCmd(rootCmd *cobra.Command) * InitRunCmd(rootCmd *cobra.Command) * InitInterfacesCmd(rootCmd *cobra.Command) - Update function calls in cli.go to pass rootCmd parameter - Update InitInterfacesCmd call in commands_service.go Benefits: - Eliminates global state dependency on rootCmd variable - Makes dependencies explicit in function signatures - Improves testability by allowing different root commands - Better encapsulation and modularity --- cmd/cli/cli.go | 10 +++++----- cmd/cli/commands_clients.go | 2 +- cmd/cli/commands_interfaces.go | 2 +- cmd/cli/commands_log.go | 2 +- cmd/cli/commands_run.go | 2 +- cmd/cli/commands_service.go | 4 ++-- cmd/cli/commands_upgrade.go | 2 +- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cmd/cli/cli.go b/cmd/cli/cli.go index 9c78909..6bb7e9b 100644 --- a/cmd/cli/cli.go +++ b/cmd/cli/cli.go @@ -127,11 +127,11 @@ func initCLI() { rootCmd.SetHelpCommand(&cobra.Command{Hidden: true}) rootCmd.CompletionOptions.HiddenDefaultCmd = true - InitRunCmd() - InitServiceCmd() - InitClientsCmd() - InitUpgradeCmd() - InitLogCmd() + InitRunCmd(rootCmd) + InitServiceCmd(rootCmd) + InitClientsCmd(rootCmd) + InitUpgradeCmd(rootCmd) + InitLogCmd(rootCmd) } // isMobile reports whether the current OS is a mobile platform. diff --git a/cmd/cli/commands_clients.go b/cmd/cli/commands_clients.go index e14db15..30effa1 100644 --- a/cmd/cli/commands_clients.go +++ b/cmd/cli/commands_clients.go @@ -109,7 +109,7 @@ func (cc *ClientsCommand) ListClients(cmd *cobra.Command, args []string) error { } // InitClientsCmd creates the clients command with proper logic -func InitClientsCmd() *cobra.Command { +func InitClientsCmd(rootCmd *cobra.Command) *cobra.Command { listClientsCmd := &cobra.Command{ Use: "list", Short: "List clients that ctrld discovered", diff --git a/cmd/cli/commands_interfaces.go b/cmd/cli/commands_interfaces.go index 62e4f8a..508ae5f 100644 --- a/cmd/cli/commands_interfaces.go +++ b/cmd/cli/commands_interfaces.go @@ -56,7 +56,7 @@ func (ic *InterfacesCommand) ListInterfaces(cmd *cobra.Command, args []string) e } // InitInterfacesCmd creates the interfaces command with proper logic -func InitInterfacesCmd() *cobra.Command { +func InitInterfacesCmd(_ *cobra.Command) *cobra.Command { listInterfacesCmd := &cobra.Command{ Use: "list", Short: "List network interfaces", diff --git a/cmd/cli/commands_log.go b/cmd/cli/commands_log.go index 7bf6fed..089a192 100644 --- a/cmd/cli/commands_log.go +++ b/cmd/cli/commands_log.go @@ -127,7 +127,7 @@ func (lc *LogCommand) ViewLogs(cmd *cobra.Command, args []string) error { } // InitLogCmd creates the log command with proper logic -func InitLogCmd() *cobra.Command { +func InitLogCmd(rootCmd *cobra.Command) *cobra.Command { lc, err := NewLogCommand() if err != nil { panic(fmt.Sprintf("failed to create log command: %v", err)) diff --git a/cmd/cli/commands_run.go b/cmd/cli/commands_run.go index eb4b04e..abb74bb 100644 --- a/cmd/cli/commands_run.go +++ b/cmd/cli/commands_run.go @@ -22,7 +22,7 @@ func (rc *RunCommand) Run(cmd *cobra.Command, args []string) { } // InitRunCmd creates the run command with proper logic -func InitRunCmd() *cobra.Command { +func InitRunCmd(rootCmd *cobra.Command) *cobra.Command { rc := NewRunCommand() runCmd := &cobra.Command{ diff --git a/cmd/cli/commands_service.go b/cmd/cli/commands_service.go index dd5378a..1e56a73 100644 --- a/cmd/cli/commands_service.go +++ b/cmd/cli/commands_service.go @@ -69,7 +69,7 @@ func (sc *ServiceCommand) createServiceConfig() *service.Config { } // InitServiceCmd creates the service command with proper logic and aliases -func InitServiceCmd() *cobra.Command { +func InitServiceCmd(rootCmd *cobra.Command) *cobra.Command { // Create service command handlers sc := NewServiceCommand() @@ -141,7 +141,7 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`, } // Interfaces command - use the existing InitInterfacesCmd function - interfacesCmd := InitInterfacesCmd() + interfacesCmd := InitInterfacesCmd(rootCmd) stopCmdAlias := &cobra.Command{ PreRun: func(cmd *cobra.Command, args []string) { diff --git a/cmd/cli/commands_upgrade.go b/cmd/cli/commands_upgrade.go index 6d73e7e..ada9166 100644 --- a/cmd/cli/commands_upgrade.go +++ b/cmd/cli/commands_upgrade.go @@ -168,7 +168,7 @@ func (uc *UpgradeCommand) Upgrade(cmd *cobra.Command, args []string) error { } // InitUpgradeCmd creates the upgrade command with proper logic -func InitUpgradeCmd() *cobra.Command { +func InitUpgradeCmd(rootCmd *cobra.Command) *cobra.Command { upgradeCmd := &cobra.Command{ Use: "upgrade", Short: "Upgrading ctrld to latest version",