Files
ss/internal/service/status.go
2026-07-19 20:01:38 +08:00

51 lines
1.3 KiB
Go

package service
import (
"os/exec"
"runtime"
"strings"
)
// Status returns a human-readable status string for the named service.
// Direct port of CoreManager.get_service_status (core_manager.py:389-434).
func Status(name string) string {
switch runtime.GOOS {
case "windows":
out, err := exec.Command("sc", "query", name).CombinedOutput()
if err != nil {
return "Service not installed or not found"
}
for _, line := range strings.Split(string(out), "\n") {
if strings.Contains(line, "STATE") {
return strings.TrimSpace(line)
}
}
return "Service exists but status unknown"
case "linux":
out, err := exec.Command("systemctl", "is-active", name).CombinedOutput()
status := strings.TrimSpace(string(out))
if err != nil {
return "Service not installed or not found"
}
if status == "active" {
return "Service is running"
}
return "Service is " + status
case "darwin":
out, err := exec.Command("launchctl", "list").CombinedOutput()
if err != nil {
return "Error checking service status: " + err.Error()
}
output := string(out)
if strings.Contains(output, name) || strings.Contains(output, "com."+name) {
return "Service is loaded (check launchctl status for details)"
}
return "Service not installed or not loaded"
default:
return "Unsupported system: " + runtime.GOOS
}
}