From b1866cf76903d81b491fb668ba14f4b1322b2ca7 Mon Sep 17 00:00:00 2001 From: SteelPhase Date: Mon, 10 Sep 2018 18:42:44 -0400 Subject: [PATCH] Add ability for service to report the system managing the service (#147) * Add ability for service to report the system managing the service * Clarify that the output of SystemName should return the same value as Platform in most cases * Rename SystemName to Platform to match the rest of the package * be a little more clear in the Platform comment --- service.go | 4 ++++ service_darwin.go | 4 ++++ service_linux.go | 4 ++-- service_systemd_linux.go | 14 ++++++++++---- service_sysv_linux.go | 14 ++++++++++---- service_upstart_linux.go | 14 ++++++++++---- service_windows.go | 4 ++++ 7 files changed, 44 insertions(+), 14 deletions(-) diff --git a/service.go b/service.go index 5176e70..77fa5ae 100644 --- a/service.go +++ b/service.go @@ -347,6 +347,10 @@ type Service interface { // otherwise the name. String() string + // Platform displays the name of the system that manages the service. + // In most cases this will be the same as service.Platform(). + Platform() string + // Status returns the current service status. Status() (Status, error) } diff --git a/service_darwin.go b/service_darwin.go index c0a4673..9626bd3 100644 --- a/service_darwin.go +++ b/service_darwin.go @@ -77,6 +77,10 @@ func (s *darwinLaunchdService) String() string { return s.Name } +func (s *darwinLaunchdService) Platform() string { + return version +} + func (s *darwinLaunchdService) getHomeDir() (string, error) { u, err := user.Current() if err == nil { diff --git a/service_linux.go b/service_linux.go index 64158d8..18fe475 100644 --- a/service_linux.go +++ b/service_linux.go @@ -13,7 +13,7 @@ type linuxSystemService struct { name string detect func() bool interactive func() bool - new func(i Interface, c *Config) (Service, error) + new func(i Interface, platform string, c *Config) (Service, error) } func (sc linuxSystemService) String() string { @@ -26,7 +26,7 @@ func (sc linuxSystemService) Interactive() bool { return sc.interactive() } func (sc linuxSystemService) New(i Interface, c *Config) (Service, error) { - return sc.new(i, c) + return sc.new(i, sc.String(), c) } func init() { diff --git a/service_systemd_linux.go b/service_systemd_linux.go index 9016be2..4b57dca 100644 --- a/service_systemd_linux.go +++ b/service_systemd_linux.go @@ -24,14 +24,16 @@ func isSystemd() bool { } type systemd struct { - i Interface + i Interface + platform string *Config } -func newSystemdService(i Interface, c *Config) (Service, error) { +func newSystemdService(i Interface, platform string, c *Config) (Service, error) { s := &systemd{ - i: i, - Config: c, + i: i, + platform: platform, + Config: c, } return s, nil @@ -44,6 +46,10 @@ func (s *systemd) String() string { return s.Name } +func (s *systemd) Platform() string { + return s.platform +} + // Systemd services should be supported, but are not currently. var errNoUserServiceSystemd = errors.New("User services are not supported on systemd.") diff --git a/service_sysv_linux.go b/service_sysv_linux.go index 27da15d..f6eebb0 100644 --- a/service_sysv_linux.go +++ b/service_sysv_linux.go @@ -16,14 +16,16 @@ import ( ) type sysv struct { - i Interface + i Interface + platform string *Config } -func newSystemVService(i Interface, c *Config) (Service, error) { +func newSystemVService(i Interface, platform string, c *Config) (Service, error) { s := &sysv{ - i: i, - Config: c, + i: i, + platform: platform, + Config: c, } return s, nil @@ -36,6 +38,10 @@ func (s *sysv) String() string { return s.Name } +func (s *sysv) Platform() string { + return s.platform +} + var errNoUserServiceSystemV = errors.New("User services are not supported on SystemV.") func (s *sysv) configPath() (cp string, err error) { diff --git a/service_upstart_linux.go b/service_upstart_linux.go index bf79ded..660357f 100644 --- a/service_upstart_linux.go +++ b/service_upstart_linux.go @@ -31,14 +31,16 @@ func isUpstart() bool { } type upstart struct { - i Interface + i Interface + platform string *Config } -func newUpstartService(i Interface, c *Config) (Service, error) { +func newUpstartService(i Interface, platform string, c *Config) (Service, error) { s := &upstart{ - i: i, - Config: c, + i: i, + platform: platform, + Config: c, } return s, nil @@ -51,6 +53,10 @@ func (s *upstart) String() string { return s.Name } +func (s *upstart) Platform() string { + return s.platform +} + // Upstart has some support for user services in graphical sessions. // Due to the mix of actual support for user services over versions, just don't bother. // Upstart will be replaced by systemd in most cases anyway. diff --git a/service_windows.go b/service_windows.go index 8d87032..09ded23 100644 --- a/service_windows.go +++ b/service_windows.go @@ -145,6 +145,10 @@ func (ws *windowsService) String() string { return ws.Name } +func (ws *windowsService) Platform() string { + return version +} + func (ws *windowsService) setError(err error) { ws.errSync.Lock() defer ws.errSync.Unlock()