From 42ea5f7fede65e921d3cde195f9a94172b17d4f2 Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Tue, 29 Jul 2025 15:24:30 +0700 Subject: [PATCH] refactor: move initRunCmd to dedicated commands_run.go file Create commands_run.go following the same modular pattern as other command files. Move initRunCmd logic to InitRunCmd function with consistent naming and complete functionality preservation. Update cli.go to use InitRunCmd() instead of initRunCmd() and clean up commands.go by removing the old function and unused imports. This completes the modular refactoring pattern where each command type has its own dedicated file with focused responsibility. --- cmd/cli/cli.go | 2 +- cmd/cli/commands.go | 38 +-------------------------- cmd/cli/commands_run.go | 58 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 38 deletions(-) create mode 100644 cmd/cli/commands_run.go diff --git a/cmd/cli/cli.go b/cmd/cli/cli.go index 51ccf9e..058e066 100644 --- a/cmd/cli/cli.go +++ b/cmd/cli/cli.go @@ -127,7 +127,7 @@ func initCLI() { rootCmd.SetHelpCommand(&cobra.Command{Hidden: true}) rootCmd.CompletionOptions.HiddenDefaultCmd = true - initRunCmd() + InitRunCmd() InitServiceCmd() InitClientsCmd() InitUpgradeCmd() diff --git a/cmd/cli/commands.go b/cmd/cli/commands.go index e681c07..9227e2b 100644 --- a/cmd/cli/commands.go +++ b/cmd/cli/commands.go @@ -6,8 +6,6 @@ import ( "github.com/kardianos/service" "github.com/spf13/cobra" - - "github.com/Control-D-Inc/ctrld" ) // dialSocketControlServerTimeout is the default timeout to wait when ping control server. @@ -54,41 +52,7 @@ func (sm *ServiceManager) Status() (service.Status, error) { } // initLogCmd is now implemented in commands_log.go as InitLogCmd - -func initRunCmd() *cobra.Command { - runCmd := &cobra.Command{ - Use: "run", - Short: "Run the DNS proxy server", - Args: cobra.NoArgs, - Run: func(cmd *cobra.Command, args []string) { - RunCobraCommand(cmd) - }, - } - runCmd.Flags().BoolVarP(&daemon, "daemon", "d", false, "Run as daemon") - runCmd.Flags().StringVarP(&configPath, "config", "c", "", "Path to config file") - runCmd.Flags().StringVarP(&configBase64, "base64_config", "", "", "Base64 encoded config") - runCmd.Flags().StringVarP(&listenAddress, "listen", "", "", "Listener address and port, in format: address:port") - runCmd.Flags().StringVarP(&primaryUpstream, "primary_upstream", "", "", "Primary upstream endpoint") - runCmd.Flags().StringVarP(&secondaryUpstream, "secondary_upstream", "", "", "Secondary upstream endpoint") - runCmd.Flags().StringSliceVarP(&domains, "domains", "", nil, "List of domain to apply in a split DNS policy") - runCmd.Flags().StringVarP(&logPath, "log", "", "", "Path to log file") - runCmd.Flags().IntVarP(&cacheSize, "cache_size", "", 0, "Enable cache with size items") - runCmd.Flags().StringVarP(&cdUID, cdUidFlagName, "", "", "Control D resolver uid") - runCmd.Flags().StringVarP(&cdOrg, cdOrgFlagName, "", "", "Control D provision token") - runCmd.Flags().StringVarP(&customHostname, customHostnameFlagName, "", "", "Custom hostname passed to ControlD API") - runCmd.Flags().BoolVarP(&cdDev, "dev", "", false, "Use Control D dev resolver/domain") - _ = runCmd.Flags().MarkHidden("dev") - runCmd.Flags().StringVarP(&homedir, "homedir", "", "", "") - _ = runCmd.Flags().MarkHidden("homedir") - runCmd.Flags().StringVarP(&iface, "iface", "", "", `Update DNS setting for iface, "auto" means the default interface gateway`) - _ = runCmd.Flags().MarkHidden("iface") - runCmd.Flags().StringVarP(&cdUpstreamProto, "proto", "", ctrld.ResolverTypeDOH, `Control D upstream type, either "doh" or "doh3"`) - - runCmd.FParseErrWhitelist = cobra.FParseErrWhitelist{UnknownFlags: true} - rootCmd.AddCommand(runCmd) - - return runCmd -} +// initRunCmd is now implemented in commands_run.go as InitRunCmd // filterEmptyStrings removes empty strings from a slice func filterEmptyStrings(slice []string) []string { diff --git a/cmd/cli/commands_run.go b/cmd/cli/commands_run.go new file mode 100644 index 0000000..eb4b04e --- /dev/null +++ b/cmd/cli/commands_run.go @@ -0,0 +1,58 @@ +package cli + +import ( + "github.com/spf13/cobra" + + "github.com/Control-D-Inc/ctrld" +) + +// RunCommand handles run-related operations +type RunCommand struct { + // Add any dependencies here if needed in the future +} + +// NewRunCommand creates a new run command handler +func NewRunCommand() *RunCommand { + return &RunCommand{} +} + +// Run implements the logic for the run command +func (rc *RunCommand) Run(cmd *cobra.Command, args []string) { + RunCobraCommand(cmd) +} + +// InitRunCmd creates the run command with proper logic +func InitRunCmd() *cobra.Command { + rc := NewRunCommand() + + runCmd := &cobra.Command{ + Use: "run", + Short: "Run the DNS proxy server", + Args: cobra.NoArgs, + Run: rc.Run, + } + runCmd.Flags().BoolVarP(&daemon, "daemon", "d", false, "Run as daemon") + runCmd.Flags().StringVarP(&configPath, "config", "c", "", "Path to config file") + runCmd.Flags().StringVarP(&configBase64, "base64_config", "", "", "Base64 encoded config") + runCmd.Flags().StringVarP(&listenAddress, "listen", "", "", "Listener address and port, in format: address:port") + runCmd.Flags().StringVarP(&primaryUpstream, "primary_upstream", "", "", "Primary upstream endpoint") + runCmd.Flags().StringVarP(&secondaryUpstream, "secondary_upstream", "", "", "Secondary upstream endpoint") + runCmd.Flags().StringSliceVarP(&domains, "domains", "", nil, "List of domain to apply in a split DNS policy") + runCmd.Flags().StringVarP(&logPath, "log", "", "", "Path to log file") + runCmd.Flags().IntVarP(&cacheSize, "cache_size", "", 0, "Enable cache with size items") + runCmd.Flags().StringVarP(&cdUID, cdUidFlagName, "", "", "Control D resolver uid") + runCmd.Flags().StringVarP(&cdOrg, cdOrgFlagName, "", "", "Control D provision token") + runCmd.Flags().StringVarP(&customHostname, customHostnameFlagName, "", "", "Custom hostname passed to ControlD API") + runCmd.Flags().BoolVarP(&cdDev, "dev", "", false, "Use Control D dev resolver/domain") + _ = runCmd.Flags().MarkHidden("dev") + runCmd.Flags().StringVarP(&homedir, "homedir", "", "", "") + _ = runCmd.Flags().MarkHidden("homedir") + runCmd.Flags().StringVarP(&iface, "iface", "", "", `Update DNS setting for iface, "auto" means the default interface gateway`) + _ = runCmd.Flags().MarkHidden("iface") + runCmd.Flags().StringVarP(&cdUpstreamProto, "proto", "", ctrld.ResolverTypeDOH, `Control D upstream type, either "doh" or "doh3"`) + + runCmd.FParseErrWhitelist = cobra.FParseErrWhitelist{UnknownFlags: true} + rootCmd.AddCommand(runCmd) + + return runCmd +}