aboutsummaryrefslogtreecommitdiffsponsor
path: root/internal/service/rsync.go
blob: c9c7e922547e2c5cfd665b0fc946b371bc9e0181 (plain)
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
package service

import (
	"bytes"
	"errors"
	"os/exec"
	"strings"

	"git.server.ky/slackcoder/mirror/internal"
)

var rsyncOpts = []string{
	"--delete-excluded",
	"--hard-links",
	"--links",
	"--perms",
	"--recursive",
	"--safe-links",
	"--sparse",
	"--times",
}

func Rsync(dst *internal.URL, src *internal.URL) error {
	src2 := *src
	if !strings.HasSuffix(src2.Path, "/.") {
		src2.Path = src2.Path + "/."
	}

	var stderr bytes.Buffer

	args := append(rsyncOpts, src2.String(), dst.String())
	cmd := exec.Command("rsync", args...)
	cmd.Stderr = &stderr

	err := cmd.Run()
	if err != nil {
		return errors.New(stderr.String())
	}

	return nil
}