dns/show: refactor Record formatting
Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
+32
-8
@@ -1,29 +1,53 @@
|
|||||||
package dns
|
package dns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"darvaza.org/core"
|
"darvaza.org/core"
|
||||||
|
"github.com/libdns/libdns"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, rr := range recs {
|
for _, rr := range recs {
|
||||||
_, _ = fmt.Printf("%s\t%v\tIN\t%s\t%s\t; %s\n",
|
_ = fmtRecord(&buf, rr)
|
||||||
rr.Name,
|
_, _ = buf.WriteRune('\n')
|
||||||
int(rr.TTL/time.Second),
|
}
|
||||||
rr.Type,
|
_, _ = fmt.Fprintf(&buf, "; %v records\n", len(recs))
|
||||||
rr.Value,
|
|
||||||
rr.ID)
|
_, err = buf.WriteTo(os.Stdout)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func fmtRecord(w io.Writer, rr libdns.Record) error {
|
||||||
|
ttl := int(rr.TTL / time.Second)
|
||||||
|
if ttl < 1 {
|
||||||
|
ttl = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _ = fmt.Printf("; %v records\n", len(recs))
|
_, err := fmt.Fprintf(w, "%s\t%v\tIN\t%s\t%s",
|
||||||
return nil
|
rr.Name,
|
||||||
|
ttl,
|
||||||
|
rr.Type,
|
||||||
|
rr.Value)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
if rr.ID != "" {
|
||||||
|
_, err = fmt.Fprintf(w, "\t; %s", rr.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user