From 1b4c111ff3a1df98aef9966f824f9b90af5beff7 Mon Sep 17 00:00:00 2001 From: Son Roy Almerol Date: Fri, 29 Nov 2024 15:23:40 -0500 Subject: [PATCH] modify install flow for windows msi --- build/package/windows/wix.json | 10 +----- cmd/windows_agent/main.go | 63 ++++++++-------------------------- 2 files changed, 16 insertions(+), 57 deletions(-) diff --git a/build/package/windows/wix.json b/build/package/windows/wix.json index 69b2838..fc413be 100644 --- a/build/package/windows/wix.json +++ b/build/package/windows/wix.json @@ -37,17 +37,9 @@ "command": "[INSTALLDIR]pbs-plus-agent.exe install", "when": "install" }, - { - "command": "[INSTALLDIR]pbs-plus-agent.exe start", - "when": "install" - }, - { - "command": "[INSTALLDIR]pbs-plus-agent.exe stop", - "when": "uninstall" - }, { "command": "[INSTALLDIR]pbs-plus-agent.exe uninstall", "when": "uninstall" } ] -} \ No newline at end of file +} diff --git a/cmd/windows_agent/main.go b/cmd/windows_agent/main.go index 66e8626..1463fb3 100644 --- a/cmd/windows_agent/main.go +++ b/cmd/windows_agent/main.go @@ -6,6 +6,7 @@ package main import ( "fmt" "os" + "time" "github.com/kardianos/service" "github.com/sonroyaalmerol/pbs-plus/internal/syslog" @@ -53,60 +54,26 @@ func main() { if len(os.Args) > 1 { cmd := os.Args[1] - switch cmd { - case "install": - status, err := s.Status() - if err == nil { - switch status { - case service.StatusStopped: - _ = s.Uninstall() - case service.StatusRunning: - _ = s.Stop() - _ = s.Uninstall() - case service.StatusUnknown: - } - } - + if cmd == "install" || cmd == "uninstall" { for _, drive := range getLocalDrives() { _ = registry.DeleteKey(registry.LOCAL_MACHINE, fmt.Sprintf(`Software\PBSPlus\Config\SFTP-%s`, drive.Letter)) } + } - err = s.Install() - if err != nil { - logger.Errorf("Failed to install service: %s", err) - } else { - logger.Info("Service installed") - } - return - case "uninstall": - err = s.Uninstall() - if err != nil { - logger.Errorf("Failed to uninstall service: %s", err) - } else { - logger.Info("Service uninstalled") - } - return - case "start": - err = s.Start() - if err != nil { - logger.Errorf("Failed to start service: %s", err) - } else { - logger.Info("Service started") - } - return - case "stop": - err = s.Stop() - if err != nil { - logger.Errorf("Failed to stop service: %s", err) - } else { - logger.Info("Service stopped") - } - return - default: - logger.Errorf("Unknown command: %s", cmd) - logger.Info("Available commands: install, uninstall, start, stop") + err = service.Control(s, cmd) + if err != nil { + logger.Errorf("Failed to %s service: %s", cmd, err) return } + + if cmd == "install" { + go func() { + <-time.After(10 * time.Second) + _ = s.Start() + }() + } + + return } if !service.Interactive() {