/
io.go
65 lines (54 loc) · 1.43 KB
/
io.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
package cloud
import (
"context"
"io"
"os"
"google.golang.org/protobuf/types/known/timestamppb"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/tilt-dev/tilt/internal/hud/webview"
"github.com/tilt-dev/tilt/internal/store"
"github.com/tilt-dev/tilt/pkg/logger"
proto_webview "github.com/tilt-dev/tilt/pkg/webview"
)
type Snapshotter struct {
st store.RStore
client ctrlclient.Client
}
func NewSnapshotter(st store.RStore, client ctrlclient.Client) *Snapshotter {
return &Snapshotter{
st: st,
client: client,
}
}
func (s *Snapshotter) WriteSnapshot(ctx context.Context, path string) {
view, err := webview.CompleteView(ctx, s.client, s.st)
if err != nil {
logger.Get(ctx).Errorf("Fetching snapshot: %v", err)
return
}
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
logger.Get(ctx).Errorf("Writing snapshot to file: %v", err)
return
}
defer func() {
_ = f.Close()
}()
snapshot := &proto_webview.Snapshot{
View: view,
CreatedAt: timestamppb.Now(),
}
err = WriteSnapshotTo(ctx, snapshot, f)
if err != nil {
logger.Get(ctx).Errorf("Writing snapshot to file: %v", err)
return
}
}
func WriteSnapshotTo(ctx context.Context, snapshot *proto_webview.Snapshot, w io.Writer) error {
jsEncoder := &runtime.JSONPb{
OrigName: false,
Indent: " ",
}
return jsEncoder.NewEncoder(w).Encode(snapshot)
}