Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added config path to Service interface #255

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion linux_test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ all: sysv systemd upstart openrc clean

# compile `go test` binary statically
test:
@CGO_ENABLED=0 go test -installsuffix netgo -a -c ..
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go test -installsuffix netgo -a -c ..
jasmingacic marked this conversation as resolved.
Show resolved Hide resolved

clean:
-rm service.test
Expand Down
5 changes: 5 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ type Service interface {
Status() (Status, error)
}

// ConfigInfoer is an optional interface which allows for certain information to be obtained.
type ConfigInfoer interface {
ConfigPath() (string, error)
}

// ControlAction list valid string texts to use in Control.
var ControlAction = [5]string{"start", "stop", "restart", "install", "uninstall"}

Expand Down
10 changes: 6 additions & 4 deletions service_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const version = "aix-ssrc"

type aixSystem struct{}

var _ ConfigInfoer = &aixService{}

func (aixSystem) String() string {
return version
}
Expand Down Expand Up @@ -114,7 +116,7 @@ func (s *aixService) template() *template.Template {
}
}

func (s *aixService) configPath() (cp string, err error) {
func (s *aixService) ConfigPath() (cp string, err error) {
cp = "/etc/rc.d/init.d/" + s.Config.Name
return
}
Expand All @@ -131,7 +133,7 @@ func (s *aixService) Install() error {
}

// write start script
confPath, err := s.configPath()
confPath, err := s.ConfigPath()
if err != nil {
return err
}
Expand Down Expand Up @@ -182,7 +184,7 @@ func (s *aixService) Uninstall() error {
return err
}

confPath, err := s.configPath()
confPath, err := s.ConfigPath()
if err != nil {
return err
}
Expand Down Expand Up @@ -211,7 +213,7 @@ func (s *aixService) Status() (Status, error) {
}
}

confPath, err := s.configPath()
confPath, err := s.ConfigPath()
if err != nil {
return StatusUnknown, err
}
Expand Down
6 changes: 6 additions & 0 deletions service_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ type darwinLaunchdService struct {
userService bool
}

var _ ConfigInfoer = &darwinLaunchdService{}

func (s *darwinLaunchdService) ConfigPath() (string, error) {
return s.getServiceFilePath()
}

