-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.go
119 lines (90 loc) · 2.01 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
package main
import (
"github.com/fsnotify/fsnotify"
"log"
"time"
"path/filepath"
)
// Package const
// Config file path name
const (
ConfigFilePathName = "./mancy_config.json"
)
// Package variables
var (
// Local dir name witch will be watched
localDir string
// Local dir name witch will be watched
remoteDir string
// Config data struct
config Config
// Global chan variables
// file_watcher will write the chan and file_handle will read the chan
// create file
fileCreateEvent = make(chan string)
// write
fileWriteEvent = make(chan string)
// remove
fileRemoveEvent = make(chan string)
// rename
fileRenameEvent = make(chan string)
// chmod
fileChmodEvent = make(chan string)
// watchMainJob chan
watcherHandlerDone = make(chan bool)
// fileHandleMainJob chan
fileHandlerDone = make(chan bool)
// timeout for watcher event
fileHandleTimeOut = time.Second * 4
)
// Config file struct
type Config struct{
// filePath
LocalDir string `localDir`
RemoteDir string `remoteDir`
// ssh
SshHost string `sshHost`
SshPort int `sshPort`
SshUserName string `sshUserName`
SshPassword string `sshPassword`
// ignoreFiles
IgnoreFiles []string `ignoreFiles`
}
// Init
func init() {
// Reset log format
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
// Check and generate config file
checkConfigFile()
// Parse config file
parseConfigFile()
// Set localDir as ABS path
localDir, err = filepath.Abs(config.LocalDir)
// Set remote dir
remoteDir = config.RemoteDir
if err != nil {
log.Fatal("Init localFilePath error: ")
panic(err)
}
}
func main() {
watch, _ := fsnotify.NewWatcher()
w := Watch{
watch: watch,
}
// Watch the local directory
go func() {
w.watchDir(localDir)
watcherHandlerDone <- true
}()
// handle the file events
go func() {
// Handle file with sftp (autoUpload changes)
// And you can change the handler whatever you need like rsync
fileSftpHandler()
fileHandlerDone <- true
}()
// Waiting job done
<-watcherHandlerDone
<-fileHandlerDone
}