将 macOS 托盘 LaunchAgent 的 KeepAlive 设置为 false,避免用户退出后被 launchd 立即拉起。 托盘退出菜单主动卸载当前 LaunchAgent,再结束托盘进程。 保留旧版本通过 launchctl bootout 清理服务的升级恢复路径。
49 lines
1.4 KiB
Go
49 lines
1.4 KiB
Go
//go:build windows
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
func serviceAction(action string) error {
|
|
name := envOr("XIANYU_SERVICE_NAME", "YdisksXianyuHelper")
|
|
if action != "start" && action != "stop" && action != "restart" {
|
|
return fmt.Errorf("未知服务操作: %s", action)
|
|
}
|
|
|
|
quotedName := strings.ReplaceAll(name, "'", "''")
|
|
script := fmt.Sprintf(`$ErrorActionPreference = 'Stop'
|
|
function Invoke-Sc([string] $verb) {
|
|
$process = Start-Process -FilePath "$env:SystemRoot\System32\sc.exe" -ArgumentList @($verb, '%s') -Verb RunAs -WindowStyle Hidden -Wait -PassThru
|
|
if ($null -eq $process) { exit 1 }
|
|
return $process.ExitCode
|
|
}
|
|
$exitCode = 0
|
|
switch ('%s') {
|
|
'start' { $exitCode = Invoke-Sc 'start' }
|
|
'stop' { $exitCode = Invoke-Sc 'stop' }
|
|
'restart' {
|
|
$stopCode = Invoke-Sc 'stop'
|
|
if ($stopCode -ne 0 -and $stopCode -ne 1062) { exit $stopCode }
|
|
$exitCode = Invoke-Sc 'start'
|
|
}
|
|
}
|
|
exit $exitCode`, quotedName, action)
|
|
|
|
cmd := exec.Command("powershell.exe", "-NoLogo", "-NoProfile", "-NonInteractive", "-WindowStyle", "Hidden", "-Command", script)
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
message := strings.TrimSpace(string(output))
|
|
if message == "" {
|
|
return fmt.Errorf("Windows 服务操作 %s 失败: %w", action, err)
|
|
}
|
|
return fmt.Errorf("Windows 服务操作 %s 失败: %s", action, message)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func quitTray() error { return nil }
|