Skip to content

Commit 46a5e14

Browse files
committed
feat(cli): match the output print same as web ui format
- refactor return type of dns lookup result as map - upgrade go-pretty for auto merge column
1 parent d6c1d21 commit 46a5e14

File tree

5 files changed

+37
-43
lines changed

5 files changed

+37
-43
lines changed

cmd/root.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"io"
55
"os"
66

7-
"github.com/jedib0t/go-pretty/table"
7+
"github.com/jedib0t/go-pretty/v6/table"
88
"github.com/spf13/cobra"
99
"github.com/sunggun-yu/dnsq/internal/dnslookup"
1010
"github.com/sunggun-yu/dnsq/internal/models"
@@ -21,10 +21,8 @@ func rootCommand() *cobra.Command {
2121
SilenceErrors: true,
2222
Args: cobra.MinimumNArgs(1),
2323
Run: func(cmd *cobra.Command, args []string) {
24-
for _, domain := range args {
25-
records := dnslookup.GetDNSRecords(domain)
26-
printRecords(cmd.OutOrStdout(), records)
27-
}
24+
records := dnslookup.GetDNSRecords(args)
25+
printRecords(cmd.OutOrStdout(), records)
2826
},
2927
}
3028
return cmd
@@ -57,7 +55,7 @@ func init() {
5755
}
5856

