doggo/cmd/output.go

50 lines
1.3 KiB
Go
Raw Normal View History

2020-12-12 11:57:13 +01:00
package main
2020-12-12 15:10:28 +01:00
import (
"fmt"
2020-12-13 04:17:07 +01:00
"os"
2020-12-12 15:10:28 +01:00
"strconv"
"github.com/fatih/color"
"github.com/miekg/dns"
"github.com/mr-karan/doggo/pkg/resolvers"
2020-12-13 04:17:07 +01:00
"github.com/olekukonko/tablewriter"
2020-12-12 15:10:28 +01:00
)
2020-12-12 11:57:13 +01:00
// Output takes a list of `dns.Answers` and based
// on the output format specified displays the information.
2020-12-12 15:10:28 +01:00
func (hub *Hub) Output(responses []resolvers.Response) {
// Create SprintXxx functions to mix strings with other non-colorized strings:
green := color.New(color.FgGreen).SprintFunc()
blue := color.New(color.FgBlue).SprintFunc()
2020-12-13 04:17:07 +01:00
table := tablewriter.NewWriter(os.Stdout)
header := []string{"Name", "Type", "Class", "TTL", "Address"}
if hub.QueryFlags.DisplayTimeTaken {
header = append(header, "Time Taken")
}
table.SetHeader(header)
2020-12-12 15:10:28 +01:00
for _, r := range responses {
var res string
for _, a := range r.Message.Answer {
switch t := a.(type) {
case *dns.A:
res = t.A.String()
}
h := a.Header()
name := green(h.Name)
qclass := dns.Class(h.Class).String()
ttl := strconv.FormatInt(int64(h.Ttl), 10) + "s"
qtype := blue(dns.Type(h.Rrtype).String())
2020-12-13 04:17:07 +01:00
output := []string{name, qtype, qclass, ttl, res}
// Print how long it took
if hub.QueryFlags.DisplayTimeTaken {
output = append(output, fmt.Sprintf("%dms", r.RTT.Milliseconds()))
}
table.Append(output)
2020-12-12 15:10:28 +01:00
}
}
2020-12-13 04:17:07 +01:00
table.Render()
2020-12-12 15:10:28 +01:00
}