refactor: split ServiceCommand methods into dedicated files

- Move ServiceCommand.Start to commands_service_start.go
- Move ServiceCommand.Stop to commands_service_stop.go
- Move ServiceCommand.Restart to commands_service_restart.go
- Move ServiceCommand.Reload to commands_service_reload.go
- Move ServiceCommand.Status to commands_service_status.go
- Move ServiceCommand.Uninstall to commands_service_uninstall.go
- Move createStartCommands to commands_service_start.go
- Clean up imports in commands_service.go
- Remove all method implementations from main service file

This refactoring improves code organization by:
- Separating concerns into focused files
- Making navigation easier for developers
- Reducing merge conflicts between different commands
- Following consistent modular patterns
- Reducing commands_service.go from ~650 lines to ~50 lines

Each method is now co-located with its related functionality,
making the codebase more maintainable and easier to understand.
This commit is contained in:
Cuong Manh Le
2026-04-30 19:19:18 +07:00
committed by Cuong Manh Le
parent 8cc5b71c69
commit d81042089b
7 changed files with 670 additions and 615 deletions
+43
View File
@@ -0,0 +1,43 @@
package cli
import (
"errors"
"os"
"github.com/kardianos/service"
"github.com/spf13/cobra"
)
// Stop implements the logic from cmdStop.Run
func (sc *ServiceCommand) Stop(cmd *cobra.Command, args []string) error {
s := sc.serviceManager.svc
p := sc.serviceManager.prog
readConfig(false)
v.Unmarshal(&cfg)
p.cfg = &cfg
p.preRun()
if ir := runningIface(s); ir != nil {
p.runningIface = ir.Name
p.requiredMultiNICsConfig = ir.All
}
initInteractiveLogging()
status, err := s.Status()
if errors.Is(err, service.ErrNotInstalled) {
mainLog.Load().Warn().Msg("service not installed")
return nil
}
if status == service.StatusStopped {
mainLog.Load().Warn().Msg("service is already stopped")
return nil
}
if err := checkDeactivationPin(s, nil); isCheckDeactivationPinErr(err) {
os.Exit(deactivationPinInvalidExitCode)
}
if doTasks([]task{{s.Stop, true, "Stop"}}) {
mainLog.Load().Notice().Msg("Service stopped")
}
return nil
}