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
+3 -20
View File
@@ -22,19 +22,11 @@ const (
// UpgradeCommand handles upgrade-related operations
type UpgradeCommand struct {
serviceManager *ServiceManager
}
// NewUpgradeCommand creates a new upgrade command handler
func NewUpgradeCommand() (*UpgradeCommand, error) {
sm, err := NewServiceManager()
if err != nil {
return nil, err
}
return &UpgradeCommand{
serviceManager: sm,
}, nil
return &UpgradeCommand{}, nil
}
// Upgrade performs the upgrade operation
@@ -53,19 +45,10 @@ func (uc *UpgradeCommand) Upgrade(cmd *cobra.Command, args []string) error {
mainLog.Load().Fatal().Err(err).Msg("failed to get current ctrld binary path")
}
// Create service config with executable path
sc := &service.Config{
Name: ctrldServiceName,
DisplayName: "Control-D Helper Service",
Description: "A highly configurable, multi-protocol DNS forwarding proxy",
Option: service.KeyValue{},
Executable: bin,
}
readConfig(false)
v.Unmarshal(&cfg)
p := &prog{}
s, err := newService(p, sc)
svcCmd := NewServiceCommand()
s, p, err := svcCmd.initializeServiceManager()
if err != nil {
mainLog.Load().Error().Msg(err.Error())
return nil