-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
121 lines (95 loc) · 2.23 KB
/
sync.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
package zcmd
import (
"context"
"fmt"
"io/ioutil"
"os"
"strings"
)
const syncPidFile = "/tmp/sync.pid"
// Sync is client for sync pull
type Sync struct {
argSyncs []string
cfgSyncs []SyncInfo
rsyncFlags string
}
// NewSync returns Syncer
func NewSync(sync []SyncInfo, argSyncs []string, rsyncFlags string) Rsync {
return &Sync{
argSyncs: argSyncs,
cfgSyncs: sync,
rsyncFlags: rsyncFlags,
}
}
// Do is main pulling process
func (s *Sync) Do(ctx context.Context) error {
rcs, err := s.generateCmd("")
if err != nil {
return err
}
return runRsyncCmd(ctx, "sync", syncPidFile, rcs)
}
// GenerateCmd generates rsync command
func (s *Sync) generateCmd(_ string) ([]rsyncClient, error) {
optsRsync := []string{"-avP", "--stats", "--delete", "--delete-excluded"}
targetSyncs := findTargetSyncs(s.cfgSyncs, s.argSyncs)
cmds := make([]rsyncClient, len(targetSyncs))
for i, sync := range targetSyncs {
var excludeFile *os.File
if sync.Excludes != nil {
var err error
excludeFile, err = ioutil.TempFile("", sync.Name)
if err != nil {
return nil, err
}
defer excludeFile.Close()
for _, exclude := range sync.Excludes {
_, err = excludeFile.WriteString(fmt.Sprintf("*%s*\n", exclude))
if err != nil {
return nil, err
}
}
}
var cmd []string
cmdRsync, err := getRsyncCmd(sync.DisableSudo)
if err != nil {
return nil, err
}
cmd = append(cmd, cmdRsync...)
cmd = append(cmd, optsRsync...)
src := sync.Source
dst := sync.Destination
if excludeFile != nil {
cmd = append(cmd, fmt.Sprintf("--exclude-from=%s", excludeFile.Name()))
}
// Add "/" to sync all files in the source URL directory
if !strings.HasSuffix(src, "/") {
src += "/"
}
if len(s.rsyncFlags) != 0 {
cmd = append(cmd, s.rsyncFlags)
}
cmd = append(cmd, src, dst)
cmds[i] = rsyncClient{
command: cmd,
excludeFile: excludeFile,
}
}
return cmds, nil
}
func findTargetSyncs(cfgs []SyncInfo, args []string) []SyncInfo {
if len(args) == 0 {
// Sync all paths
return cfgs
}
targetCfgs := make([]SyncInfo, 0)
for _, cfg := range cfgs {
for _, arg := range args {
if cfg.Name == arg {
targetCfgs = append(targetCfgs, cfg)
break
}
}
}
return targetCfgs
}