/
main.go
180 lines (145 loc) · 2.94 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"fmt"
"github.com/howeyc/fsnotify"
"io"
"os"
//"os/signal"
//"syscall"
"flag"
"path"
"path/filepath"
"sync"
)
var mu *sync.RWMutex
var watcher *fsnotify.Watcher
var watching map[string]*os.File
var cwd string
var (
recursive bool // recurse into directories
watchAll bool // watch hidden files
)
// function pased to os.Walk
func Tail(path string, info os.FileInfo, err error) error {
// FIXME this wont skip hidden files on windows
// not sure if people even use them
if watchAll == false && info.Name()[0:1] == "." {
return filepath.SkipDir
}
watcher.Watch(path)
addWatch(path, true)
fmt.Println(path)
return nil
}
func update(in io.Reader) {
b := make([]byte, 4096)
for {
n, err := in.Read(b)
os.Stdout.Write(b[:n])
if err != nil {
break
}
}
}
func addWatch(path string, seekEnd bool) error {
mu.Lock()
fd, err := os.Open(path)
if err != nil {
return err
}
watching[path] = fd
mu.Unlock()
if seekEnd {
fd.Seek(0, os.SEEK_END)
}
return nil
}
func watch() {
var event *fsnotify.FileEvent
var open bool
for {
if event, open = <-watcher.Event; open == false {
break
} else if event.IsModify() {
go fileModified(event)
} else if event.IsDelete() {
out("DELETED", event.Name)
} else if event.IsCreate() {
go fileCreated(event)
} else if event.IsRename() {
}
}
}
var lastformat string
func out(action, file string) {
format := fmt.Sprintf("%s <== %s\n", file, action)
if format == lastformat {
return
}
lastformat = format
fmt.Print(format)
}
func fileModified(event *fsnotify.FileEvent) {
mu.RLock()
fd, exists := watching[event.Name]
mu.RUnlock()
if exists == false {
err := addWatch(event.Name, false)
if err != nil {
fmt.Printf("couldnt open file: %s", event.Name)
return
}
fd = watching[event.Name]
fd.Seek(0, os.SEEK_END)
} else {
out("MODIFIED", event.Name)
update(fd)
}
}
func fileCreated(event *fsnotify.FileEvent) {
mu.RLock()
fd, exists := watching[event.Name]
mu.RUnlock()
if exists == false {
err := addWatch(event.Name, false)
if err != nil {
fmt.Printf("couldnt open file: %s", event.Name)
return
}
out("CREATE", event.Name)
} else {
fd.Seek(0, 0)
out("TRUNCATE", event.Name)
}
}
func main() {
flag.BoolVar(&recursive, "R", false, "tail failes in subfolders")
flag.BoolVar(&watchAll, "a", false, "watch hidden directories")
flag.Parse()
target := flag.Arg(0)
var err error
cwd, err = os.Getwd()
if err != nil {
fmt.Println("couldnt get cwd")
return
}
// if no args its cwd, otherwise the directory/file to tail
if target == "" {
target = cwd
} else {
_, staterr := os.Stat(path.Join(cwd, target))
if staterr != nil {
fmt.Println("couldnt stat " + target)
return
}
}
watcher, err = fsnotify.NewWatcher()
if err != nil {
fmt.Println("couldnt open watcher")
return
}
watching = make(map[string]*os.File)
mu = &sync.RWMutex{}
go filepath.Walk(target, Tail)
watch()
}