refactor: replace direct newService calls with ServiceCommand pattern

- Replace all direct newService() calls with ServiceCommand initialization
- Update command constructors to use ServiceCommand instead of ServiceManager
- Simplify LogCommand and UpgradeCommand structs by removing serviceManager field
- Remove unused global svcConfig variable from prog.go
- Improve consistency and centralize service creation logic

This change establishes a consistent pattern for service operations across
the codebase, making it easier to maintain and extend service-related
functionality.
This commit is contained in:
Cuong Manh Le
2025-07-30 16:49:35 +07:00
committed by Cuong Manh Le
parent 33dd720d80
commit a61cb1f5bf
6 changed files with 39 additions and 44 deletions
+7 -3
View File
@@ -241,7 +241,9 @@ func run(appCallback *AppCallback, stopCh chan struct{}) {
// We need to call s.Run() as soon as possible to response to the OS manager, so it
// can see ctrld is running and don't mark ctrld as failed service.
go func() {
s, err := newService(p, svcConfig)
svcCmd := NewServiceCommand()
svcConfig := svcCmd.createServiceConfig()
s, err := svcCmd.newService(p, svcConfig)
if err != nil {
p.Fatal().Err(err).Msg("failed create new service")
}
@@ -1636,7 +1638,8 @@ func exchangeContextWithTimeout(c *dns.Client, timeout time.Duration, msg *dns.M
// curCdUID returns the current ControlD UID used by running ctrld process.
func curCdUID() string {
if s, _ := newService(&prog{}, svcConfig); s != nil {
svcCmd := NewServiceCommand()
if s, _, _ := svcCmd.initializeServiceManager(); s != nil {
// Configure Windows service failure actions
if err := ConfigureWindowsServiceFailureActions(ctrldServiceName); err != nil {
mainLog.Load().Debug().Err(err).Msgf("failed to configure Windows service %s failure actions", ctrldServiceName)
@@ -1770,7 +1773,8 @@ func doValidateCdRemoteConfig(cdUID string, fatal bool) error {
// uninstallInvalidCdUID performs self-uninstallation because the ControlD device does not exist.
func uninstallInvalidCdUID(p *prog, logger *ctrld.Logger, doStop bool) bool {
s, err := newService(p, svcConfig)
svcCmd := NewServiceCommand()
s, _, err := svcCmd.initializeServiceManager()
if err != nil {
logger.Warn().Err(err).Msg("failed to create new service")
return false