-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhelpers.go
More file actions
72 lines (61 loc) · 1.28 KB
/
helpers.go
File metadata and controls
72 lines (61 loc) · 1.28 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
// ThreatSpec package main
package main
import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/hex"
"fmt"
"github.com/pki-io/core/crypto"
"io/ioutil"
"os"
"time"
)
func NewID() string {
idBytes, _ := crypto.RandomBytes(16)
return hex.EncodeToString(idBytes)
}
type ExportFile struct {
Name string
Mode int64
Owner int64
Group int64
Content []byte
}
func TarGZ(files []ExportFile) ([]byte, error) {
tarBuffer := new(bytes.Buffer)
tarWriter := tar.NewWriter(tarBuffer)
for _, file := range files {
header := &tar.Header{
Name: file.Name,
Mode: int64(file.Mode),
Size: int64(len(file.Content)),
ModTime: time.Now(),
}
if err := tarWriter.WriteHeader(header); err != nil {
return nil, err
}
if _, err := tarWriter.Write(file.Content); err != nil {
return nil, err
}
}
if err := tarWriter.Close(); err != nil {
return nil, err
}
zipBuffer := new(bytes.Buffer)
zipWriter := gzip.NewWriter(zipBuffer)
zipWriter.Write(tarBuffer.Bytes())
zipWriter.Close()
return zipBuffer.Bytes(), nil
}
func Export(files []ExportFile, outFile string) {
tarGz, err := TarGZ(files)
fmt.Println(err)
if outFile == "-" {
os.Stdout.Write(tarGz)
} else {
// Write to file
err := ioutil.WriteFile(outFile, tarGz, 0600)
fmt.Println(err)
}
}