forked from gazette/core
-
Notifications
You must be signed in to change notification settings - Fork 2
/
journals_apply.go
60 lines (48 loc) · 1.58 KB
/
journals_apply.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
package main
import (
"context"
"os"
"github.com/LiveRamp/gazette/v2/pkg/client"
mbp "github.com/LiveRamp/gazette/v2/pkg/mainboilerplate"
pb "github.com/LiveRamp/gazette/v2/pkg/protocol"
"github.com/LiveRamp/gazette/v2/pkg/protocol/journalspace"
"github.com/gogo/protobuf/proto"
log "github.com/sirupsen/logrus"
)
type cmdJournalsApply struct {
ApplyConfig
}
func (cmd *cmdJournalsApply) Execute([]string) error {
startup()
// Decode journal specification tree from YAML.
var tree journalspace.Node
mbp.Must(cmd.decode(&tree), "failed to decode journal tree")
mbp.Must(tree.Validate(), "journal tree failed to validate")
var req = newJournalSpecApplyRequest(&tree)
mbp.Must(req.Validate(), "failed to validate ApplyRequest")
if cmd.DryRun {
_ = proto.MarshalText(os.Stdout, req)
return nil
}
var ctx = context.Background()
resp, err := client.ApplyJournals(ctx, journalsCfg.Broker.JournalClient(ctx), req)
mbp.Must(err, "failed to apply journals")
log.WithField("rev", resp.Header.Etcd.Revision).Info("successfully applied")
return nil
}
// newJournalSpecApplyRequest flattens a journal specification tree into
// concrete JournalSpecs and builds the request.
func newJournalSpecApplyRequest(tree *journalspace.Node) *pb.ApplyRequest {
var nodes = tree.Flatten()
var req = new(pb.ApplyRequest)
for i := range nodes {
var change = pb.ApplyRequest_Change{ExpectModRevision: nodes[i].Revision}
if nodes[i].Delete {
change.Delete = nodes[i].JournalSpec.Name
} else {
change.Upsert = &nodes[i].JournalSpec
}
req.Changes = append(req.Changes, change)
}
return req
}