// Package service manages the galahad2lcd systemd unit. package service import ( "context" "fmt" "os/exec" ) // Service represents the installed systemd unit. type Service struct { name string // runCmd is the command constructor; overridable for tests. runCmd func(ctx context.Context, name string, arg ...string) *exec.Cmd } // New returns a Service wrapper for the given unit name. func New(name string) *Service { return &Service{name: name, runCmd: exec.CommandContext} } // Restart tells systemd to restart the service. It fails early when the // caller lacks privileges. func (s *Service) Restart(ctx context.Context) error { cmd := s.runCmd(ctx, "systemctl", "restart", s.name) out, err := cmd.CombinedOutput() if err != nil { if len(out) > 0 { return fmt.Errorf("systemctl restart %s: %w (%s)", s.name, err, string(out)) } return fmt.Errorf("systemctl restart %s: %w", s.name, err) } return nil }