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
This commit is contained in:
SteelPhase
2018-09-10 18:42:44 -04:00
committed by Daniel Theophanes
parent 2b860c2dd7
commit b1866cf769
7 changed files with 44 additions and 14 deletions
+4
View File
@@ -347,6 +347,10 @@ type Service interface {
// otherwise the name. // otherwise the name.
String() string 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 returns the current service status.
Status() (Status, error) Status() (Status, error)
} }
+4
View File
@@ -77,6 +77,10 @@ func (s *darwinLaunchdService) String() string {
return s.Name return s.Name
} }
func (s *darwinLaunchdService) Platform() string {
return version
}
func (s *darwinLaunchdService) getHomeDir() (string, error) { func (s *darwinLaunchdService) getHomeDir() (string, error) {
u, err := user.Current() u, err := user.Current()
if err == nil { if err == nil {
+2 -2
View File
@@ -13,7 +13,7 @@ type linuxSystemService struct {
name string name string
detect func() bool detect func() bool
interactive 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 { func (sc linuxSystemService) String() string {
@@ -26,7 +26,7 @@ func (sc linuxSystemService) Interactive() bool {
return sc.interactive() return sc.interactive()
} }
func (sc linuxSystemService) New(i Interface, c *Config) (Service, error) { func (sc linuxSystemService) New(i Interface, c *Config) (Service, error) {
return sc.new(i, c) return sc.new(i, sc.String(), c)
} }
func init() { func init() {
+10 -4
View File
@@ -24,14 +24,16 @@ func isSystemd() bool {
} }
type systemd struct { type systemd struct {
i Interface i Interface
platform string
*Config *Config
} }
func newSystemdService(i Interface, c *Config) (Service, error) { func newSystemdService(i Interface, platform string, c *Config) (Service, error) {
s := &systemd{ s := &systemd{
i: i, i: i,
Config: c, platform: platform,
Config: c,
} }
return s, nil return s, nil
@@ -44,6 +46,10 @@ func (s *systemd) String() string {
return s.Name return s.Name
} }
func (s *systemd) Platform() string {
return s.platform
}
// Systemd services should be supported, but are not currently. // Systemd services should be supported, but are not currently.
var errNoUserServiceSystemd = errors.New("User services are not supported on systemd.") var errNoUserServiceSystemd = errors.New("User services are not supported on systemd.")
+10 -4
View File
@@ -16,14 +16,16 @@ import (
) )
type sysv struct { type sysv struct {
i Interface i Interface
platform string
*Config *Config
} }
func newSystemVService(i Interface, c *Config) (Service, error) { func newSystemVService(i Interface, platform string, c *Config) (Service, error) {
s := &sysv{ s := &sysv{
i: i, i: i,
Config: c, platform: platform,
Config: c,
} }
return s, nil return s, nil
@@ -36,6 +38,10 @@ func (s *sysv) String() string {
return s.Name return s.Name
} }
func (s *sysv) Platform() string {
return s.platform
}
var errNoUserServiceSystemV = errors.New("User services are not supported on SystemV.") var errNoUserServiceSystemV = errors.New("User services are not supported on SystemV.")
func (s *sysv) configPath() (cp string, err error) { func (s *sysv) configPath() (cp string, err error) {
+10 -4
View File
@@ -31,14 +31,16 @@ func isUpstart() bool {
} }
type upstart struct { type upstart struct {
i Interface i Interface
platform string
*Config *Config
} }
func newUpstartService(i Interface, c *Config) (Service, error) { func newUpstartService(i Interface, platform string, c *Config) (Service, error) {
s := &upstart{ s := &upstart{
i: i, i: i,
Config: c, platform: platform,
Config: c,
} }
return s, nil return s, nil
@@ -51,6 +53,10 @@ func (s *upstart) String() string {
return s.Name return s.Name
} }
func (s *upstart) Platform() string {
return s.platform
}
// Upstart has some support for user services in graphical sessions. // 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. // 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. // Upstart will be replaced by systemd in most cases anyway.
+4
View File
@@ -145,6 +145,10 @@ func (ws *windowsService) String() string {
return ws.Name return ws.Name
} }
func (ws *windowsService) Platform() string {
return version
}
func (ws *windowsService) setError(err error) { func (ws *windowsService) setError(err error) {
ws.errSync.Lock() ws.errSync.Lock()
defer ws.errSync.Unlock() defer ws.errSync.Unlock()