func (s *darwinLaunchdService) String() string {
if len(s.DisplayName) > 0 {
return s.DisplayName
Expand Down
10 changes: 6 additions & 4 deletions service_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type freebsdService struct {
*Config
}

var _ ConfigInfoer = &freebsdService{}

func (s *freebsdService) String() string {
if len(s.DisplayName) > 0 {
return s.DisplayName
Expand Down Expand Up @@ -87,7 +89,7 @@ func (s *freebsdService) template() *template.Template {
}
}

func (s *freebsdService) configPath() (cp string, err error) {
func (s *freebsdService) ConfigPath() (cp string, err error) {
cp = "/usr/local/etc/rc.d/" + s.Config.Name
return
}
Expand All @@ -99,7 +101,7 @@ func (s *freebsdService) Install() error {
}

// write start script
confPath, err := s.configPath()
confPath, err := s.ConfigPath()
if err != nil {
return err
}
Expand Down Expand Up @@ -135,15 +137,15 @@ func (s *freebsdService) Install() error {
}

func (s *freebsdService) Uninstall() error {
cp, err := s.configPath()
cp, err := s.ConfigPath()
if err != nil {
return err
}
return os.Remove(cp)
}

func (s *freebsdService) Status() (Status, error) {
cp, err := s.configPath()
cp, err := s.ConfigPath()
if err != nil {
return StatusUnknown, err
}
Expand Down
5 changes: 5 additions & 0 deletions service_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type linuxSystemService struct {
new func(i Interface, platform string, c *Config) (Service, error)
}

var _ ConfigInfoer = &linuxSystemService{}

func (sc linuxSystemService) String() string {
return sc.name
}
Expand All @@ -33,6 +35,9 @@ func (sc linuxSystemService) Interactive() bool {
func (sc linuxSystemService) New(i Interface, c *Config) (Service, error) {
return sc.new(i, sc.String(), c)
}
func (sc linuxSystemService) ConfigPath() (string, error) {
return "", fmt.Errorf("not implemented")
}

func init() {
ChooseSystem(linuxSystemService{
Expand Down
8 changes: 5 additions & 3 deletions service_openrc_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type openrc struct {
*Config
}

var _ ConfigInfoer = &openrc{}

func (s *openrc) String() string {
if len(s.DisplayName) > 0 {
return s.DisplayName
Expand Down Expand Up @@ -77,7 +79,7 @@ func newOpenRCService(i Interface, platform string, c *Config) (Service, error)

var errNoUserServiceOpenRC = errors.New("user services are not supported on OpenRC")

func (s *openrc) configPath() (cp string, err error) {
func (s *openrc) ConfigPath() (cp string, err error) {
if s.Option.bool(optionUserService, optionUserServiceDefault) {
err = errNoUserServiceOpenRC
return
Expand All @@ -87,7 +89,7 @@ func (s *openrc) configPath() (cp string, err error) {
}

func (s *openrc) Install() error {
confPath, err := s.configPath()
confPath, err := s.ConfigPath()
if err != nil {
return err
}
Expand Down Expand Up @@ -129,7 +131,7 @@ func (s *openrc) Install() error {
}

func (s *openrc) Uninstall() error {
confPath, err := s.configPath()
confPath, err := s.ConfigPath()
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions service_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type solarisService struct {
Prefix string
}

var _ ConfigInfoer = &solarisService{}

func (s *solarisService) String() string {
if len(s.DisplayName) > 0 {
return s.DisplayName
Expand Down Expand Up @@ -98,7 +100,7 @@ func (s *solarisService) template() *template.Template {
}
}

func (s *solarisService) configPath() (string, error) {
func (s *solarisService) ConfigPath() (string, error) {
return "/lib/svc/manifest/" + s.Prefix + "/" + s.Config.Name + ".xml", nil
}

Expand All @@ -108,7 +110,7 @@ func (s *solarisService) getFMRI() string {

func (s *solarisService) Install() error {
// write start script
confPath, err := s.configPath()
confPath, err := s.ConfigPath()
if err != nil {
return err
}
Expand Down Expand Up @@ -161,7 +163,7 @@ func (s *solarisService) Install() error {
func (s *solarisService) Uninstall() error {
s.Stop()

confPath, err := s.configPath()
confPath, err := s.ConfigPath()
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions service_systemd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type systemd struct {
*Config
}

var _ ConfigInfoer = &systemd{}

func newSystemdService(i Interface, platform string, c *Config) (Service, error) {
s := &systemd{
i: i,
Expand All @@ -67,7 +69,7 @@ func (s *systemd) Platform() string {
return s.platform
}

func (s *systemd) configPath() (cp string, err error) {
func (s *systemd) ConfigPath() (cp string, err error) {
if !s.isUserService() {
cp = "/etc/systemd/system/" + s.Config.Name + ".service"
return
Expand Down Expand Up @@ -134,7 +136,7 @@ func (s *systemd) isUserService() bool {
}

func (s *systemd) Install() error {
confPath, err := s.configPath()
confPath, err := s.ConfigPath()
if err != nil {
return err
}
Expand Down Expand Up @@ -194,7 +196,7 @@ func (s *systemd) Uninstall() error {
if err != nil {
return err
}
cp, err := s.configPath()
cp, err := s.ConfigPath()
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions service_sysv_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type sysv struct {
*Config
}

var _ ConfigInfoer = &sysv{}

func newSystemVService(i Interface, platform string, c *Config) (Service, error) {
s := &sysv{
i: i,
Expand All @@ -44,7 +46,7 @@ func (s *sysv) Platform() string {

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) {
if s.Option.bool(optionUserService, optionUserServiceDefault) {
err = errNoUserServiceSystemV
return
Expand All @@ -64,7 +66,7 @@ func (s *sysv) template() *template.Template {
}

func (s *sysv) Install() error {
confPath, err := s.configPath()
confPath, err := s.ConfigPath()
if err != nil {
return err
}
Expand Down Expand Up @@ -115,7 +117,7 @@ func (s *sysv) Install() error {
}

func (s *sysv) Uninstall() error {
cp, err := s.configPath()
cp, err := s.ConfigPath()
if err != nil {
return err
}
Expand Down
16 changes: 16 additions & 0 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ func TestRunInterrupt(t *testing.T) {
}
}

func TestConfigPath(t *testing.T) {
p := &program{}
sc := &service.Config{
Name: "go_service_test",
}
s, err := service.New(p, sc)
if err != nil {
t.Fatalf("New err: %s", err)
}

_, err = s.(service.ConfigInfoer).ConfigPath()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this test valid?

if err != nil {
t.Fatal("Failed to fetch or not implemented")
}
}

const testInstallEnv = "TEST_USER_INSTALL"

// Should always run, without asking for any permission
Expand Down
8 changes: 5 additions & 3 deletions service_upstart_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type upstart struct {
*Config
}

var _ ConfigInfoer = &upstart{}

func newUpstartService(i Interface, platform string, c *Config) (Service, error) {
s := &upstart{
i: i,
Expand All @@ -61,7 +63,7 @@ func (s *upstart) Platform() string {
// Upstart will be replaced by systemd in most cases anyway.
var errNoUserServiceUpstart = errors.New("User services are not supported on Upstart.")

func (s *upstart) configPath() (cp string, err error) {
func (s *upstart) ConfigPath() (cp string, err error) {
if s.Option.bool(optionUserService, optionUserServiceDefault) {
err = errNoUserServiceUpstart
return
Expand Down Expand Up @@ -126,7 +128,7 @@ func (s *upstart) template() *template.Template {
}

func (s *upstart) Install() error {
confPath, err := s.configPath()
confPath, err := s.ConfigPath()
if err != nil {
return err
}
Expand Down Expand Up @@ -164,7 +166,7 @@ func (s *upstart) Install() error {
}

func (s *upstart) Uninstall() error {
cp, err := s.configPath()
cp, err := s.ConfigPath()
if err != nil {
return err
}
Expand Down