service: return better error messages from executed commands.

This commit is contained in:
Daniel Theophanes
2015-03-14 08:14:25 -07:00
parent 32a574a9fb
commit 17f5541e81
7 changed files with 30 additions and 23 deletions

View File

@@ -65,9 +65,9 @@ func main() {
flag.Parse()
svcConfig := &service.Config{
Name: "GoServiceTest",
DisplayName: "Go Service Test",
Description: "This is a test Go service.",
Name: "GoServiceExampleLogging",
DisplayName: "Go Service Example for Logging",
Description: "This is an example Go service that outputs log messages.",
}
prg := &program{}

View File

@@ -30,9 +30,9 @@ func (p *program) Stop(s service.Service) error {
func main() {
svcConfig := &service.Config{
Name: "GoServiceTest",
DisplayName: "Go Service Test",
Description: "This is a test Go service.",
Name: "GoServiceExampleSimple",
DisplayName: "Go Service Example",
Description: "This is an example Go service.",
}
prg := &program{}

View File

@@ -7,7 +7,6 @@ package service
import (
"fmt"
"os"
"os/exec"
"os/signal"
"os/user"
"syscall"
@@ -143,16 +142,14 @@ func (s *darwinLaunchdService) Start() error {
if err != nil {
return err
}
cmd := exec.Command("launchctl", "load", confPath)
return cmd.Run()
return run("launchctl", "load", confPath)
}
func (s *darwinLaunchdService) Stop() error {
confPath, err := s.getServiceFilePath()
if err != nil {
return err
}
cmd := exec.Command("launchctl", "unload", confPath)
return cmd.Run()
return run("launchctl", "unload", confPath)
}
func (s *darwinLaunchdService) Restart() error {
err := s.Stop()

View File

@@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"syscall"
"text/template"
@@ -94,15 +93,18 @@ func (s *systemd) Install() error {
return err
}
err = exec.Command("systemctl", "enable", s.Name+".service").Run()
err = run("systemctl", "enable", s.Name+".service")
if err != nil {
return err
}
return exec.Command("systemctl", "daemon-reload").Run()
return run("systemctl", "daemon-reload")
}
func (s *systemd) Uninstall() error {
exec.Command("systemctl", "disable", s.Name+".service").Run()
err := run("systemctl", "disable", s.Name+".service")
if err != nil {
return err
}
cp, err := s.configPath()
if err != nil {
return err
@@ -139,11 +141,11 @@ func (s *systemd) Run() (err error) {
}
func (s *systemd) Start() error {
return exec.Command("systemctl", "start", s.Name+".service").Run()
return run("systemctl", "start", s.Name+".service")
}
func (s *systemd) Stop() error {
return exec.Command("systemctl", "stop", s.Name+".service").Run()
return run("systemctl", "stop", s.Name+".service")
}
func (s *systemd) Restart() error {

View File

@@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"syscall"
"text/template"
@@ -140,11 +139,11 @@ func (s *sysv) Run() (err error) {
}
func (s *sysv) Start() error {
return exec.Command("service", s.Name, "start").Run()
return run("service", s.Name, "start")
}
func (s *sysv) Stop() error {
return exec.Command("service", s.Name, "stop").Run()
return run("service", s.Name, "stop")
}
func (s *sysv) Restart() error {

View File

@@ -9,6 +9,7 @@ package service
import (
"fmt"
"log/syslog"
"os/exec"
)
func newSysLogger(name string, errs chan<- error) (Logger, error) {
@@ -49,3 +50,12 @@ func (s sysLogger) Warningf(format string, a ...interface{}) error {
func (s sysLogger) Infof(format string, a ...interface{}) error {
return s.send(s.Writer.Info(fmt.Sprintf(format, a...)))
}
func run(command string, arguments ...string) error {
cmd := exec.Command(command, arguments...)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%q failed: %v, %s", command, err, out)
}
return nil
}

View File

@@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"syscall"
"text/template"
@@ -131,11 +130,11 @@ func (s *upstart) Run() (err error) {
}
func (s *upstart) Start() error {
return exec.Command("initctl", "start", s.Name).Run()
return run("initctl", "start", s.Name)
}
func (s *upstart) Stop() error {
return exec.Command("initctl", "stop", s.Name).Run()
return run("initctl", "stop", s.Name)
}
func (s *upstart) Restart() error {