mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-02-03 22:18:39 +00:00
- 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
48 lines
983 B
Go
48 lines
983 B
Go
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 {
|
|
readConfig(false)
|
|
v.Unmarshal(&cfg)
|
|
|
|
s, p, err := sc.initializeServiceManager()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
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
|
|
}
|