This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtarball.go
72 lines (61 loc) · 1.58 KB
/
tarball.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
package main
import (
"archive/tar"
"bytes"
"compress/gzip"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/coreos/go-etcd/etcd"
"time"
)
func FillTarballBuffer(rootNode *etcd.Node) *bytes.Buffer {
buffer := bytes.NewBuffer(nil)
gzipWriter := gzip.NewWriter(buffer)
tarWriter := tar.NewWriter(gzipWriter)
// LIFO order; close the tar writer, then the gzip writer.
defer gzipWriter.Close()
defer tarWriter.Close()
writeNode(tarWriter, rootNode)
return buffer
}
func writeNode(w *tar.Writer, node *etcd.Node) { // I'm recursive!
log.WithField("key", node.Key).Debug("writing to tarball")
if node.Dir {
// Always write a header for a directory, unless it's the root.
if len(node.Key) > 0 {
w.WriteHeader(&tar.Header{
// Always strip the leading slash from the key.
Name: node.Key[1:] + "/",
Mode: 0755,
Xattrs: nodeXattrs(node),
})
}
for _, subNode := range node.Nodes {
writeNode(w, subNode) // see?
}
return
}
buf := bytes.NewBuffer([]byte(node.Value))
w.WriteHeader(&tar.Header{
// Always strip the leading slash from the key.
Name: node.Key[1:],
Mode: 0644,
Size: int64(buf.Len()),
Xattrs: nodeXattrs(node),
})
buf.WriteTo(w)
}
func nodeExpiration(node *etcd.Node) string {
if node.Expiration == nil {
return "never"
} else {
return node.Expiration.Format(time.RFC3339)
}
}
func nodeXattrs(node *etcd.Node) map[string]string {
return map[string]string{
"ModifiedIndex": fmt.Sprintf("%d", node.ModifiedIndex),
"CreatedIndex": fmt.Sprintf("%d", node.CreatedIndex),
"Expiration": nodeExpiration(node),
}
}