Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the new fortio.org/cli to greatly simplify main code #23

Merged
merged 1 commit into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ Otherwise head over to https://github.com/fortio/dnsping/releases for binary rel
`dnsping [flags] query server`

<!-- generate using
go run . -h 2>&1 | expand | fold -s -w 90 | sed -e "s/ $//" -e "s/</\&lt;/"
go run . help | expand | fold -s -w 90 | sed -e "s/ $//" -e "s/</\&lt;/"
and remove the full path on the special args line
-->
```Shell
$ dnsping -h
dnsping 1.3.0 usage:
$ dnsping help
dnsping 1.4.0 usage:
dnsping [flags] query server
eg: dnsping www.google.com. 127.0.0.1
with flags:
eg: dnsping www.google.com. 8.8.8.8
or 1 of the special arguments
dnsping {help|version|buildinfo}
flags:
-c requests
How many requests to make. Default is to run until ^C
-fixed-id int
Expand All @@ -44,11 +47,12 @@ with flags:
Port to connect to (default 53)
-q type
Query type to use (A, AAAA, SOA, CNAME...) (default "A")
-quiet
Quiet mode, sets log level to warning
-sequential-id
Use sequential ids instead of random.
-t Timeout
Timeout for each query (default 700ms)
-v Display version and exit.
```

Sample run
Expand Down
45 changes: 7 additions & 38 deletions dnsping.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,12 @@ import (
"syscall"
"time"

"fortio.org/cli"
"fortio.org/fortio/stats"
"fortio.org/log"
"fortio.org/version"
"github.com/miekg/dns"
)

// Version is the version of the command, replace at link time to match the git tag for release builds.
var Version = "dev"

func usage() {
fmt.Fprintln(flag.CommandLine.Output(),
"dnsping "+Version+" usage:\n\tdnsping [flags] query server\neg:\tdnsping www.google.com. 127.0.0.1\nwith flags:")
flag.PrintDefaults()
}

