Compare commits

..

2 Commits

Author SHA1 Message Date
amery 9da49f2d86 dns/show: sort records
v2: change Name to lower case and Type to upper case before comparing

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-26 18:47:29 +00:00
amery 356322bc94 dns/show: introduce writeRecords() helper
to print a whole []libdns.Record

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-26 18:41:32 +00:00
2 changed files with 18 additions and 8 deletions
+12 -5
View File
@@ -6,6 +6,7 @@ import (
"io" "io"
"net/netip" "net/netip"
"sort" "sort"
"strings"
"time" "time"
"darvaza.org/core" "darvaza.org/core"
@@ -47,19 +48,25 @@ func SortRecords(s []libdns.Record) []libdns.Record {
} }
func lessRecord(a, b libdns.Record) bool { func lessRecord(a, b libdns.Record) bool {
aName := strings.ToLower(a.Name)
bName := strings.ToLower(b.Name)
switch { switch {
case a.Name < b.Name: case aName < bName:
return true return true
case a.Name > b.Name: case aName > bName:
return false return false
} }
aType := strings.ToUpper(a.Type)
bType := strings.ToUpper(b.Type)
switch { switch {
case a.Type < b.Type: case aType < bType:
return true return true
case a.Type > b.Type: case aType > bType:
return false return false
case a.Type == "A", a.Type == "AAAA": case aType == "A", aType == "AAAA":
// IP Addresses // IP Addresses
var aa, ba netip.Addr var aa, ba netip.Addr
+6 -3
View File
@@ -14,14 +14,17 @@ import (
// Show shows current DNS entries // Show shows current DNS entries
func (mgr *Manager) Show(ctx context.Context, names ...string) error { func (mgr *Manager) Show(ctx context.Context, names ...string) error {
var buf bytes.Buffer
recs, err := mgr.GetRecords(ctx, names...) recs, err := mgr.GetRecords(ctx, names...)
if err != nil { if err != nil {
return core.Wrap(err, "GetRecords") return core.Wrap(err, "GetRecords")
} }
SortRecords(recs) SortRecords(recs)
return writeRecords(recs, os.Stdout)
}
func writeRecords(recs []libdns.Record, w io.Writer) error {
var buf bytes.Buffer
for _, rr := range recs { for _, rr := range recs {
_ = fmtRecord(&buf, rr) _ = fmtRecord(&buf, rr)
@@ -29,7 +32,7 @@ func (mgr *Manager) Show(ctx context.Context, names ...string) error {
} }
_, _ = fmt.Fprintf(&buf, "; %v records\n", len(recs)) _, _ = fmt.Fprintf(&buf, "; %v records\n", len(recs))
_, err = buf.WriteTo(os.Stdout) _, err := buf.WriteTo(w)
return err return err
} }