Skip to content

Commit c316b9a

Browse files
chore: gofmt
Signed-off-by: Bruno Schaatsbergen <[email protected]>
1 parent 265b066 commit c316b9a

File tree

2 files changed

+65
-62
lines changed

2 files changed

+65
-62
lines changed

cmd/printer.go

+61-62
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,109 @@
11
package cmd
22

33
import (
4-
"fmt"
5-
"os"
6-
"strconv"
7-
"strings"
8-
"time"
9-
10-
"github.com/miekg/dns"
11-
"github.com/fatih/color"
12-
"github.com/juju/ansiterm"
4+
"fmt"
5+
"os"
6+
"strconv"
7+
"strings"
8+
"time"
9+
10+
"github.com/fatih/color"
11+
"github.com/juju/ansiterm"
12+
"github.com/miekg/dns"
1313
)
1414

1515
// printRecords prints the DNS records to the terminal.
1616
func printRecords(domainName string, messages []*dns.Msg) {
17-
w := ansiterm.NewTabWriter(os.Stdout, 8, 8, 4, ' ', 0)
18-
w.SetColorCapable(true)
17+
w := ansiterm.NewTabWriter(os.Stdout, 8, 8, 4, ' ', 0)
18+
w.SetColorCapable(true)
1919

20-
domainColored := color.HiBlueString(domainName)
20+
domainColored := color.HiBlueString(domainName)
2121

22-
for _, msg := range messages {
23-
for _, answer := range msg.Answer {
24-
queryType := dns.TypeToString[answer.Header().Rrtype]
25-
formattedTTL := color.HiMagentaString(formatTTL(answer.Header().Ttl))
22+
for _, msg := range messages {
23+
for _, answer := range msg.Answer {
24+
queryType := dns.TypeToString[answer.Header().Rrtype]
25+
formattedTTL := color.HiMagentaString(formatTTL(answer.Header().Ttl))
2626

27-
switch rec := answer.(type) {
28-
case *dns.A:
29-
printARecord(w, queryType, domainColored, formattedTTL, rec)
27+
switch rec := answer.(type) {
28+
case *dns.A:
29+
printARecord(w, queryType, domainColored, formattedTTL, rec)
3030

31-
case *dns.AAAA:
32-
printAAAARecord(w, queryType, domainColored, formattedTTL, rec)
31+
case *dns.AAAA:
32+
printAAAARecord(w, queryType, domainColored, formattedTTL, rec)
3333

34-
case *dns.CNAME:
35-
printCNAMERecord(w, queryType, domainColored, formattedTTL, rec)
34+
case *dns.CNAME:
35+
printCNAMERecord(w, queryType, domainColored, formattedTTL, rec)
3636

37-
case *dns.MX:
38-
printMXRecord(w, queryType, domainColored, formattedTTL, rec)
37+
case *dns.MX:
38+
printMXRecord(w, queryType, domainColored, formattedTTL, rec)
3939

40-
case *dns.TXT:
41-
printTXTRecord(w, queryType, domainColored, formattedTTL, rec)
40+
case *dns.TXT:
41+
printTXTRecord(w, queryType, domainColored, formattedTTL, rec)
4242

43-
case *dns.NS:
44-
printNSRecord(w, queryType, domainColored, formattedTTL, rec)
43+
case *dns.NS:
44+
printNSRecord(w, queryType, domainColored, formattedTTL, rec)
4545

46-
case *dns.SOA:
47-
printSOARecord(w, queryType, domainColored, formattedTTL, rec)
46+
case *dns.SOA:
47+
printSOARecord(w, queryType, domainColored, formattedTTL, rec)
4848

49-
case *dns.PTR:
50-
printPTRRecord(w, queryType, domainColored, formattedTTL, rec)
49+
case *dns.PTR:
50+
printPTRRecord(w, queryType, domainColored, formattedTTL, rec)
5151

52-
default:
53-
fmt.Fprintf(os.Stderr, "Unknown record type: %s\n", queryType)
54-
}
55-
}
56-
}
52+
default:
53+
fmt.Fprintf(os.Stderr, "Unknown record type: %s\n", queryType)
54+
}
55+
}
56+
}
5757

58-
w.Flush()
58+
w.Flush()
5959
}
6060

6161
func printARecord(w *ansiterm.TabWriter, queryType, domain, ttl string, rec *dns.A) {
62-
fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.A.String()))
62+
fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.A.String()))
6363
}
6464

