-
Notifications
You must be signed in to change notification settings - Fork 74
/
utils.go
68 lines (56 loc) · 1.16 KB
/
utils.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
// Copyright 2021 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package common
import (
"bytes"
"compress/gzip"
"io"
"os"
"path/filepath"
"github.com/pkg/errors"
)
// GzipData compresses the data bytes
func GzipData(data []byte) ([]byte, error) {
var b bytes.Buffer
gz := gzip.NewWriter(&b)
if _, err := gz.Write(data); err != nil {
return nil, err
}
if err := gz.Flush(); err != nil {
return nil, err
}
if err := gz.Close(); err != nil {
return nil, err
}
return b.Bytes(), nil
}
// GunzipData un-compress the compressed data bytes
func GunzipData(data []byte) ([]byte, error) {
var r io.Reader
var err error
b := bytes.NewBuffer(data)
r, err = gzip.NewReader(b)
if err != nil {
return nil, errors.WithStack(err)
}
var resB bytes.Buffer
_, err = resB.ReadFrom(r)
if err != nil {
return nil, errors.WithStack(err)
}
return resB.Bytes(), nil
}
// RemoveGlob removes glob file as specified path
func RemoveGlob(path string) error {
contents, err := filepath.Glob(path)
if err != nil {
return err
}
for _, item := range contents {
err = os.RemoveAll(item)
if err != nil {
return err
}
}
return nil
}