-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsync_wrapper.go
111 lines (93 loc) · 3.01 KB
/
rsync_wrapper.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
package piaas
import (
"fmt"
"os/exec"
)
type RsyncWrapper struct {
// The rsync command to execute, usually just 'rsync' on Mac and Linux.
// But may involves batch file on windows.
Executable
basedir string
ignoreFile string
syncTarget string
sshOptions string
syncCh chan *exec.Cmd
}
// Create a RsyncWrapper using `cmd` as the command, running on `basedir`, syncing to `target`.
// The RsyncWrapper will also be configured to use ssh options 'ConnectTimeout=10'
func NewRsyncWrapper(rsyncCmd Executable, basedir string, target string) RsyncWrapper {
return RsyncWrapper{
Executable: rsyncCmd,
basedir: basedir,
syncTarget: target,
// Use 10 as the default connect timeout. Can be override.
sshOptions: "ConnectTimeout=10",
}
}
// Open the channel to start working on sync events
//
// When cmd is received, `process` will be invoked to actually running the command.
func (rs *RsyncWrapper) Start(process func(cmd *exec.Cmd)) {
go func() {
for {
cmd := <-rs.syncCh
process(cmd)
}
}()
// The sync channel of a buffer size of 30.
rs.syncCh = make(chan *exec.Cmd, 30)
}
func (rs *RsyncWrapper) SetIgnoreFile(ignore string) {
rs.ignoreFile = ignore
}
func (rs *RsyncWrapper) SetSshOptions(options string) {
rs.sshOptions = options
}
// Sync only the specified files
// If the files list is empty, do nothing
func (rs *RsyncWrapper) SyncFiles(files []string) {
if len(files) <= 0 {
return
}
var arguments = rs.Executable.Params
arguments = append(arguments, "-av")
arguments = append(arguments, rs.getSshOptionsForRsync()...)
arguments = append(arguments, rs.getExcludeFromForRsync()...)
// Build the include file list
arguments = append(arguments, []string{"--include='*/'"}...)
for _, f := range files {
arguments = append(arguments, fmt.Sprintf("--include='%s'", f))
}
arguments = append(arguments, []string{"--exclude='*'", "--delete", "--copy-links"}...)
arguments = append(arguments, []string{".", rs.syncTarget}...)
cmd := exec.Command(rs.Executable.Cmd, arguments...)
// Build command similar to this
// Ex: rsync -av <ssh options> --exclude-from=...
// --include=... . [email protected]:~/src
// send the commands to syncCh.
rs.syncCh <- cmd
}
// Sync all files to remote.
func (rs *RsyncWrapper) SyncAll() {
var arguments = rs.Executable.Params
arguments = append(arguments, "-av")
arguments = append(arguments, rs.getSshOptionsForRsync()...)
arguments = append(arguments, rs.getExcludeFromForRsync()...)
arguments = append(arguments, []string{"--delete", "--copy-links", ".", rs.syncTarget}...)
cmd := exec.Command(rs.Executable.Cmd, arguments...)
rs.syncCh <- cmd
}
func (rs *RsyncWrapper) getSshOptionsForRsync() []string {
var sshOptions []string
if rs.sshOptions != "" {
sshOptions = []string{"-e", fmt.Sprintf("ssh -o %s", rs.sshOptions)}
}
return sshOptions
}
func (rs *RsyncWrapper) getExcludeFromForRsync() []string {
var excludeFrom []string
if rs.ignoreFile != "" {
excludeFrom = []string{fmt.Sprintf("--exclude-from=%s", rs.ignoreFile)}
}
return excludeFrom
}