Refactor bee CLI and LiveCD integration
This commit is contained in:
54
audit/internal/platform/services.go
Normal file
54
audit/internal/platform/services.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package platform
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *System) ListBeeServices() ([]string, error) {
|
||||
seen := map[string]bool{}
|
||||
var out []string
|
||||
for _, pattern := range []string{"/etc/systemd/system/bee-*.service", "/lib/systemd/system/bee-*.service"} {
|
||||
matches, err := filepath.Glob(pattern)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, match := range matches {
|
||||
name := strings.TrimSuffix(filepath.Base(match), ".service")
|
||||
if !seen[name] {
|
||||
seen[name] = true
|
||||
out = append(out, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *System) ServiceState(name string) string {
|
||||
raw, err := exec.Command("systemctl", "is-active", name).CombinedOutput()
|
||||
if err == nil {
|
||||
return strings.TrimSpace(string(raw))
|
||||
}
|
||||
raw, err = exec.Command("systemctl", "show", name, "--property=ActiveState", "--value").CombinedOutput()
|
||||
if err != nil {
|
||||
return "unknown"
|
||||
}
|
||||
state := strings.TrimSpace(string(raw))
|
||||
if state == "" {
|
||||
return "unknown"
|
||||
}
|
||||
return state
|
||||
}
|
||||
|
||||
func (s *System) ServiceDo(name string, action ServiceAction) (string, error) {
|
||||
raw, err := exec.Command("systemctl", string(action), name).CombinedOutput()
|
||||
return string(raw), err
|
||||
}
|
||||
|
||||
func (s *System) ServiceStatus(name string) (string, error) {
|
||||
raw, err := exec.Command("systemctl", "status", name, "--no-pager").CombinedOutput()
|
||||
return string(raw), err
|
||||
}
|
||||
Reference in New Issue
Block a user