forked from fujiwara/stretcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_strategy.go
71 lines (59 loc) · 1.32 KB
/
sync_strategy.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
package stretcher
import (
"fmt"
"log"
"os"
"os/exec"
"strings"
)
type SyncStrategy interface {
Sync(from, to string) error
}
type RsyncStrategy struct {
*Manifest
}
var RsyncDefaultOpts = []string{"-av", "--delete"}
func NewSyncStrategy(m *Manifest) (SyncStrategy, error) {
switch m.SyncStrategy {
case "", "rsync":
// default to Rsync
return &RsyncStrategy{Manifest: m}, nil
case "mv":
return &MvSyncStrategy{}, nil
default:
return nil, fmt.Errorf("invalid strategy name: %s", m.SyncStrategy)
}
}
func (s *RsyncStrategy) Sync(from, to string) error {
m := s.Manifest
// append "/" when not terminated by "/"
if strings.LastIndex(to, "/") != len(to)-1 {
to = to + "/"
}
args := []string{}
args = append(args, RsyncDefaultOpts...)
if m.ExcludeFrom != "" {
args = append(args, "--exclude-from", from+m.ExcludeFrom)
}
if len(m.Excludes) > 0 {
for _, ex := range m.Excludes {
args = append(args, "--exclude", ex)
}
}
args = append(args, from, to)
log.Println("rsync", args)
out, err := exec.Command("rsync", args...).CombinedOutput()
if len(out) > 0 {
log.Println(string(out))
}
if err != nil {
return err
}
return nil
}
type MvSyncStrategy struct {
}
func (s *MvSyncStrategy) Sync(from, to string) error {
log.Printf("Rename srcdir %s to dest %s\n", from, to)
return os.Rename(from, to)
}