-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
129 lines (115 loc) · 2.95 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
package main
import (
"flag"
"fmt"
"io"
"io/fs"
"os"
"os/signal"
"path/filepath"
"strings"
"time"
"github.com/mattn/go-colorable"
)
const (
_STAMP_STYLE = "15:04:05.000"
)
var (
flagRoot = flag.String("target", "", "Set the target `directory`")
flagOnAdd = flag.String("add", "", "execute `commandline`({} is replaced to the path) on new file found")
flagOnUpd = flag.String("upd", "", "execute `commandline({} is replaced to the path)` on file updated")
flagOnDel = flag.String("del", "", "execute `commandline({} is replaced to the path)` on file deleted")
)
func eventAction(cmdline, filename string) {
cmdline = strings.ReplaceAll(cmdline, `{}`, `"`+filename+`"`)
fmt.Println(cmdline)
System(cmdline)
}
func filesEqual(left, right fs.FileInfo) bool {
var size1 int64 = 0
var time1 time.Time
if left != nil && !left.IsDir() {
size1 = left.Size()
time1 = left.ModTime()
}
var size2 int64
var time2 time.Time
if right != nil && !right.IsDir() {
size2 = right.Size()
time2 = right.ModTime()
}
return size1 == size2 && time1 == time2
}
func watch(rootPath string, out io.Writer) error {
previous := make(map[string]fs.FileInfo)
filepath.Walk(rootPath, func(path string, info fs.FileInfo, err error) error {
previous[path] = info
return nil
})
ctrlc := make(chan os.Signal, 1)
signal.Notify(ctrlc, os.Interrupt)
defer signal.Reset(os.Interrupt)
ticker := time.NewTicker(time.Second / 5)
defer ticker.Stop()
fmt.Fprintf(out, "\x1B[37;1mWatch Start: %s\x1B[0m\n", rootPath)
for {
select {
case next := <-ticker.C:
current := make(map[string]fs.FileInfo)
stamp := next.Format(_STAMP_STYLE)
filepath.Walk(rootPath, func(path string, info fs.FileInfo, err error) error {
relPath, err := filepath.Rel(rootPath, path)
if err != nil {
relPath = path
}
if pre, ok := previous[path]; ok {
if !filesEqual(pre, info) {
if *flagOnUpd != "" {
eventAction(*flagOnUpd, path)
}
fmt.Fprintf(out, "\x1B[33;1m%s Upd %s\x1B[0m\n", stamp, relPath)
}
delete(previous, path)
} else {
fmt.Fprintf(out, "\x1B[32;1m%s Add %s\x1B[0m\n", stamp, relPath)
if *flagOnAdd != "" {
eventAction(*flagOnAdd, path)
}
}
current[path] = info
return nil
})
for path := range previous {
relPath, err := filepath.Rel(rootPath, path)
if err != nil {
relPath = path
}
fmt.Fprintf(out, "\x1B[31;1m%s Del %s\x1B[0m\n", stamp, relPath)
if *flagOnDel != "" {
eventAction(*flagOnDel, path)
}
}
previous = current
case <-ctrlc:
fmt.Fprintln(out, "\x1B[37;1mDone\x1B[0m")
return nil
}
}
}
func mains() error {
if dispose := colorable.EnableColorsStdout(nil); dispose != nil {
defer dispose()
}
target := *flagRoot
if target == "" {
target = os.TempDir()
}
return watch(target, colorable.NewColorableStdout())
}
func main() {
flag.Parse()
if err := mains(); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}