This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (62 loc) · 1.57 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
package main
import (
"flag"
"log"
"net/url"
"time"
"github.com/pkg/sftp"
"github.com/zhulik/roger/syncer"
"golang.org/x/crypto/ssh"
)
var (
localDir = flag.String("local", "", "Local directory path")
remoteURL = flag.String("remote", "", "Remote directory URL")
workers = flag.Int("workers", 16, "Count of download workers")
// daemon options
daemon = flag.Bool("daemon", false, "Run in daemon mode")
interval = flag.Int64("interval", 120, "Interval between syncronizations (seconds)")
)
func runSync(url *url.URL, config ssh.ClientConfig) {
log.Println("Connecting...")
conn, err := ssh.Dial("tcp", url.Host, &config)
if err != nil {
panic(err)
}
sconn, err := sftp.NewClient(conn, sftp.MaxPacket(1<<15))
if err != nil {
panic(err)
}
defer sconn.Close()
syncer.Sync(sconn, *localDir, url.Path, *workers)
}
func main() {
flag.Parse()
if *localDir == "" {
panic("Please, specify local dir, see --help")
}
if *remoteURL == "" {
panic("Please, specify remote URL, see --help")
}
url, err := url.Parse(*remoteURL)
if err != nil {
panic(err)
}
username := url.User.Username()
password, _ := url.User.Password()
config := ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{ssh.Password(password)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: 5 * time.Second,
}
if *daemon {
log.Println("Running in daemon mode...")
for {
runSync(url, config)
log.Printf("Waiting %d seconds...", *interval)
time.Sleep(time.Duration(*interval) * time.Second)
}
} else {
runSync(url, config)
}
}