fix: parse systemctl ExecStart path= format correctly
This commit is contained in:
parent
32a5913c4d
commit
2aca93f36b
@ -56,20 +56,36 @@ func (s *SystemCtlController) Status() (Status, error) {
|
||||
|
||||
func (s *SystemCtlController) Version() (string, error) {
|
||||
// 从 systemctl 获取 ExecStart 路径,再调用 --version
|
||||
// Output format: { path=/path/to/binary ; argv[]=... }
|
||||
binOut, err := exec.Command("systemctl", "show", "--property=ExecStart", "--value", s.serviceName).Output()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("get version failed: %w", err)
|
||||
}
|
||||
binPath := strings.Fields(strings.TrimSpace(string(binOut)))[0]
|
||||
out := strings.TrimSpace(string(binOut))
|
||||
// Parse path= from systemctl ExecStart format
|
||||
const prefix = "path="
|
||||
idx := strings.Index(out, prefix)
|
||||
if idx < 0 {
|
||||
return "", fmt.Errorf("get version failed: cannot parse ExecStart")
|
||||
}
|
||||
rest := out[idx+len(prefix):]
|
||||
end := strings.IndexByte(rest, ' ')
|
||||
if end < 0 {
|
||||
end = strings.IndexByte(rest, ';')
|
||||
}
|
||||
if end < 0 {
|
||||
end = len(rest)
|
||||
}
|
||||
binPath := rest[:end]
|
||||
if binPath == "" {
|
||||
return "", fmt.Errorf("get version failed: empty ExecStart")
|
||||
}
|
||||
cmd := exec.Command(binPath, "--version")
|
||||
out, err := cmd.Output()
|
||||
out2, err := cmd.Output()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("get version failed: %w", err)
|
||||
}
|
||||
return strings.TrimSpace(string(out)), nil
|
||||
return strings.TrimSpace(string(out2)), nil
|
||||
}
|
||||
|
||||
func (s *SystemCtlController) Start(configName string) (Status, error) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user