Skip to content

Commit

Permalink
Flag added for setting working directory for service
Browse files Browse the repository at this point in the history
  • Loading branch information
markdicksonjr committed Sep 5, 2019
1 parent e7183e6 commit f969247
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ func main() {
log.Println("simple log")
return nil
},
UseExeDirAsCwd: true,
}); 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.
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

Expand Down
14 changes: 11 additions & 3 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/kardianos/service"
"log"
"os"
"path"
)

type Info struct {
Expand All @@ -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 {
Expand All @@ -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)
}
}

Expand Down

0 comments on commit f969247

Please sign in to comment.