| 1234567891011121314151617181920212223242526272829303132 |
- package systemaction
- import (
- "fmt"
- "os/exec"
- "strings"
- )
- type Service struct{}
- func New() *Service { return &Service{} }
- func (s *Service) Reboot() error {
- return runCommand("systemctl", "reboot")
- }
- func (s *Service) Shutdown() error {
- return runCommand("systemctl", "poweroff")
- }
- func runCommand(name string, args ...string) error {
- cmd := exec.Command(name, args...)
- output, err := cmd.CombinedOutput()
- if err != nil {
- trimmed := strings.TrimSpace(string(output))
- if trimmed == "" {
- return fmt.Errorf("%s failed", strings.Join(append([]string{name}, args...), " "))
- }
- return fmt.Errorf("%s failed: %s", strings.Join(append([]string{name}, args...), " "), trimmed)
- }
- return nil
- }
|