From 17f5541e8126d378a967d5eac7d60d6828f12db7 Mon Sep 17 00:00:00 2001 From: Daniel Theophanes Date: Sat, 14 Mar 2015 08:14:25 -0700 Subject: [PATCH] service: return better error messages from executed commands. --- example/logging/main.go | 6 +++--- example/simple/main.go | 6 +++--- service_darwin.go | 7 ++----- service_systemd_linux.go | 14 ++++++++------ service_sysv_linux.go | 5 ++--- service_unix.go | 10 ++++++++++ service_upstart_linux.go | 5 ++--- 7 files changed, 30 insertions(+), 23 deletions(-) diff --git a/example/logging/main.go b/example/logging/main.go index f8c762a..a9838a7 100644 --- a/example/logging/main.go +++ b/example/logging/main.go @@ -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{} diff --git a/example/simple/main.go b/example/simple/main.go index cc4866b..76d88f1 100644 --- a/example/simple/main.go +++ b/example/simple/main.go @@ -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{} diff --git a/service_darwin.go b/service_darwin.go index 900327e..18b1eb3 100644 --- a/service_darwin.go +++ b/service_darwin.go @@ -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() diff --git a/service_systemd_linux.go b/service_systemd_linux.go index 557c144..7616730 100644 --- a/service_systemd_linux.go +++ b/service_systemd_linux.go @@ -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 { diff --git a/service_sysv_linux.go b/service_sysv_linux.go index 83027cb..b08244e 100644 --- a/service_sysv_linux.go +++ b/service_sysv_linux.go @@ -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 { diff --git a/service_unix.go b/service_unix.go index 1af0cc8..19faf91 100644 --- a/service_unix.go +++ b/service_unix.go @@ -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 +} diff --git a/service_upstart_linux.go b/service_upstart_linux.go index 09952ea..0e922e5 100644 --- a/service_upstart_linux.go +++ b/service_upstart_linux.go @@ -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 {