-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
executable file
·96 lines (81 loc) · 1.76 KB
/
Copy pathmain.go
File metadata and controls
executable file
·96 lines (81 loc) · 1.76 KB
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
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"sync"
)
type SyncConfig struct { //json.Unmarshal struct must public var
LocalDir string //absolute path needed
RemoteDir string
SshHost string
SshPort int
SshUserName string
SshPassword string
IgnoreFiles []string
IgnoreDirs []string //relative path to LocalDir
ReplaceRule map[string]string
}
var (
g_WaitGroup sync.WaitGroup
g_SyncCfg SyncConfig
g_FileSyncer *FileSyncer
g_FileWatcher *FileWatcher
g_ConfigFile = "config.json"
)
func loadConfig() bool {
flag.StringVar(&g_ConfigFile, "config", "config.json", "sync config file")
flag.Parse()
_, err := os.Stat(g_ConfigFile)
if err != nil {
log.Printf("Not Exist ConfigFile:%v\n", err)
return false
}
configJson, err := ioutil.ReadFile(g_ConfigFile)
if err != nil {
log.Printf("ReadFile Error:%v\n", err)
return false
}
err = json.Unmarshal(configJson, &g_SyncCfg)
if err != nil {
log.Printf("json.Unmarshal Error:%v\n", err)
return false
}
if !filepath.IsAbs(g_SyncCfg.LocalDir) {
log.Print("LocalDir must be Abs Path\n")
return false
}
log.Printf("---load cfg: %v----\n", g_SyncCfg)
return true
}
func init() {
iCpuNum := runtime.NumCPU()
runtime.GOMAXPROCS(iCpuNum)
}
func main() {
ok := loadConfig()
if !ok {
os.Exit(-1)
}
g_FileSyncer = newFileSyncer()
ok = g_FileSyncer.Connect()
if !ok {
os.Exit(-1)
}
g_FileSyncer.Disconnect()
g_FileWatcher = newFileWatcher()
ok = g_FileWatcher.Init()
if !ok {
os.Exit(-1)
}
g_WaitGroup.Add(3)
go g_FileSyncer.Run()
go g_FileWatcher.Run()
go handleConsole()
g_WaitGroup.Wait()
log.Printf("Safe Exit!\n")
}