Skip to content

Commit 22a18c0

Browse files
implement querier and add comments to satisfy linters
Signed-off-by: Bruno Schaatsbergen <[email protected]>
1 parent 12f4b15 commit 22a18c0

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

cmd/root.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66

7+
"github.com/hashicorp/go-multierror"
78
"github.com/spf13/cobra"
89
"github.com/znscli/zns/internal/query"
910
)
@@ -23,6 +24,30 @@ var (
2324
fmt.Println("See 'zns -h' for help and examples")
2425
os.Exit(1)
2526
}
27+
28+
// Initialize a new querier with the specified DNS server
29+
querier := query.NewQuerier("1.1.1.1:53", nil)
30+
31+
qtypes := make([]uint16, 0, len(query.QueryTypes))
32+
for _, qtype := range query.QueryTypes {
33+
qtypes = append(qtypes, qtype)
34+
}
35+
36+
messages, err := querier.QueryTypes(args[0], qtypes)
37+
if err != nil {
38+
if merr, ok := err.(*multierror.Error); ok {
39+
for _, e := range merr.Errors {
40+
fmt.Println(e)
41+
}
42+
} else { // This should never happen since all errors in zns are multierrors, but we handle it anyway.
43+
fmt.Println(err)
44+
}
45+
os.Exit(1)
46+
}
47+
48+
for _, msg := range messages {
49+
fmt.Println(msg)
50+
}
2651
},
2752
}
2853
)
@@ -38,4 +63,3 @@ func Execute() {
3863
os.Exit(1)
3964
}
4065
}
41-

internal/query/query.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@ var (
2222
}
2323
)
2424

25-
// Querier is an interface for querying DNS records
25+
// Querier is an interface for querying DNS records.
2626
type Querier interface {
2727
Query(domain string, qtype uint16) (*dns.Msg, error)
28+
QueryTypes(domain string, qtypes []uint16) ([]*dns.Msg, error)
2829
}
2930

30-
// Query implements the Querier interface
31+
// Query implements the Querier interface.
3132
type Query struct {
3233
Server string
3334
hclog.Logger
3435
}
3536

36-
// NewQuerier creates a new Querier with the specified server
37+
// NewQuerier creates a new Querier with the specified server.
3738
func NewQuerier(server string, logger hclog.Logger) Querier {
3839
if logger == nil {
3940
logger = hclog.New(&hclog.LoggerOptions{
@@ -47,7 +48,7 @@ func NewQuerier(server string, logger hclog.Logger) Querier {
4748
return &Query{Server: server, Logger: logger}
4849
}
4950

50-
// QueryTypes performs DNS queries for multiple types concurrently
51+
// QueryTypes performs DNS queries for multiple types concurrently.
5152
func (q *Query) QueryTypes(domain string, qtypes []uint16) ([]*dns.Msg, error) {
5253
var errors *multierror.Error
5354
var wg sync.WaitGroup
@@ -68,7 +69,7 @@ func (q *Query) QueryTypes(domain string, qtypes []uint16) ([]*dns.Msg, error) {
6869
return messages, errors.ErrorOrNil()
6970
}
7071

71-
// Query performs the DNS query and returns the response and any error encountered
72+
// Query performs the DNS query and returns the response and any error encountered.
7273
func (q *Query) Query(domain string, qtype uint16) (*dns.Msg, error) {
7374
msg := new(dns.Msg)
7475
msg.SetQuestion(dns.Fqdn(domain), qtype)

0 commit comments

Comments
 (0)