feat: Output parsing basics

pull/2/head
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)
}
}
}

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/mr-karan/doggo
go 1.15
require (
github.com/fatih/color v1.10.0
github.com/miekg/dns v1.1.35
github.com/sirupsen/logrus v1.7.0
github.com/urfave/cli/v2 v2.3.0

9
go.sum
View File

@ -3,6 +3,12 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSY
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/miekg/dns v1.1.35 h1:oTfOaDH+mZkdcgdIjH6yBajRGtIwcwcaR+rt23ZSrJs=
github.com/miekg/dns v1.1.35/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@ -33,6 +39,9 @@ golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe h1:6fAMxZRR6sl1Uq8U61gxU+kPT
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=