Skip to content

Commit

Permalink
Readme updates, configurable commands
Browse files Browse the repository at this point in the history
  • Loading branch information
markdicksonjr committed Aug 27, 2019
1 parent e039cc2 commit adc981b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 21 deletions.
60 changes: 58 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,63 @@
# Simple Daemon

A super-simple wrapper for making a simple cross-platform service.
A super-simple wrapper for making a simple cross-platform service. With only a few lines of code, you can
run your process as a daemon/service.

## Getting Started

Get this repository:

`go get github.com/markdicksonjr/simple-daemon`

Initialize a simple daemon instance with the basic information about the service (like name and description), along with
the behavior of the service (most importantly, the function that does the work).

```go
import (
simple_daemon "github.com/markdicksonjr/simple-daemon"
"log"
)

func main() {
if err := simple_daemon.Start(simple_daemon.Info{
Name: "Test",
Description: "A simple test daemon",
Dependencies: nil,
}, simple_daemon.Behavior{
WorkFn: func() error {
log.Println("simple log")
return nil
},
}); err != nil {
log.Fatal(err)
}
}
```

This will create the service with the name and description provided. On Windows, the service will be "auto" and will
run immediately.

## Service Lifecycle

The various lifecycle stages of services can be managed (by default):

`binaryName install`

`binaryName remove`

`binaryName start`

`binaryName stop`

`binaryName status`

To customize these flags, you can handle them yourself and set the "Command" property on the Behavior you pass to the
service at creation time.

## Restrictions

Due to a third-party implementation detail, service names with spaces will have those spaces converted to underscores.

## Credits

[github.com/takama/daemon](http://github.com/takama/daemon)
[github.com/takama/daemon](http://github.com/takama/daemon)
43 changes: 25 additions & 18 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ type Info struct {
}

type Behavior struct {
WorkFn func() error
ExitFn func() error
StdLog *log.Logger
ErrLog *log.Logger
WorkFn func() error
ExitFn func() error
StdLog *log.Logger
ErrLog *log.Logger
Command string
}

func Start(info Info, behavior Behavior) error {
Expand All @@ -28,12 +29,13 @@ func Start(info Info, behavior Behavior) error {
}

s := ManagedService{
Daemon: srv,
info: info,
workFn: behavior.WorkFn,
exitFn: behavior.ExitFn,
stdlog: behavior.StdLog,
errlog: behavior.ErrLog,
Daemon: srv,
info: info,
workFn: behavior.WorkFn,
exitFn: behavior.ExitFn,
stdlog: behavior.StdLog,
errlog: behavior.ErrLog,
command: behavior.Command,
}

if s.stdlog == nil {
Expand All @@ -56,19 +58,24 @@ func Start(info Info, behavior Behavior) error {

type ManagedService struct {
daemon.Daemon
workFn func() error
exitFn func() error
info Info
stdlog *log.Logger
errlog *log.Logger
workFn func() error
exitFn func() error
info Info
stdlog *log.Logger
errlog *log.Logger
command string
}

func (s *ManagedService) Manage() (string, error) {
usage := "Usage: " + s.info.Name + " install | remove | start | stop | status"
command := s.command

// if received any kind of command, do it
if len(os.Args) > 1 {
command := os.Args[1]
// if received any kind of command, and we haven't specified one ourselves, do it
if len(command) == 0 && len(os.Args) > 1 {
command = os.Args[1]
}

if len(command) > 0 {
switch command {
case "install":
return s.Install()
Expand Down
2 changes: 1 addition & 1 deletion sample/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ func main() {
}); err != nil {
log.Fatal(err)
}
}
}

0 comments on commit adc981b

Please sign in to comment.