/
tracking_utils.go
75 lines (61 loc) · 1.47 KB
/
tracking_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
69
70
71
72
73
74
75
package analytics
import (
"archive/zip"
"bytes"
"encoding/json"
"fmt"
"go.uber.org/zap"
"math"
"net/http"
"time"
"github.com/klothoplatform/klotho/pkg/core"
)
const kloServerUrl = "http://srv.klo.dev"
type AnalyticsFile struct {
Id string
}
func (t *Client) send(payload Payload) {
postBody, _ := json.Marshal(payload)
data := bytes.NewBuffer(postBody)
url := t.serverUrlOverride
if url == "" {
url = kloServerUrl
}
resp, err := http.Post(fmt.Sprintf("%s/analytics/track", url), "application/json", data)
if err != nil {
zap.L().Debug(fmt.Sprintf("Failed to send metrics info. %v", err))
return
}
resp.Body.Close()
}
func CompressFiles(input *core.InputFiles) ([]byte, error) {
buf := new(bytes.Buffer)
zipWriter := zip.NewWriter(buf)
now := time.Now().UTC()
for _, f := range input.Files() {
buf := new(bytes.Buffer)
if _, err := f.WriteTo(buf); err != nil {
return nil, err
}
header := &zip.FileHeader{
Method: zip.Deflate,
Name: f.Path(),
UncompressedSize64: uint64(buf.Len()),
Modified: now,
}
if header.UncompressedSize64 >= math.MaxUint32 {
header.UncompressedSize = math.MaxUint32
} else {
header.UncompressedSize = uint32(header.UncompressedSize64)
}
headerWriter, err := zipWriter.CreateHeader(header)
if err != nil {
return nil, err
}
if _, err := buf.WriteTo(headerWriter); err != nil {
return nil, err
}
}
err := zipWriter.Close()
return buf.Bytes(), err
}