Compare views rebuilt: from the zone's view, in sections, one line per record
The Compare views tab and jarl zone diff compare a zone with the same zone in other views from the view you're on: a summary per view, what is answered differently (Use this everywhere), the names only some views have (Add to VIEW), and how many are the same. One name can be compared (jarl zone diff ZONE NAME [TYPE], a Compare button per record). The CLI prints one line per record, cut to the terminal (--wide shows it whole); the web UI cuts long values with the whole one on hover. GET /zones/{zone}/compare returns these sections, with ?with, ?name and ?type.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ec16de875d
commit
b1719ce362
12 files changed
+656
-146
No files matched your search
@@ -0,0 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func TestPrintCompare(t *testing.T) {
|
||||
long := `"v=spf1 ` + strings.Repeat("include:_spf.example.net ", 10) + `-all"`
|
||||
r := &compareReport{Zone: "adbraun.lol", Base: "internal", Views: []string{"internal", "external"}, Linked: true}
|
||||
r.Summary = append(r.Summary, struct {
|
||||
View string `json:"view"`
|
||||
Different int `json:"different"`
|
||||
OnlyBase int `json:"only_base"`
|
||||
OnlyOther int `json:"only_other"`
|
||||
Same int `json:"same"`
|
||||
}{View: "external", Different: 1, OnlyBase: 1, Same: 5})
|
||||
r.Different = []compareRow{{Name: "@", Type: "TXT", Views: []compareSet{
|
||||
{View: "internal", Records: []string{long, `"short"`}, IDs: []int64{7, 8}, Linked: "local"},
|
||||
{View: "external", Records: []string{`"other"`}, IDs: []int64{9}, Linked: "local"},
|
||||
}}}
|
||||
r.Only = append(r.Only, struct {
|
||||
Views []string `json:"views"`
|
||||
Rows []compareRow `json:"rows"`
|
||||
}{Views: []string{"internal"}, Rows: []compareRow{{Name: "nas", Type: "A", Views: []compareSet{{View: "internal", Records: []string{"10.0.10.20"}}, {View: "external"}}}}})
|
||||
r.Same = 5
|
||||
|
||||
var b bytes.Buffer
|
||||
printCompare(&b, r, false, 60)
|
||||
out := b.String()
|
||||
// Record lines fit (the heading and summary are prose, and may wrap).
|
||||
for _, l := range strings.Split(strings.TrimRight(out, "\n"), "\n")[3:] {
|
||||
if n := utf8.RuneCountInString(l); n > 60 {
|
||||
t.Errorf("line of %d characters: %q", n, l)
|
||||
}
|
||||
}
|
||||
for _, want := range []string{
|
||||
"adbraun.lol in internal, compared with external\n linked: ",
|
||||
" external: 1 answered differently, 1 only in internal, 0 only in external, 5 the same",
|
||||
"ANSWERED DIFFERENTLY (1)\n @ TXT\n internal \"v=spf1 include:",
|
||||
"…\n \"short\"\n external \"other\" [local, id 9]",
|
||||
"ONLY IN INTERNAL (1)\n nas A 10.0.10.20",
|
||||
"5 names and types are the same (--all lists them)",
|
||||
} {
|
||||
if !strings.Contains(out, want) {
|
||||
t.Errorf("output lacks %q:\n%s", want, out)
|
||||
}
|
||||
}
|
||||
// Width 0: whole.
|
||||
b.Reset()
|
||||
printCompare(&b, r, false, 0)
|
||||
if !strings.Contains(b.String(), long+" [local, id 7]") {
|
||||
t.Errorf("whole: %s", b.String())
|
||||
}
|
||||
}
|
||||
+181
-62
@@ -962,89 +962,208 @@ zone is dissolved.`
|
||||
|
||||
// zoneDiffCmd is 'zone diff': a zone's records side by side in the views
|
||||
// that have it.
|
||||
type compareSet struct {
|
||||
View string `json:"view"`
|
||||
Records []string `json:"records"`
|
||||
IDs []int64 `json:"ids"`
|
||||
Linked string `json:"linked"`
|
||||
}
|
||||
|
||||
type compareRow struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Views []compareSet `json:"views"`
|
||||
}
|
||||
|
||||
type compareReport struct {
|
||||
Zone string `json:"zone"`
|
||||
Base string `json:"base"`
|
||||
Views []string `json:"views"`
|
||||
Linked bool `json:"linked"`
|
||||
Summary []struct {
|
||||
View string `json:"view"`
|
||||
Different int `json:"different"`
|
||||
OnlyBase int `json:"only_base"`
|
||||
OnlyOther int `json:"only_other"`
|
||||
Same int `json:"same"`
|
||||
} `json:"summary"`
|
||||
Different []compareRow `json:"different"`
|
||||
Only []struct {
|
||||
Views []string `json:"views"`
|
||||
Rows []compareRow `json:"rows"`
|
||||
} `json:"only"`
|
||||
Same int `json:"same"`
|
||||
SameRows []compareRow `json:"same_rows"`
|
||||
}
|
||||
|
||||
func zoneDiffCmd() *cobra.Command {
|
||||
var views []string
|
||||
var all bool
|
||||
var view string
|
||||
var with []string
|
||||
var all, wide bool
|
||||
cmd := &cobra.Command{
|
||||
Use: "diff ZONE [--view V --view W] [--all]",
|
||||
Short: "Show what differs between the views that have a zone",
|
||||
Long: `Lines up a zone's records in the views that have it (or the views
|
||||
given), one row per name and type, and shows the ones that differ: a
|
||||
local override in a linked zone, a record one view lacks. With --all, the
|
||||
ones that are the same too. In a linked zone, 'jarl record update ID
|
||||
--shared' makes a view's version every view's.`,
|
||||
Use: "diff ZONE [NAME [TYPE]] [--view V] [--with W...] [--all] [--wide]",
|
||||
Short: "Compare a zone with the same zone in other views",
|
||||
Long: `Compares a zone in one view (--view, default the first view that has it)
|
||||
with the same zone in the other views (--with, default all of them): how
|
||||
many names each answers differently, the names and types only some views
|
||||
have, and how many are the same. NAME (and TYPE) narrows it to one name,
|
||||
the same records included. One line per record; long values are cut to
|
||||
fit the terminal (--wide shows them whole).
|
||||
|
||||
In a linked zone a difference is a view's own (local) records, on
|
||||
purpose; between zones that aren't linked it is drift. 'jarl record
|
||||
update ID --shared' makes a linked view's version every view's.`,
|
||||
Example: ` jarl zone diff adbraun.lol
|
||||
jarl zone diff adbraun.lol --view internal --view external --all`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
jarl zone diff adbraun.lol --view internal --with external
|
||||
jarl zone diff adbraun.lol nas
|
||||
jarl zone diff adbraun.lol @ TXT --wide`,
|
||||
Args: cobra.RangeArgs(1, 3),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := newClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
path, qv := zonePath(args[0], "")
|
||||
if len(views) > 0 {
|
||||
qv.Set("views", strings.Join(views, ","))
|
||||
path, qv := zonePath(args[0], view)
|
||||
if len(with) > 0 {
|
||||
qv.Set("with", strings.Join(with, ","))
|
||||
}
|
||||
if len(args) > 1 {
|
||||
qv.Set("name", args[1])
|
||||
}
|
||||
if len(args) > 2 {
|
||||
qv.Set("type", args[2])
|
||||
}
|
||||
if all {
|
||||
qv.Set("all", "1")
|
||||
}
|
||||
var r struct {
|
||||
Zone string `json:"zone"`
|
||||
Views []string `json:"views"`
|
||||
Linked bool `json:"linked"`
|
||||
Same int `json:"same"`
|
||||
Rows []struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Same bool `json:"same"`
|
||||
Views []struct {
|
||||
Records []string `json:"records"`
|
||||
IDs []int64 `json:"ids"`
|
||||
Linked string `json:"linked"`
|
||||
} `json:"views"`
|
||||
} `json:"rows"`
|
||||
}
|
||||
var r compareReport
|
||||
if err := c.Get(ctx(), path+"/compare", qv, &r); err != nil {
|
||||
return err
|
||||
}
|
||||
return output(r, func(w io.Writer) {
|
||||
head := []string{"NAME", "TYPE"}
|
||||
for _, v := range r.Views {
|
||||
head = append(head, strings.ToUpper(v))
|
||||
}
|
||||
fmt.Fprintln(w, strings.Join(head, "\t"))
|
||||
for _, row := range r.Rows {
|
||||
cols := []string{row.Name, row.Type}
|
||||
for _, vs := range row.Views {
|
||||
cell := "-"
|
||||
if len(vs.Records) > 0 {
|
||||
cell = strings.Join(vs.Records, ", ")
|
||||
if vs.Linked == "local" {
|
||||
cell += " [local, id " + fmt.Sprint(vs.IDs[0]) + "]"
|
||||
}
|
||||
}
|
||||
cols = append(cols, cell)
|
||||
}
|
||||
fmt.Fprintln(w, strings.Join(cols, "\t"))
|
||||
}
|
||||
link := "not linked (jarl zone link " + r.Zone + ")"
|
||||
if r.Linked {
|
||||
link = "linked"
|
||||
}
|
||||
fmt.Fprintf(w, "\n%d names and types differ, %d are the same; %s\n", len(r.Rows)-boolCount(all, r.Same), r.Same, link)
|
||||
})
|
||||
width := 0
|
||||
if !wide {
|
||||
width = terminalWidth()
|
||||
}
|
||||
return output(r, func(w io.Writer) { printCompare(w, &r, len(args) > 1, width) })
|
||||
},
|
||||
}
|
||||
cmd.Flags().StringArrayVar(&views, "view", nil, "a view to compare (repeat; default: every view with the zone)")
|
||||
cmd.Flags().BoolVar(&all, "all", false, "also show what is the same everywhere")
|
||||
cmd.Flags().StringVar(&view, "view", "", "the view to compare from (default: the first that has the zone)")
|
||||
cmd.Flags().StringArrayVar(&with, "with", nil, "a view to compare with (repeat; default: every other view with the zone)")
|
||||
cmd.Flags().BoolVar(&all, "all", false, "also list what is the same")
|
||||
cmd.Flags().BoolVarP(&wide, "wide", "w", false, "show long values whole instead of cutting them to the terminal")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func boolCount(b bool, n int) int {
|
||||
if b {
|
||||
return n
|
||||
// printCompare prints a comparison as sections, one line per record,
|
||||
// each cut to width characters (0: whole).
|
||||
func printCompare(w io.Writer, r *compareReport, oneName bool, width int) {
|
||||
vw := 0 // the views column
|
||||
for _, v := range r.Views {
|
||||
vw = max(vw, len(v))
|
||||
}
|
||||
return 0
|
||||
line := func(indent, label, text string) {
|
||||
s := indent + label + text
|
||||
if width > 0 && utf8.RuneCountInString(s) > width {
|
||||
keep := max(width-utf8.RuneCountInString(indent+label)-1, 8)
|
||||
text = string([]rune(text)[:min(keep, utf8.RuneCountInString(text))]) + "…"
|
||||
}
|
||||
fmt.Fprintln(w, indent+label+text)
|
||||
}
|
||||
link := "not linked, so a difference may have crept in ('jarl zone link " + r.Zone + "' keeps them in step)"
|
||||
if r.Linked {
|
||||
link = "linked: a difference is a view's own records, there on purpose"
|
||||
}
|
||||
fmt.Fprintf(w, "%s in %s, compared with %s\n %s\n", r.Zone, r.Base, strings.Join(r.Views[1:], ", "), link)
|
||||
if !oneName {
|
||||
for _, s := range r.Summary {
|
||||
fmt.Fprintf(w, " %s: %d answered differently, %d only in %s, %d only in %s, %d the same\n", s.View, s.Different, s.OnlyBase, r.Base, s.OnlyOther, s.View, s.Same)
|
||||
}
|
||||
}
|
||||
name := func(row compareRow) string { return row.Name + " " + row.Type }
|
||||
sets := func(row compareRow, local bool) {
|
||||
for _, vs := range row.Views {
|
||||
label := fmt.Sprintf(" %-*s ", vw, vs.View)
|
||||
if len(vs.Records) == 0 {
|
||||
line("", label, "-")
|
||||
continue
|
||||
}
|
||||
for i, d := range vs.Records {
|
||||
if i > 0 {
|
||||
label = strings.Repeat(" ", 4+vw+2)
|
||||
}
|
||||
suffix := ""
|
||||
if local && vs.Linked == "local" && i == 0 {
|
||||
suffix = " [local, id " + fmt.Sprint(vs.IDs[0]) + "]"
|
||||
}
|
||||
line("", label, d+suffix)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(r.Different) > 0 {
|
||||
fmt.Fprintf(w, "\nANSWERED DIFFERENTLY (%d)\n", len(r.Different))
|
||||
for _, row := range r.Different {
|
||||
fmt.Fprintln(w, " "+name(row))
|
||||
sets(row, true)
|
||||
}
|
||||
}
|
||||
for _, o := range r.Only {
|
||||
fmt.Fprintf(w, "\nONLY IN %s (%d)\n", strings.ToUpper(strings.Join(o.Views, ", ")), len(o.Rows))
|
||||
nw := 0
|
||||
for _, row := range o.Rows {
|
||||
nw = max(nw, len(name(row)))
|
||||
}
|
||||
for _, row := range o.Rows {
|
||||
var recs []string
|
||||
for _, vs := range row.Views {
|
||||
if len(vs.Records) > 0 {
|
||||
recs = vs.Records
|
||||
break
|
||||
}
|
||||
}
|
||||
for i, d := range recs {
|
||||
label := fmt.Sprintf(" %-*s ", nw, name(row))
|
||||
if i > 0 {
|
||||
label = strings.Repeat(" ", 2+nw+2)
|
||||
}
|
||||
line("", label, d)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(r.SameRows) > 0 {
|
||||
fmt.Fprintf(w, "\nTHE SAME IN %s (%d)\n", strings.ToUpper(strings.Join(r.Views, ", ")), len(r.SameRows))
|
||||
nw := 0
|
||||
for _, row := range r.SameRows {
|
||||
nw = max(nw, len(name(row)))
|
||||
}
|
||||
for _, row := range r.SameRows {
|
||||
for i, d := range row.Views[0].Records {
|
||||
label := fmt.Sprintf(" %-*s ", nw, name(row))
|
||||
if i > 0 {
|
||||
label = strings.Repeat(" ", 2+nw+2)
|
||||
}
|
||||
line("", label, d)
|
||||
}
|
||||
}
|
||||
} else if r.Same > 0 && !oneName {
|
||||
fmt.Fprintf(w, "\n%d names and types are the same (--all lists them)\n", r.Same)
|
||||
}
|
||||
if oneName && len(r.Different) == 0 && len(r.Only) == 0 && len(r.SameRows) == 0 {
|
||||
fmt.Fprintln(w, "\nNo records of that name in these views.")
|
||||
}
|
||||
}
|
||||
|
||||
// terminalWidth returns the terminal's width, or 0 when standard output
|
||||
// isn't a terminal (then nothing is cut).
|
||||
func terminalWidth() int {
|
||||
fd := int(os.Stdout.Fd())
|
||||
if !term.IsTerminal(fd) {
|
||||
return 0
|
||||
}
|
||||
width, _, err := term.GetSize(fd)
|
||||
if err != nil || width <= 0 {
|
||||
return 0
|
||||
}
|
||||
return width
|
||||
}
|
||||
|
||||
func zoneFillPTRCmd() *cobra.Command {
|
||||
|
||||
Reference in new issue
Block a user