mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-08-10 13:20:33 +02:00
refactor: improve ServiceManager initialization with cleaner API
- Split initializeServiceManager into two methods: * initializeServiceManager(): Simple method using default configuration * initializeServiceManagerWithServiceConfig(): Advanced method for custom config - Simplify NewServiceCommand() to return *ServiceCommand without error - Update all service command methods to use appropriate initialization: * Start: Uses initializeServiceManagerWithServiceConfig() for custom args * Stop/Restart/Reload/Status/Uninstall: Use simple initializeServiceManager() - Remove direct access to sc.serviceManager.svc/prog in favor of lazy initialization - Improve separation of concerns and reduce code duplication
This commit is contained in:
+20
-11
@@ -25,16 +25,28 @@ type ServiceCommand struct {
|
|||||||
serviceManager *ServiceManager
|
serviceManager *ServiceManager
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewServiceCommand creates a new service command handler
|
// initializeServiceManager creates a service manager with default configuration
|
||||||
func NewServiceCommand() (*ServiceCommand, error) {
|
func (sc *ServiceCommand) initializeServiceManager() (service.Service, *prog, error) {
|
||||||
sm, err := NewServiceManager()
|
svcConfig := sc.createServiceConfig()
|
||||||
|
return sc.initializeServiceManagerWithServiceConfig(svcConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
// initializeServiceManagerWithServiceConfig creates a service manager with the given configuration
|
||||||
|
func (sc *ServiceCommand) initializeServiceManagerWithServiceConfig(svcConfig *service.Config) (service.Service, *prog, error) {
|
||||||
|
p := &prog{}
|
||||||
|
|
||||||
|
s, err := newService(p, svcConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, fmt.Errorf("failed to create service: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ServiceCommand{
|
sc.serviceManager = &ServiceManager{prog: p, svc: s}
|
||||||
serviceManager: sm,
|
return s, p, nil
|
||||||
}, nil
|
}
|
||||||
|
|
||||||
|
// NewServiceCommand creates a new service command handler
|
||||||
|
func NewServiceCommand() *ServiceCommand {
|
||||||
|
return &ServiceCommand{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// createServiceConfig creates a properly initialized service configuration
|
// createServiceConfig creates a properly initialized service configuration
|
||||||
@@ -50,10 +62,7 @@ func (sc *ServiceCommand) createServiceConfig() *service.Config {
|
|||||||
// InitServiceCmd creates the service command with proper logic and aliases
|
// InitServiceCmd creates the service command with proper logic and aliases
|
||||||
func InitServiceCmd() *cobra.Command {
|
func InitServiceCmd() *cobra.Command {
|
||||||
// Create service command handlers
|
// Create service command handlers
|
||||||
sc, err := NewServiceCommand()
|
sc := NewServiceCommand()
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("failed to create service command: %v", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
startCmd, startCmdAlias := createStartCommands(sc)
|
startCmd, startCmdAlias := createStartCommands(sc)
|
||||||
rootCmd.AddCommand(startCmdAlias)
|
rootCmd.AddCommand(startCmdAlias)
|
||||||
|
|||||||
@@ -12,7 +12,11 @@ import (
|
|||||||
|
|
||||||
// Reload implements the logic from cmdReload.Run
|
// Reload implements the logic from cmdReload.Run
|
||||||
func (sc *ServiceCommand) Reload(cmd *cobra.Command, args []string) error {
|
func (sc *ServiceCommand) Reload(cmd *cobra.Command, args []string) error {
|
||||||
status, err := sc.serviceManager.svc.Status()
|
s, _, err := sc.initializeServiceManager()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
status, err := s.Status()
|
||||||
if errors.Is(err, service.ErrNotInstalled) {
|
if errors.Is(err, service.ErrNotInstalled) {
|
||||||
mainLog.Load().Warn().Msg("service not installed")
|
mainLog.Load().Warn().Msg("service not installed")
|
||||||
return nil
|
return nil
|
||||||
@@ -37,7 +41,7 @@ func (sc *ServiceCommand) Reload(cmd *cobra.Command, args []string) error {
|
|||||||
case http.StatusCreated:
|
case http.StatusCreated:
|
||||||
mainLog.Load().Warn().Msg("Service was reloaded, but new config requires service restart.")
|
mainLog.Load().Warn().Msg("Service was reloaded, but new config requires service restart.")
|
||||||
mainLog.Load().Warn().Msg("Restarting service")
|
mainLog.Load().Warn().Msg("Restarting service")
|
||||||
if _, err := sc.serviceManager.svc.Status(); errors.Is(err, service.ErrNotInstalled) {
|
if _, err := s.Status(); errors.Is(err, service.ErrNotInstalled) {
|
||||||
mainLog.Load().Warn().Msg("Service not installed")
|
mainLog.Load().Warn().Msg("Service not installed")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,13 +9,16 @@ import (
|
|||||||
|
|
||||||
// Restart implements the logic from cmdRestart.Run
|
// Restart implements the logic from cmdRestart.Run
|
||||||
func (sc *ServiceCommand) Restart(cmd *cobra.Command, args []string) error {
|
func (sc *ServiceCommand) Restart(cmd *cobra.Command, args []string) error {
|
||||||
s := sc.serviceManager.svc
|
|
||||||
p := sc.serviceManager.prog
|
|
||||||
readConfig(false)
|
readConfig(false)
|
||||||
v.Unmarshal(&cfg)
|
v.Unmarshal(&cfg)
|
||||||
cdUID = curCdUID()
|
cdUID = curCdUID()
|
||||||
cdMode := cdUID != ""
|
cdMode := cdUID != ""
|
||||||
|
|
||||||
|
s, p, err := sc.initializeServiceManager()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
p.cfg = &cfg
|
p.cfg = &cfg
|
||||||
if iface == "" {
|
if iface == "" {
|
||||||
iface = "auto"
|
iface = "auto"
|
||||||
|
|||||||
@@ -21,8 +21,6 @@ import (
|
|||||||
|
|
||||||
// Start implements the logic from cmdStart.Run
|
// Start implements the logic from cmdStart.Run
|
||||||
func (sc *ServiceCommand) Start(cmd *cobra.Command, args []string) error {
|
func (sc *ServiceCommand) Start(cmd *cobra.Command, args []string) error {
|
||||||
s := sc.serviceManager.svc
|
|
||||||
p := sc.serviceManager.prog
|
|
||||||
checkStrFlagEmpty(cmd, cdUidFlagName)
|
checkStrFlagEmpty(cmd, cdUidFlagName)
|
||||||
checkStrFlagEmpty(cmd, cdOrgFlagName)
|
checkStrFlagEmpty(cmd, cdOrgFlagName)
|
||||||
validateCdAndNextDNSFlags()
|
validateCdAndNextDNSFlags()
|
||||||
@@ -36,6 +34,12 @@ func (sc *ServiceCommand) Start(cmd *cobra.Command, args []string) error {
|
|||||||
setDependencies(svcConfig)
|
setDependencies(svcConfig)
|
||||||
svcConfig.Arguments = append([]string{"run"}, osArgs...)
|
svcConfig.Arguments = append([]string{"run"}, osArgs...)
|
||||||
|
|
||||||
|
// Initialize service manager with proper configuration
|
||||||
|
s, p, err := sc.initializeServiceManagerWithServiceConfig(svcConfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
p.cfg = &cfg
|
p.cfg = &cfg
|
||||||
p.preRun()
|
p.preRun()
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,11 @@ import (
|
|||||||
|
|
||||||
// Status implements the logic from cmdStatus.Run
|
// Status implements the logic from cmdStatus.Run
|
||||||
func (sc *ServiceCommand) Status(cmd *cobra.Command, args []string) error {
|
func (sc *ServiceCommand) Status(cmd *cobra.Command, args []string) error {
|
||||||
status, err := sc.serviceManager.svc.Status()
|
s, _, err := sc.initializeServiceManager()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
status, err := s.Status()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mainLog.Load().Error().Msg(err.Error())
|
mainLog.Load().Error().Msg(err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
@@ -10,10 +10,14 @@ import (
|
|||||||
|
|
||||||
// Stop implements the logic from cmdStop.Run
|
// Stop implements the logic from cmdStop.Run
|
||||||
func (sc *ServiceCommand) Stop(cmd *cobra.Command, args []string) error {
|
func (sc *ServiceCommand) Stop(cmd *cobra.Command, args []string) error {
|
||||||
s := sc.serviceManager.svc
|
|
||||||
p := sc.serviceManager.prog
|
|
||||||
readConfig(false)
|
readConfig(false)
|
||||||
v.Unmarshal(&cfg)
|
v.Unmarshal(&cfg)
|
||||||
|
|
||||||
|
s, p, err := sc.initializeServiceManager()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
p.cfg = &cfg
|
p.cfg = &cfg
|
||||||
p.preRun()
|
p.preRun()
|
||||||
if ir := runningIface(s); ir != nil {
|
if ir := runningIface(s); ir != nil {
|
||||||
|
|||||||
@@ -12,10 +12,14 @@ import (
|
|||||||
|
|
||||||
// Uninstall implements the logic from cmdUninstall.Run
|
// Uninstall implements the logic from cmdUninstall.Run
|
||||||
func (sc *ServiceCommand) Uninstall(cmd *cobra.Command, args []string) error {
|
func (sc *ServiceCommand) Uninstall(cmd *cobra.Command, args []string) error {
|
||||||
s := sc.serviceManager.svc
|
|
||||||
p := sc.serviceManager.prog
|
|
||||||
readConfig(false)
|
readConfig(false)
|
||||||
v.Unmarshal(&cfg)
|
v.Unmarshal(&cfg)
|
||||||
|
|
||||||
|
s, p, err := sc.initializeServiceManager()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
p.cfg = &cfg
|
p.cfg = &cfg
|
||||||
if iface == "" {
|
if iface == "" {
|
||||||
iface = "auto"
|
iface = "auto"
|
||||||
|
|||||||
Reference in New Issue
Block a user