6565
func printAAAARecord(w *ansiterm.TabWriter, queryType, domain, ttl string, rec *dns.AAAA) {
66-
fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.AAAA.String()))
66+
fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.AAAA.String()))
6767
}
6868

6969
func printCNAMERecord(w *ansiterm.TabWriter, queryType, domain, ttl string, rec *dns.CNAME) {
70-
fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.Target))
70+
fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.Target))
7171
}
7272

7373
func printMXRecord(w *ansiterm.TabWriter, queryType, domain, ttl string, rec *dns.MX) {
74-
preference := color.HiRedString(strconv.FormatUint(uint64(rec.Preference), 10))
75-
fmt.Fprintf(w, "%s\t%s.\t%s\t%s %s\n", color.HiYellowString(queryType), domain, ttl, preference, color.HiWhiteString(rec.Mx))
74+
preference := color.HiRedString(strconv.FormatUint(uint64(rec.Preference), 10))
75+
fmt.Fprintf(w, "%s\t%s.\t%s\t%s %s\n", color.HiYellowString(queryType), domain, ttl, preference, color.HiWhiteString(rec.Mx))
7676
}
7777

7878
func printTXTRecord(w *ansiterm.TabWriter, queryType, domain, ttl string, rec *dns.TXT) {
79-
txtJoined := strings.Join(rec.Txt, " ")
80-
fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(txtJoined))
79+
txtJoined := strings.Join(rec.Txt, " ")
80+
fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(txtJoined))
8181
}
8282

8383
func printNSRecord(w *ansiterm.TabWriter, queryType, domain, ttl string, rec *dns.NS) {
84-
fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.Ns))
84+
fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.Ns))
8585
}
8686

8787
func printSOARecord(w *ansiterm.TabWriter, queryType, domain, ttl string, rec *dns.SOA) {
88-
primaryNameServer := color.HiRedString(rec.Ns)
89-
fmt.Fprintf(w, "%s\t%s.\t%s\t%s %s\n", color.HiYellowString(queryType), domain, ttl, primaryNameServer, rec.Mbox)
88+
primaryNameServer := color.HiRedString(rec.Ns)
89+
fmt.Fprintf(w, "%s\t%s.\t%s\t%s %s\n", color.HiYellowString(queryType), domain, ttl, primaryNameServer, rec.Mbox)
9090
}
9191

9292
func printPTRRecord(w *ansiterm.TabWriter, queryType, domain, ttl string, rec *dns.PTR) {
93-
fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.Ptr))
93+
fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.Ptr))
9494
}
9595

9696
// formatTTL converts TTL to a more readable format (hours, minutes, seconds).
9797
func formatTTL(ttl uint32) string {
98-
duration := time.Duration(ttl) * time.Second
99-
hours := int(duration.Hours())
100-
minutes := int(duration.Minutes()) % 60
101-
seconds := int(duration.Seconds()) % 60
102-
103-
if hours > 0 {
104-
return fmt.Sprintf("%02dh%02dm%02ds", hours, minutes, seconds)
105-
} else if minutes > 0 {
106-
return fmt.Sprintf("%02dm%02ds", minutes, seconds)
107-
}
108-
return fmt.Sprintf("%02ds", seconds)
98+
duration := time.Duration(ttl) * time.Second
99+
hours := int(duration.Hours())
100+
minutes := int(duration.Minutes()) % 60
101+
seconds := int(duration.Seconds()) % 60
102+
103+
if hours > 0 {
104+
return fmt.Sprintf("%02dh%02dm%02ds", hours, minutes, seconds)
105+
} else if minutes > 0 {
106+
return fmt.Sprintf("%02dm%02ds", minutes, seconds)
107+
}
108+
return fmt.Sprintf("%02ds", seconds)
109109
}
110-

internal/query/query.go

+4
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,19 @@ func NewQuerier(server string, logger hclog.Logger) Querier {
4242
func (q *Query) MultiQuery(domain string, qtypes []uint16) ([]*dns.Msg, error) {
4343
var errors *multierror.Error
4444
var wg sync.WaitGroup
45+
var mu sync.Mutex
46+
4547
messages := make([]*dns.Msg, len(qtypes))
4648

4749
for i, qtype := range qtypes {
4850
wg.Add(1)
4951
go func(i int, qtype uint16) {
5052
defer wg.Done()
5153
msg, err := q.Query(domain, qtype)
54+
mu.Lock()
5255
messages[i] = msg
5356
errors = multierror.Append(errors, err)
57+
mu.Unlock()
5458
}(i, qtype)
5559
}
5660

0 commit comments

Comments
 (0)