-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
123 lines (107 loc) · 1.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
package main
import (
"log"
"os"
"syscall"
"time"
"github.com/gen2brain/beeep"
"github.com/gen2brain/dlgs"
_ "github.com/qodrorid/godaemon"
)
type Events struct {
Event chan Event
NoEvent chan NoEvent
}
type Event struct {
Name string
}
type NoEvent struct {
}
type Error struct {
Error error
}
func atime(fi os.FileInfo) time.Time {
return time.Unix(0, fi.Sys().(*syscall.Win32FileAttributeData).LastAccessTime.Nanoseconds())
}
func main() {
args := os.Args
var file string
if len(args) < 2 {
file_innput, _, err := dlgs.File("Select file", "", false)
file = file_innput
if err != nil {
panic(err)
}
} else {
file = args[1]
}
ch := make(chan any)
done := make(chan bool)
go func() {
var latest_updated time.Time
var latest_size int64
f, err := os.Stat(file)
if err != nil {
close(ch)
log.Println(err)
return
}
atime1 := atime(f)
latest_updated = atime1
latest_size = f.Size()
for {
f_n, err := os.Stat(file)
if err != nil {
ch <- Error{
Error: err,
}
continue
}
atime := atime(f_n)
if atime.Format("2006-01-02 15:04:05") != latest_updated.Format("2006-01-02 15:04:05") {
latest_updated = atime
if latest_size == f_n.Size() {
ch <- Event{
Name: f.Name() + ": OPENED",
}
continue
} else if latest_size != f_n.Size() {
latest_size = f_n.Size()
ch <- Event{
Name: f.Name() + ": WRITE",
}
continue
} else {
ch <- NoEvent{}
continue
}
} else {
ch <- NoEvent{}
continue
}
}
}()
go func() {
for {
select {
case evt, ok := <-ch:
_ = ok
switch evt.(type) {
case Event:
err := beeep.Alert("Spyfile", evt.(Event).Name, "logo.jpg")
if err != nil {
log.Println(err)
}
continue
case Error:
err := beeep.Alert("Spyfile", evt.(Error).Error.Error(), "logo.jpg")
if err != nil {
log.Println(err)
}
continue
}
}
}
}()
<-done
}