// DNSPingConfig is the input configuration for DNSPing().
type DNSPingConfig struct {
Server string // Server to send query to
Expand Down Expand Up @@ -72,29 +63,12 @@ func Main() int {
timeoutFlag := flag.Duration("t", 700*time.Millisecond, "`Timeout` for each query")
countFlag := flag.Int("c", 0, "How many `requests` to make. Default is to run until ^C")
queryTypeFlag := flag.String("q", "A", "Query `type` to use (A, AAAA, SOA, CNAME...)")
versionFlag := flag.Bool("v", false, "Display version and exit.")
seqIDFlag := flag.Bool("sequential-id", false, "Use sequential ids instead of random.")
sameIDFlag := flag.Int("fixed-id", 0, "Non 0 id to use instead of random or sequential")
recursionFlag := flag.Bool("no-recursion", false, "Pass to disable (default) recursion.")
// make logger be less about debug by default
log.SetDefaultsForClientTools()
log.LoggerStaticFlagSetup()
var fullVersion string
Version, _, fullVersion = version.FromBuildInfo()
flag.CommandLine.Usage = usage
flag.Parse()
args := flag.Args()
nArgs := len(args)
log.LogVf("got %d arguments: %v", nArgs, args)
if *versionFlag {
// Short version, used by the build system
fmt.Println(Version)
return 0
}
if nArgs > 0 && args[0] == "version" {
fmt.Print(fullVersion)
return 0
}
cli.Config.MinArgs = 2
cli.Config.ArgsHelp = "query server\neg:\tdnsping www.google.com. 8.8.8.8"
cli.Main()
qt, exists := dns.StringToType[strings.ToUpper(*queryTypeFlag)]
if !exists {
keys := []string{}
Expand All @@ -104,18 +78,13 @@ func Main() int {
sort.Strings(keys)
return log.FErrf("Invalid -q type name %q, should be one of %v", *queryTypeFlag, keys)
}
if nArgs != 2 {
fmt.Fprintf(os.Stderr, "Error: need exactly 2 arguments outside of the flags, got %d\n", nArgs)
usage()
return 1
}
server := args[1]
server := flag.Arg(1)
if strings.Contains(server, ":") && !strings.HasPrefix(server, "[") {
server = "[" + server + "]"
log.Infof("Adding [] around detected input IPv6 server ip info: %s", server)
}
addrStr := fmt.Sprintf("%s:%d", server, *portFlag)
query := args[0]
query := flag.Arg(0)
if !strings.HasSuffix(query, ".") {
query += "."
log.LogVf("Adding missing . to query, now %q", query)
Expand Down Expand Up @@ -182,7 +151,7 @@ func DNSPing(cfg *DNSPingConfig) *DNSPingResults {
howManyStr = "until interrupted"
}
log.Infof("dnsping %s: will query %s, sleeping %v in between, the server %s for %s (%d) record for %s",
Version, howManyStr, cfg.Interval, cfg.Server, qtS, cfg.QueryType, cfg.Query)
cli.Config.ShortVersion, howManyStr, cfg.Interval, cfg.Server, qtS, cfg.QueryType, cfg.Query)
log.LogVf("Query is: %v", m)
successCount := 0
errorCount := 0
Expand Down
9 changes: 5 additions & 4 deletions dnsping.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
# Basic usage test
! dnsping
! stdout .
stderr 'need exactly 2 arguments outside of the flags, got 0'
stderr 'Exactly 2 arguments expected, got 0'

# (short) version
dnsping -v
stdout 'dev'
dnsping version
stdout '^dev$'
! stderr .

# (long) version
dnsping version
dnsping buildinfo
stdout '^dev go'
stdout 'path fortio.org/dnsping'
! stderr .

Expand Down
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ module fortio.org/dnsping
go 1.18

require (
fortio.org/fortio v1.50.0
fortio.org/cli v0.4.0
fortio.org/fortio v1.50.1
fortio.org/log v1.2.2
fortio.org/version v1.0.2
github.com/miekg/dns v1.1.50
github.com/rogpeppe/go-internal v1.9.0
)

require (
fortio.org/dflag v1.4.1 // indirect
fortio.org/version v1.0.2 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e // indirect
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/tools v0.6.0 // indirect
)
26 changes: 15 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
fortio.org/assert v1.1.3 h1:zXm8xiNiKvq2xG/YQ3sONAg3287XUuklKIDdjyD9pyg=
fortio.org/fortio v1.41.0-pre1 h1:/GQEeggvlKlLhqLdYsTztS3sgtWIngZ2bdh7gPFqGmw=
fortio.org/fortio v1.41.0-pre1/go.mod h1:ed1BLCuo6dn7lI46TpXpUUVTZsErhQNhyaAFVy45T2s=
fortio.org/fortio v1.50.0 h1:ZCkTKADhlGyycUuv4aPENdl2t2UGX5kmy3bqYx2BNAQ=
fortio.org/fortio v1.50.0/go.mod h1:ed1BLCuo6dn7lI46TpXpUUVTZsErhQNhyaAFVy45T2s=
fortio.org/cli v0.4.0 h1:h2Yh1Umv/3MrtrXODYubvuQvk/SpxB0gFdHClMy3UPs=
fortio.org/cli v0.4.0/go.mod h1:+h3Iv0kIfC3F/RnZG5seKk9VjBzA8uHQKtkQGiLqkWs=
fortio.org/dflag v1.4.1 h1:WDhlHMh3yrQFrvspyN5YEyr8WATdKM2dUJlTxsjCDtI=
fortio.org/dflag v1.4.1/go.mod h1:pTEF7UEj6sHP9rj9gZG2GyhAGrrPJE4c6zOO7zB2yyI=
fortio.org/fortio v1.50.1 h1:5FSttAHQsyAsi3dzxDmSByfzDYByrWY/yw53bqOg+Kc=
fortio.org/fortio v1.50.1/go.mod h1:TpAo6nixVbTxeIfE8F2c63D3ffouMTa+KDr9eFAYxys=
fortio.org/log v1.2.2 h1:vs42JjNwiqbMbacittZjJE9+oi72Za6aekML9gKmILg=
fortio.org/log v1.2.2/go.mod h1:u/8/2lyczXq52aT5Nw6reD+3cR6m/EbS2jBiIYhgiTU=
fortio.org/version v1.0.2 h1:8NwxdX58aoeKx7T5xAPO0xlUu1Hpk42nRz5s6e6eKZ0=
fortio.org/version v1.0.2/go.mod h1:2JQp9Ax+tm6QKiGuzR5nJY63kFeANcgrZ0osoQFDVm0=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/miekg/dns v1.1.50 h1:DQUfb9uc6smULcREF09Uc+/Gd46YWqJd5DbpPE9xkcA=
github.com/miekg/dns v1.1.50/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
Expand All @@ -16,21 +22,18 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb h1:PaBZQdo+iSDyHT053FjUCgZQ/9uqVwPOcl7KSWhKn6w=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0 h1:b9gGHsz9/HhJ3HF5DHQytPpuwocVTChQJK3AvoLRD5I=
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -40,17 +43,18 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.2.0 h1:G6AHpWxTMGY1KyEYoAQ5WTtIekUUvDNjan3ugu60JvE=
golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down