/
main.go
45 lines (37 loc) · 972 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"fmt"
"github.com/zero-boilerplate/go-api-helpers/service"
"path/filepath"
service2 "github.com/ayufan/golang-kardianos-service"
)
type app struct {
logger service2.Logger
watcherDoneChannel chan bool
}
func (a *app) OnStop() {
defer recover()
if a.watcherDoneChannel != nil {
close(a.watcherDoneChannel)
}
}
func (a *app) Run(logger service2.Logger) {
a.logger = logger
defer func() {
if r := recover(); r != nil {
a.logger.Errorf("Run app error: %s", getStringFromRecovery(r))
}
}()
userHomeDir := getUserHomeDir()
watchDir := filepath.Join(userHomeDir, ".script-watcher", "scripts")
if !doesDirExist(watchDir) {
panic(fmt.Sprintf("The watch dir '%s' does not exist", watchDir))
return
}
a.scanDirForExistingFile(watchDir)
a.startWatching(watchDir)
}
func main() {
a := &app{}
service.NewServiceRunnerBuilder("Script Watcher", a).WithOnStopHandler(a).WithServiceUserName_AsCurrentUser().Run()
}