5957
// printRecords prints DNS records to the console
60-
func printRecords(w io.Writer, records []models.DNSRecord) {
58+
func printRecords(w io.Writer, records map[string][]models.DNSRecord) {
6159

6260
tw := table.NewWriter()
6361
tw.SetStyle(table.StyleLight)
@@ -66,10 +64,16 @@ func printRecords(w io.Writer, records []models.DNSRecord) {
6664
tw.Style().Options.SeparateRows = false
6765
tw.Style().Options.SeparateColumns = true
6866

69-
tw.AppendHeader(table.Row{"Host", "Type", "Data"})
70-
for _, rerecords := range records {
71-
tw.AppendRow(table.Row{rerecords.Host, rerecords.Type, rerecords.Data})
67+
tw.AppendHeader(table.Row{"Domain", "Host", "Type", "Data"})
68+
for domain, data := range records {
69+
for _, d := range data {
70+
tw.AppendRow(table.Row{domain, d.Host, d.Type, d.Data})
71+
}
7272
}
73+
tw.SetColumnConfigs([]table.ColumnConfig{
74+
{Number: 1, AutoMerge: true},
75+
})
76+
tw.Style().Options.SeparateRows = true
7377
w.Write([]byte(tw.Render()))
7478
w.Write([]byte("\n"))
7579
}

go.mod

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,42 @@ go 1.22.3
44

55
require (
66
github.com/gin-gonic/gin v1.10.0
7-
github.com/jedib0t/go-pretty v4.3.0+incompatible
7+
github.com/jedib0t/go-pretty/v6 v6.6.0
88
github.com/spf13/cobra v1.8.1
99
)
1010

1111
require (
12-
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
1312
github.com/bytedance/sonic v1.11.6 // indirect
1413
github.com/bytedance/sonic/loader v0.1.1 // indirect
1514
github.com/cloudwego/base64x v0.1.4 // indirect
1615
github.com/cloudwego/iasm v0.2.0 // indirect
1716
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
1817
github.com/gin-contrib/sse v0.1.0 // indirect
19-
github.com/go-openapi/errors v0.22.0 // indirect
20-
github.com/go-openapi/strfmt v0.23.0 // indirect
2118
github.com/go-playground/locales v0.14.1 // indirect
2219
github.com/go-playground/universal-translator v0.18.1 // indirect
2320
github.com/go-playground/validator/v10 v10.20.0 // indirect
2421
github.com/goccy/go-json v0.10.2 // indirect
25-
github.com/google/uuid v1.6.0 // indirect
2622
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2723
github.com/json-iterator/go v1.1.12 // indirect
2824
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
25+
github.com/kr/text v0.2.0 // indirect
2926
github.com/leodido/go-urn v1.4.0 // indirect
3027
github.com/mattn/go-isatty v0.0.20 // indirect
3128
github.com/mattn/go-runewidth v0.0.15 // indirect
32-
github.com/mitchellh/mapstructure v1.5.0 // indirect
3329
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
3430
github.com/modern-go/reflect2 v1.0.2 // indirect
35-
github.com/oklog/ulid v1.3.1 // indirect
31+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
3632
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
3733
github.com/rivo/uniseg v0.2.0 // indirect
3834
github.com/spf13/pflag v1.0.5 // indirect
3935
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
4036
github.com/ugorji/go/codec v1.2.12 // indirect
41-
go.mongodb.org/mongo-driver v1.14.0 // indirect
4237
golang.org/x/arch v0.8.0 // indirect
4338
golang.org/x/crypto v0.23.0 // indirect
4439
golang.org/x/net v0.25.0 // indirect
4540
golang.org/x/sys v0.20.0 // indirect
4641
golang.org/x/text v0.15.0 // indirect
4742
google.golang.org/protobuf v1.34.1 // indirect
43+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
4844
gopkg.in/yaml.v3 v3.0.1 // indirect
4945
)

go.sum

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
2-
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
31
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
42
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
53
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
@@ -9,6 +7,7 @@ github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJ
97
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
108
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
119
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
10+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
1211
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1312
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1413
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -18,10 +17,6 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE
1817
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
1918
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
2019
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
21-
github.com/go-openapi/errors v0.22.0 h1:c4xY/OLxUBSTiepAg3j/MHuAv5mJhnf53LLMWFB+u/w=
22-
github.com/go-openapi/errors v0.22.0/go.mod h1:J3DmZScxCDufmIMsdOuDHxJbdOGC0xtUynjIx092vXE=
23-
github.com/go-openapi/strfmt v0.23.0 h1:nlUS6BCqcnAk0pyhi9Y+kdDVZdZMHfEKQiS4HaMgO/c=
24-
github.com/go-openapi/strfmt v0.23.0/go.mod h1:NrtIpfKtWIygRkKVsxh7XQMDQW5HKQl6S5ik2elW+K4=
2520
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
2621
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
2722
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
@@ -35,18 +30,18 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
3530
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
3631
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3732
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
38-
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
39-
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4033
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
4134
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
42-
github.com/jedib0t/go-pretty v4.3.0+incompatible h1:CGs8AVhEKg/n9YbUenWmNStRW2PHJzaeDodcfvRAbIo=
43-
github.com/jedib0t/go-pretty v4.3.0+incompatible/go.mod h1:XemHduiw8R651AF9Pt4FwCTKeG3oo7hrHJAoznj9nag=
35+
github.com/jedib0t/go-pretty/v6 v6.6.0 h1:wmZVuAcEkZRT+Aq1xXpE8IGat4vE5WXOMmBpbQqERXw=
36+
github.com/jedib0t/go-pretty/v6 v6.6.0/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E=
4437
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
4538
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
4639
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
4740
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
4841
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
4942
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
43+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
44+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
5045
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
5146
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
5247
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
@@ -55,17 +50,13 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
5550
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
5651
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
5752
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
58-
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
59-
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
6053
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
6154
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
6255
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
6356
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
6457
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
6558
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
6659
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
67-
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
68-
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
6960
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
7061
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
7162
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -93,8 +84,6 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
9384
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
9485
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
9586
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
96-
go.mongodb.org/mongo-driver v1.14.0 h1:P98w8egYRjYe3XDjxhYJagTokP/H6HzlsnojRgZRd80=
97-
go.mongodb.org/mongo-driver v1.14.0/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c=
9887
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
9988
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
10089
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=

internal/dnslookup/dnslookup.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,19 @@ func randomHostname() string {
2121
}
2222

2323
// GetDNSRecords returns DNS records for a given hostname
24-
func GetDNSRecords(hostname string) []models.DNSRecord {
24+
func GetDNSRecords(hosts []string) map[string][]models.DNSRecord {
25+
result := make(map[string][]models.DNSRecord)
26+
for _, host := range hosts {
27+
host = strings.TrimSpace(host)
28+
if host != "" {
29+
result[host] = dnsRecords(host)
30+
}
31+
}
32+
return result
33+
}
34+
35+
// GetDNSRecords returns DNS records for a given hostname
36+
func dnsRecords(hostname string) []models.DNSRecord {
2537
var records []models.DNSRecord
2638
resolveDNS(hostname, &records)
2739
return records

internal/server/handlers/handlers.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/gin-gonic/gin"
88
"github.com/sunggun-yu/dnsq/internal/dnslookup"
9-
"github.com/sunggun-yu/dnsq/internal/models"
109
)
1110

1211
// DNSLookupHandler handles DNS lookup requests
@@ -19,14 +18,8 @@ func DNSLookupHandler(c *gin.Context) {
1918
}
2019

2120
hosts := strings.Split(hostsParam, ",")
22-
results := make(map[string][]models.DNSRecord)
23-
for _, host := range hosts {
24-
host = strings.TrimSpace(host)
25-
if host != "" {
26-
results[host] = dnslookup.GetDNSRecords(host)
27-
}
28-
}
21+
results := dnslookup.GetDNSRecords(hosts)
2922

3023
// return 200 OK with the DNS records
3124
c.JSON(http.StatusOK, results)
32-
}
25+
}

0 commit comments

Comments
 (0)