From f96924734228c9b85620a775efe2e0ae7355ae7b Mon Sep 17 00:00:00 2001 From: Mark Dickson Jr Date: Thu, 5 Sep 2019 15:13:38 -0400 Subject: [PATCH] Flag added for setting working directory for service --- README.md | 5 ++++- lib.go | 14 +++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 615f486..dc6a133 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ func main() { log.Println("simple log") return nil }, + UseExeDirAsCwd: true, }); err != nil { log.Fatal(err) } @@ -35,7 +36,9 @@ func main() { ``` This will create the service with the name and description provided. On Windows, the service will be "auto" and will -run immediately. +run immediately. The "UseExeDirAsCwd" flag determines whether or not the working directory is set to wherever the exe +is run from. Typically, if this is false on windows, this means the current directory will be something like +C:/Windows/system32 or such. ## Service Lifecycle diff --git a/lib.go b/lib.go index a301e26..ec0a880 100644 --- a/lib.go +++ b/lib.go @@ -4,6 +4,7 @@ import ( "github.com/kardianos/service" "log" "os" + "path" ) type Info struct { @@ -13,8 +14,9 @@ type Info struct { } type Behavior struct { - WorkFn func() error - ExitFn func() error + WorkFn func() error + ExitFn func() error + UseExeDirAsCwd bool } type program struct { @@ -27,8 +29,14 @@ func (p *program) Start(s service.Service) error { } func (p *program) run() { + if p.B.UseExeDirAsCwd { + if err := os.Chdir(path.Dir(os.Args[0])); err != nil { + panic(err) + } + } + if err := p.B.WorkFn(); err != nil { - log.Fatal(err) // todo + panic(err) } }