From 7a0abbd49c4a5717e60cfcdf227ac3c2f0e41229 Mon Sep 17 00:00:00 2001 From: Daniel Theophanes Date: Wed, 29 Oct 2014 08:50:16 -0700 Subject: [PATCH] linux: change ifs to switches. --- service_linux.go | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/service_linux.go b/service_linux.go index 70ed30b..2871b38 100644 --- a/service_linux.go +++ b/service_linux.go @@ -19,11 +19,10 @@ const ( func getFlavor() initFlavor { flavor := initSystemV - if isUpstart() { - flavor = initUpstart - } if isSystemd() { flavor = initSystemd + } else if isUpstart() { + flavor = initUpstart } return flavor } @@ -82,12 +81,12 @@ func (f initFlavor) String() string { func (f initFlavor) ConfigPath(name string) string { switch f { + case initSystemd: + return "/etc/systemd/system/" + name + ".service" case initSystemV: return "/etc/init.d/" + name case initUpstart: return "/etc/init/" + name + ".conf" - case initSystemd: - return "/etc/systemd/system/" + name + ".service" default: return "" } @@ -96,12 +95,12 @@ func (f initFlavor) ConfigPath(name string) string { func (f initFlavor) GetTemplate() *template.Template { var templ string switch f { + case initSystemd: + templ = systemdScript case initSystemV: templ = systemVScript case initUpstart: templ = upstartScript - case initSystemd: - templ = systemdScript } return template.Must(template.New(f.String() + "Script").Parse(templ)) } @@ -191,23 +190,25 @@ func (s *linuxService) Run(onStart, onStop func() error) (err error) { } func (s *linuxService) Start() error { - if s.flavor == initUpstart { - return exec.Command("initctl", "start", s.name).Run() - } - if s.flavor == initSystemd { + switch s.flavor { + case initSystemd: return exec.Command("systemctl", "start", s.name+".service").Run() + case initUpstart: + return exec.Command("initctl", "start", s.name).Run() + default: + return exec.Command("service", s.name, "start").Run() } - return exec.Command("service", s.name, "start").Run() } func (s *linuxService) Stop() error { - if s.flavor == initUpstart { - return exec.Command("initctl", "stop", s.name).Run() - } - if s.flavor == initSystemd { + switch s.flavor { + case initSystemd: return exec.Command("systemctl", "stop", s.name+".service").Run() + case initUpstart: + return exec.Command("initctl", "stop", s.name).Run() + default: + return exec.Command("service", s.name, "stop").Run() } - return exec.Command("service", s.name, "stop").Run() } func (s *linuxService) Error(format string, a ...interface{}) error {