/
journals_edit.go
67 lines (55 loc) · 1.65 KB
/
journals_edit.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
package main
import (
"bytes"
"context"
"io"
log "github.com/sirupsen/logrus"
"go.gazette.dev/core/broker/client"
"go.gazette.dev/core/broker/journalspace"
"go.gazette.dev/core/cmd/gazctl/editor"
mbp "go.gazette.dev/core/mainboilerplate"
"gopkg.in/yaml.v2"
)
type cmdJournalsEdit struct {
EditConfig
}
func init() {
_ = mustAddCmd(cmdJournals, "edit", "Edit journal specifications", journalsEditLongDesc, &cmdJournalsEdit{})
}
func (cmd *cmdJournalsEdit) Execute([]string) error {
startup()
return editor.EditRetryLoop(editor.RetryLoopArgs{
FilePrefix: "gazctl-journals-edit-",
SelectFn: cmd.selectSpecs,
ApplyFn: cmd.applySpecs,
AbortIfUnchanged: true,
})
}
// selectSpecs returns the hoisted YAML specs of journals matching the selector.
func (cmd *cmdJournalsEdit) selectSpecs() io.Reader {
var resp = listJournals(cmd.Selector)
if len(resp.Journals) == 0 {
log.WithField("selector", cmd.Selector).Panic("no journals match selector")
}
var buf = &bytes.Buffer{}
writeHoistedJournalSpecTree(buf, resp)
return buf
}
func (cmd *cmdJournalsEdit) applySpecs(b []byte) error {
var tree journalspace.Node
if err := yaml.UnmarshalStrict(b, &tree); err != nil {
return err
}
if err := tree.Validate(); err != nil {
return err
}
var req = newJournalSpecApplyRequest(&tree)
if err := req.Validate(); err != nil {
return err
}
var ctx = context.Background()
var resp, err = client.ApplyJournalsInBatches(ctx, journalsCfg.Broker.MustJournalClient(ctx), req, cmd.MaxTxnSize)
mbp.Must(err, "failed to apply journals")
log.WithField("revision", resp.Header.Etcd.Revision).Info("successfully applied")
return nil
}