Skip to content

Commit

Permalink
feat: Add status for non NOERROR responses
Browse files Browse the repository at this point in the history
Closes #5
  • Loading branch information
mr-karan committed Dec 19, 2020
1 parent 90fee87 commit 8d1b6ad
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@
- [ ] Mkdocs init project
- [ ] Custom Index (Landing Page)
- [ ] Homebrew - Goreleaser
- [ ] Separate Authority/Answer in JSON output.
1 change: 0 additions & 1 deletion cmd/doggo/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func main() {

// Configure Flags.
f := flag.NewFlagSet("config", flag.ContinueOnError)
hub.flag = f

// Custom Help Text.
f.Usage = renderCustomHelp
Expand Down
3 changes: 0 additions & 3 deletions cmd/doggo/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/miekg/dns"
"github.com/mr-karan/doggo/pkg/resolvers"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
)

// Hub represents the structure for all app wide configuration.
Expand All @@ -18,7 +17,6 @@ type Hub struct {
Questions []dns.Question
Resolver []resolvers.Resolver
Nameservers []Nameserver
flag *pflag.FlagSet
}

// QueryFlags is used store the query params
Expand All @@ -36,7 +34,6 @@ type QueryFlags struct {
Ndots int `koanf:"ndots"`
Color bool `koanf:"color"`
Timeout time.Duration `koanf:"timeout"`
isNdotsSet bool
}

// Nameserver represents the type of Nameserver
Expand Down
7 changes: 6 additions & 1 deletion cmd/doggo/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import (
// to all resolvers. It returns a list of []resolver.Response from
// each resolver
func (hub *Hub) Lookup() ([][]resolvers.Response, error) {
// check if ndots is 0 (that means it's not supplied by user)
if hub.QueryFlags.Ndots == 0 {
// set the default as 1
hub.QueryFlags.Ndots = 1
}
questions, err := hub.prepareQuestions()
if err != nil {
return nil, err
Expand Down Expand Up @@ -79,7 +84,7 @@ func (hub *Hub) prepareQuestions() ([]dns.Question, error) {
func fetchDomainList(d string, ndots int) ([]string, error) {
if runtime.GOOS == "windows" {
// TODO: Add a method for reading system default nameserver in windows.
return []string{d}, nil
return []string{dns.Fqdn(d)}, nil
}
cfg, err := dns.ClientConfigFromFile(DefaultResolvConfPath)
if err != nil {
Expand Down
18 changes: 16 additions & 2 deletions cmd/doggo/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Output struct {
Address string `json:"address"`
TimeTaken string `json:"rtt"`
Nameserver string `json:"nameserver"`
Status string `json:"status"`
}

type Query struct {
Expand Down Expand Up @@ -68,6 +69,7 @@ func (hub *Hub) outputTerminal(out []Output) {
yellow := color.New(color.FgYellow, color.Bold).SprintFunc()
cyan := color.New(color.FgCyan, color.Bold).SprintFunc()
red := color.New(color.FgRed, color.Bold).SprintFunc()
magenta := color.New(color.FgMagenta, color.Bold).SprintFunc()

if !hub.QueryFlags.Color {
color.NoColor = true // disables colorized output
Expand All @@ -78,8 +80,16 @@ func (hub *Hub) outputTerminal(out []Output) {
if hub.QueryFlags.DisplayTimeTaken {
header = append(header, "Time Taken")
}
outputStatus := false
for _, o := range out {
if dns.StringToRcode[o.Status] != dns.RcodeSuccess {
header = append(header, "Status")
outputStatus = true
}
}

table.SetHeader(header)
table.SetAutoWrapText(false)
table.SetAutoWrapText(true)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
Expand All @@ -99,7 +109,7 @@ func (hub *Hub) outputTerminal(out []Output) {
case "AAAA":
typOut = blue(o.Type)
case "MX":
typOut = red(o.Type)
typOut = magenta(o.Type)
case "NS":
typOut = cyan(o.Type)
case "CNAME":
Expand All @@ -116,6 +126,9 @@ func (hub *Hub) outputTerminal(out []Output) {
if hub.QueryFlags.DisplayTimeTaken {
output = append(output, o.TimeTaken)
}
if outputStatus {
output = append(output, red(o.Status))
}
table.Append(output)
}
table.Render()
Expand Down Expand Up @@ -166,6 +179,7 @@ func collectOutput(responses [][]resolvers.Response) []Output {
Address: addr,
TimeTaken: rtt,
Nameserver: r.Nameserver,
Status: dns.RcodeToString[r.Message.Rcode],
}
out = append(out, o)
}
Expand Down
2 changes: 0 additions & 2 deletions cmd/doggo/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ func (hub *Hub) loadQueryArgs() error {
if err != nil {
return err
}
// check if ndots is set
hub.QueryFlags.isNdotsSet = isFlagPassed("ndots", hub.flag)

// Load all fallbacks in internal query flags.
hub.loadFallbacks()
Expand Down

0 comments on commit 8d1b6ad

Please sign in to comment.