//go:build windows package service import ( "os/exec" "strings" "mfd-monitor-agent/internal/config" ) // windowsManager manages the agent through the Windows Service Control Manager. type windowsManager struct{} // newManager returns the Windows service manager. func newManager() Manager { return windowsManager{} } // Install registers the agent executable as an auto-start Windows Service. func (windowsManager) Install(executable string, cfg config.Config, opts InstallOptions) error { args := []string{`"` + executable + `"`, "run"} if opts.ConfigPath != "" { args = append(args, "-config", `"`+opts.ConfigPath+`"`) } return exec.Command("sc.exe", "create", cfg.Service.Name, "binPath=", strings.Join(args, " "), "DisplayName=", cfg.Service.DisplayName, "start=", "auto").Run() } // Uninstall stops and removes the configured Windows Service. func (windowsManager) Uninstall(cfg config.Config) error { _ = exec.Command("sc.exe", "stop", cfg.Service.Name).Run() return exec.Command("sc.exe", "delete", cfg.Service.Name).Run() } // Start asks the Windows Service Control Manager to start the service. func (windowsManager) Start(cfg config.Config) error { return exec.Command("sc.exe", "start", cfg.Service.Name).Run() } // Stop asks the Windows Service Control Manager to stop the service. func (windowsManager) Stop(cfg config.Config) error { return exec.Command("sc.exe", "stop", cfg.Service.Name).Run() }