Skip to content

Commit

Permalink
Merge pull request #12 from Sheile/feature/dependencies
Browse files Browse the repository at this point in the history
Add dependencies option
  • Loading branch information
takama committed Sep 1, 2015
2 parents d244e72 + 76b2721 commit 170c1ee
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 23 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func main() {
service, err := daemon.New("name", "description")
service, err := daemon.New("name", "description", []string{})
if err != nil {
log.Fatal("Error: ", err)
}
Expand Down Expand Up @@ -57,6 +57,9 @@ const (
port = ":9977"
)

// dependencies that are required by the service
var dependencies = []string{"dummy.service"}

var stdlog, errlog *log.Logger

// Service has embedded daemon
Expand Down Expand Up @@ -155,7 +158,7 @@ func init() {
}

func main() {
srv, err := daemon.New(name, description)
srv, err := daemon.New(name, description, dependencies)
if err != nil {
errlog.Println("Error: ", err)
os.Exit(1)
Expand Down
9 changes: 6 additions & 3 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Example:
port = ":9977"
)
// dependencies that are required by the service
var dependencies = []string{"dummy.service"}
var stdlog, errlog *log.Logger
// Service has embedded daemon
Expand Down Expand Up @@ -134,7 +137,7 @@ Example:
}
func main() {
srv, err := daemon.New(name, description)
srv, err := daemon.New(name, description, dependencies)
if err != nil {
errlog.Println("Error: ", err)
os.Exit(1)
Expand Down Expand Up @@ -177,6 +180,6 @@ type Daemon interface {
//
// name: name of the service
// description: any explanation, what is the service, its purpose
func New(name, description string) (Daemon, error) {
return newDaemon(strings.Join(strings.Fields(name), "_"), description)
func New(name, description string, dependencies []string) (Daemon, error) {
return newDaemon(strings.Join(strings.Fields(name), "_"), description, dependencies)
}
9 changes: 5 additions & 4 deletions daemon_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import (

// darwinRecord - standard record (struct) for darwin version of daemon package
type darwinRecord struct {
name string
description string
name string
description string
dependencies []string
}

func newDaemon(name, description string) (Daemon, error) {
func newDaemon(name, description string, dependencies []string) (Daemon, error) {

return &darwinRecord{name, description}, nil
return &darwinRecord{name, description, dependencies}, nil
}

// Standard service path for system daemons
Expand Down
6 changes: 3 additions & 3 deletions daemon_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
)

// Get the daemon properly
func newDaemon(name, description string) (Daemon, error) {
func newDaemon(name, description string, dependencies []string) (Daemon, error) {
if _, err := os.Stat("/run/systemd/system"); err == nil {
return &systemDRecord{name, description}, nil
return &systemDRecord{name, description, dependencies}, nil
}
return &systemVRecord{name, description}, nil
return &systemVRecord{name, description, dependencies}, nil
}

// Get executable path
Expand Down
12 changes: 8 additions & 4 deletions daemon_linux_systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"os"
"os/exec"
"regexp"
"strings"
"text/template"
)

// systemDRecord - standard record (struct) for linux systemD version of daemon package
type systemDRecord struct {
name string
description string
name string
description string
dependencies []string
}

// Standard service path for systemD daemons
Expand Down Expand Up @@ -83,8 +85,8 @@ func (linux *systemDRecord) Install() (string, error) {
if err := templ.Execute(
file,
&struct {
Name, Description, Path string
}{linux.name, linux.description, execPatch},
Name, Description, Dependencies, Path string
}{linux.name, linux.description, strings.Join(linux.dependencies, " "), execPatch},
); err != nil {
return installAction + failed, err
}
Expand Down Expand Up @@ -187,6 +189,8 @@ func (linux *systemDRecord) Status() (string, error) {

var systemDConfig = `[Unit]
Description={{.Description}}
Requires={{.Dependencies}}
After={{.Dependencies}}
[Service]
PIDFile=/var/run/{{.Name}}.pid
Expand Down
5 changes: 3 additions & 2 deletions daemon_linux_systemv.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (

// systemVRecord - standard record (struct) for linux systemV version of daemon package
type systemVRecord struct {
name string
description string
name string
description string
dependencies []string
}

// Standard service path for systemV daemons
Expand Down
9 changes: 5 additions & 4 deletions daemon_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (

// windowsRecord - standard record (struct) for windows version of daemon package
type windowsRecord struct {
name string
description string
name string
description string
dependencies []string
}

func newDaemon(name, description string) (Daemon, error) {
func newDaemon(name, description string, dependencies []string) (Daemon, error) {

return &windowsRecord{name, description}, nil
return &windowsRecord{name, description, dependencies}, nil
}

// Install the service
Expand Down
5 changes: 4 additions & 1 deletion example/myservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const (
port = ":9977"
)

// dependencies that are required by the service
var dependencies = []string{"dummy.service"}

var stdlog, errlog *log.Logger

// Service has embedded daemon
Expand Down Expand Up @@ -120,7 +123,7 @@ func init() {
}

func main() {
srv, err := daemon.New(name, description)
srv, err := daemon.New(name, description, dependencies)
if err != nil {
errlog.Println("Error: ", err)
os.Exit(1)
Expand Down

0 comments on commit 170c1ee

Please sign in to comment.