-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathtar.go
More file actions
79 lines (65 loc) · 2.07 KB
/
tar.go
File metadata and controls
79 lines (65 loc) · 2.07 KB
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
package sup
import (
"fmt"
"io"
"os/exec"
"strings"
"github.com/pkg/errors"
)
// Copying dirs/files over SSH using TAR.
// upload:
// tar -C . -cvzf - $SRC | ssh $HOST "tar -C $DST -xvzf -"
// download:
// ssh $HOST "tar -C $SRC_DIR -czvf - $SRC_FILE" | tar -C $DST -xzvf -
// RemoteTarCommand returns command to be run on remote SSH host
// to properly receive the created TAR stream.
// TODO: Check for relative directory.
func RemoteTarCommand(dir string) string {
return fmt.Sprintf("tar -C \"%s\" -xzf -", dir)
}
func LocalTarCmdArgs(path, exclude string) []string {
args := []string{}
// Added pattens to exclude from tar compress
excludes := strings.Split(exclude, ",")
for _, exclude := range excludes {
trimmed := strings.TrimSpace(exclude)
if trimmed != "" {
args = append(args, `--exclude=`+trimmed)
}
}
args = append(args, "-C", ".", "-czf", "-", path)
return args
}
// NewTarStreamReader creates a tar stream reader from a local path.
// TODO: Refactor. Use "archive/tar" instead.
func NewTarStreamReader(cwd, path, exclude string) (io.Reader, error) {
cmd := exec.Command("tar", LocalTarCmdArgs(path, exclude)...)
cmd.Dir = cwd
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, errors.Wrap(err, "tar: stdout pipe failed")
}
if err := cmd.Start(); err != nil {
return nil, errors.Wrap(err, "tar: starting cmd failed")
}
return stdout, nil
}
// RemoteTarCreateCommand forms "tar -C $SRC_DIR -czvf - $SRC_FILE"
// which is the remote part of download task
func RemoteTarCreateCommand(dir, src string) string {
return fmt.Sprintf("tar -C \"%s\" -czvf - \"%s\"", dir, src)
}
// NewTarStreamWriter creates a tar stream writer to local path
// by calling tar -C $DST -xzvf -
// which is the local part of download task
func NewTarStreamWriter(dst string) (io.Writer, error) {
cmd := exec.Command("tar", "-C", dst, "-xzvf", "-")
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, errors.Wrap(err, "tar: stdin pipe failed")
}
if err := cmd.Start(); err != nil {
return nil, errors.Wrap(err, "tar: starting cmd failed")
}
return stdin, nil
}