-
Notifications
You must be signed in to change notification settings - Fork 3
/
kebab.go
74 lines (58 loc) · 1.32 KB
/
kebab.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
package kebab
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"github.com/davidlazar/kebab/bucket"
)
type Bucket bucket.Bucket
type TarError struct {
Err error
Stderr string
}
func (e *TarError) Error() string {
s := fmt.Sprintf("tar error: %s", e.Err.Error())
if e.Stderr != "" {
lines := strings.Split(e.Stderr, "\n")
s += fmt.Sprintf(": %q", lines[0])
}
return s
}
func Put(b Bucket, srcPath string, files []string) (int64, error) {
args := []string{"-c", "-z", "-p"}
if srcPath != "" {
args = append(args, "-C", srcPath)
}
args = append(args, files...)
cmd := exec.Command("tar", args...)
w := NewWriter(b, 64*1024*1024)
cmd.Stdout = w
var buf bytes.Buffer
cmd.Stderr = &buf
err := cmd.Run()
if err != nil {
return w.Size(), &TarError{Err: err, Stderr: string(buf.Bytes())}
}
if err := w.Close(); err != nil {
return w.Size(), fmt.Errorf("Close() failed: %s", err)
}
return w.Size(), nil
}
func Get(b Bucket, destPath string) (int64, error) {
r, err := NewReader(b)
if err != nil {
return 0, err
}
if err := os.Mkdir(destPath, 0700); err != nil {
return 0, err
}
cmd := exec.Command("tar", "-x", "-z", "-f", "-", "-C", destPath)
cmd.Stdin = r
out, err := cmd.CombinedOutput()
if err != nil {
return r.Size(), &TarError{Err: err, Stderr: string(out)}
}
return r.Size(), nil
}