feat: Output parsing basics

This commit is contained in:
Karan Sharma 2020-12-12 19:40:28 +05:30
parent a7268f578f
commit bcc405db1f
4 changed files with 41 additions and 18 deletions

View file

@ -1,7 +1,6 @@
package main
import (
"fmt"
"strings"
"github.com/miekg/dns"
@ -13,16 +12,9 @@ func (hub *Hub) Lookup(c *cli.Context) error {
hub.prepareQuestions()
responses, err := hub.Resolver.Lookup(hub.Questions)
if err != nil {
hub.Logger.Error(err)
return err
}
for _, r := range responses {
for _, a := range r.Message.Answer {
if t, ok := a.(*dns.A); ok {
fmt.Println(t.String())
}
}
}
hub.Output(responses)
return nil
}

View file

@ -1,12 +1,33 @@
package main
import (
"fmt"
"strconv"
"github.com/fatih/color"
"github.com/miekg/dns"
"github.com/mr-karan/doggo/pkg/resolvers"
)
// Output takes a list of `dns.Answers` and based
// on the output format specified displays the information.
// func (hub *Hub) Output(c *cli.Context) error {
// hub.prepareQuestions()
// answers, err := hub.Resolver.Lookup(hub.Questions)
// if err != nil {
// hub.Logger.Error(err)
// }
// return nil
// }
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()
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())
fmt.Printf("%s \t %s \t %s \t %s \t %s\n", qtype, name, qclass, ttl, res)
}
}
}