Skip to content

Commit

Permalink
fix: standardize structured output
Browse files Browse the repository at this point in the history
  • Loading branch information
natesales committed Dec 13, 2023
1 parent 5c65bee commit c2884c1
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 32 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/ameshkov/dnscrypt/v2 v2.2.7
github.com/jedisct1/go-dnsstamps v0.0.0-20230211133001-124a632de565
github.com/jessevdk/go-flags v1.5.0
github.com/json-iterator/go v1.1.12
github.com/miekg/dns v1.1.57
github.com/natesales/bgptools-go v0.0.0-20230212051756-2b519d61269c
github.com/quic-go/quic-go v0.40.0
Expand All @@ -27,6 +28,8 @@ require (
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/google/pprof v0.0.0-20231212022811-ec68065c825e // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/onsi/ginkgo/v2 v2.13.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,25 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/pprof v0.0.0-20231212022811-ec68065c825e h1:bwOy7hAFd0C91URzMIEBfr6BAz29yk7Qj0cy6S7DJlU=
github.com/google/pprof v0.0.0-20231212022811-ec68065c825e/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
github.com/jedisct1/go-dnsstamps v0.0.0-20230211133001-124a632de565 h1:BPBMaUCgtmiHvqgugbSuegXjADJfERsPbmRqgdq8Pjo=
github.com/jedisct1/go-dnsstamps v0.0.0-20230211133001-124a632de565/go.mod h1:mEGEFZsGe4sG5Mb3Xi89pmsy+TZ0946ArbYMGKAM5uA=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/miekg/dns v1.1.57 h1:Jzi7ApEIzwEPLHWRcafCN9LZSBbqQpxjt/wpgvg7wcM=
github.com/miekg/dns v1.1.57/go.mod h1:uqRjCRUuEAA6qsOiJvDd+CFo/vW+y5WR6SNmHE55hZk=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/natesales/bgptools-go v0.0.0-20230212051756-2b519d61269c h1:bblm7D7Ld1/zkWvMU1j60lMk5h/F4AiKW23j1yffM5Y=
github.com/natesales/bgptools-go v0.0.0-20230212051756-2b519d61269c/go.mod h1:jl8YnQACciyOXRgNIRhURrCF9FmRHjnfT8UCj3LBkyY=
github.com/onsi/ginkgo/v2 v2.13.2 h1:Bi2gGVkfn6gQcjNjZJVO8Gf0FHzMPf2phUei9tejVMs=
Expand All @@ -63,6 +70,7 @@ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs
github.com/sthorne/odoh-go v1.0.4 h1:RPJceVs/dIpNE3gyzk6wdSay+nR+tI1YXBUwykvCEXI=
github.com/sthorne/odoh-go v1.0.4/go.mod h1:KdB/NGiepr9bLVs3k26uWl4HHPHqa2DaoPUgUfKNmJU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ All long form (--) flags can be toggled with the dig-standard +[no]flag notation
}

entries = append(entries, &output.Entry{
Queries: msgs,
Replies: replies,
Server: server,
Txp: txp,
Expand Down
10 changes: 6 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ func TestMainJSONFormat(t *testing.T) {
"--format=json",
)
assert.Nil(t, err)
assert.Contains(t, out.String(), `"Preference":0,"Mx":"."`)
assert.Contains(t, out.String(), `"Ns":"a.iana-servers.net."`)
assert.Contains(t, out.String(), `"Txt":["v=spf1 -all"]`)
o := strings.ReplaceAll(out.String(), `\\"`, `"`)
assert.Contains(t, o, `"preference":0,"mx":"."`)
assert.Contains(t, o, `"ns":"a.iana-servers.net."`)
assert.Contains(t, o, `"txt":["v=spf1 -all"`)
}

func TestMainInvalidOutputFormat(t *testing.T) {
Expand Down Expand Up @@ -200,7 +201,8 @@ func TestMainPad(t *testing.T) {
"--format=json",
)
assert.Nil(t, err)
assert.Contains(t, out.String(), `"Truncated":false`)
o := strings.ReplaceAll(out.String(), `\\"`, `"`)
assert.Contains(t, o, `"truncated":false`)
}

func TestMainChaosClass(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Printer struct {

// Entry stores the replies from a server
type Entry struct {
Queries []dns.Msg
Replies []*dns.Msg
Server string

Expand Down
38 changes: 10 additions & 28 deletions output/structured.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,27 @@
package output

import (
"encoding/json"
"time"
"strings"

"github.com/miekg/dns"
jsoniter "github.com/json-iterator/go"
"github.com/json-iterator/go/extra"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"

"github.com/natesales/q/util"
)

type reply struct {
Server string
QueryTime int64
Answers []dns.RR
ID uint16
Truncated bool
}

func (p Printer) PrintStructured(entries []*Entry) {
out := make([]reply, 0)
for _, entry := range entries {
for _, r := range entry.Replies {
out = append(out, reply{
Server: entry.Server,
QueryTime: int64(entry.Time.Round(time.Millisecond)),
Answers: r.Answer,
ID: r.Id,
Truncated: r.Truncated,
})
}
}

var b []byte
var err error
var marshaler func(any) ([]byte, error)
if p.Opts.Format == "json" {
b, err = json.Marshal(out)
extra.SetNamingStrategy(strings.ToLower)
json := jsoniter.ConfigCompatibleWithStandardLibrary
marshaler = json.Marshal
} else { // yaml
b, err = yaml.Marshal(out)
marshaler = yaml.Marshal
}

b, err := marshaler(entries)
if err != nil {
log.Fatalf("error marshaling output: %s", err)
}
Expand Down

0 comments on commit c2884c1

Please sign in to comment.