From adc981bbe14632b92e7c0bcdbde7fb2890d89133 Mon Sep 17 00:00:00 2001 From: Mark Dickson Jr Date: Mon, 26 Aug 2019 22:47:31 -0500 Subject: [PATCH] Readme updates, configurable commands --- README.md | 60 ++++++++++++++++++++++++++++++++++++++++++++++++-- lib.go | 43 +++++++++++++++++++++--------------- sample/main.go | 2 +- 3 files changed, 84 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index ae3d8aa..94a7a78 100644 --- a/README.md +++ b/README.md @@ -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) \ No newline at end of file +[github.com/takama/daemon](http://github.com/takama/daemon) diff --git a/lib.go b/lib.go index f0e9721..b02ab7d 100644 --- a/lib.go +++ b/lib.go @@ -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 { @@ -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 { @@ -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() diff --git a/sample/main.go b/sample/main.go index 88bb9e7..a07511f 100644 --- a/sample/main.go +++ b/sample/main.go @@ -18,4 +18,4 @@ func main() { }); err != nil { log.Fatal(err) } -} \ No newline at end of file +}