-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
144 lines (116 loc) · 3.11 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2018 Yaacov Zamir <kobi.zamir@gmail.com>
// and other contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package main
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"strings"
"sync"
"time"
"github.com/yaacov/observer/observer"
)
type arrayFlags []string
func (i *arrayFlags) String() string {
return "my string representation"
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
func runScript(s string) (err error) {
// Get command line to run on events.
c := strings.Split(s, " ")
// If script has no args, just run it, o/w sent args.
if len(c) == 1 {
err = exec.Command(c[0]).Run()
} else if len(c) > 1 {
err = exec.Command(c[0], c[1:]...).Run()
}
return
}
func printUsage() {
fmt.Println("Usage:")
flag.PrintDefaults()
fmt.Println("")
fmt.Println("Examples:")
fmt.Println(" observer -w main.c -r ./run.sh")
fmt.Println(" observer -w main.c -w src/*.c -r run.sh -d 1")
os.Exit(1)
}
func main() {
var err error
var watchFiles arrayFlags
var scripts arrayFlags
// Create a mutex for the event listener.
mutex := &sync.Mutex{}
// Parse cli arguments.
flag.Var(&watchFiles, "w", "list of files to watch.")
flag.Var(&scripts, "r", "list of scripts to run on file modifiaction event.")
bufferSecPtr := flag.Int("d", 0, "buffer events for N sec.")
verbosePtr := flag.Bool("V", false, "dump debug data.")
flag.Usage = printUsage
flag.Parse()
// Check user input.
if len(watchFiles) < 1 {
fmt.Println("[Error] no watch files.")
flag.Usage()
}
// Sanity check.
if len(scripts) < 1 {
fmt.Println("[Error] no scripts to run.")
flag.Usage()
}
// Open observer and start watching.
o := observer.Observer{}
defer o.Close()
// Set verbosity.
o.Verbose = *verbosePtr
// Set damping time.
if *bufferSecPtr != 0 {
sec := time.Duration(*bufferSecPtr) * time.Second
o.SetBufferDuration(sec)
}
// Watch for changes in files.
err = o.Watch(watchFiles)
if err != nil {
log.Fatal("[Error] watch files: ", err)
}
// Add a listener for events.
o.AddListener(func(e interface{}) {
// Lock the listener.
mutex.Lock()
defer mutex.Unlock()
// Log the event.
log.Printf("[Info]Received: %v\n", e)
for _, s := range scripts {
// Try to run a script. and check for errors running script.
if err = runScript(s); err != nil {
log.Printf("[Error] running event listener: %s\n", err)
}
}
})
// Log watcher starting.
log.Print("Observer starting.")
log.Print("Press Ctrl+C to exit.")
// Wait for Ctrl+C.
waitCtrlC := make(chan os.Signal, 1)
signal.Notify(waitCtrlC, os.Interrupt)
<-waitCtrlC
}