Minor (complete) refactor #38

Merged
sam merged 20 commits from small-things into master 2022-07-26 00:32:32 +00:00
3 changed files with 43 additions and 11 deletions
Showing only changes of commit d820b75db1 - Show all commits

View file

@ -46,7 +46,7 @@ func ParseCLI(version string) (Options, error) {
reverse = flag.Bool("reverse", false, "do a reverse lookup", flag.OptShorthand('x'))
timeout = flag.Float32("timeout", 1, "Timeout, in `seconds`")
retry = flag.Int("retry", 2, "number of `times` to retry")
retry = flag.Int("retries", 2, "number of `times` to retry")
dnssec = flag.Bool("dnssec", false, "enable DNSSEC", flag.OptShorthand('D'))
truncate = flag.Bool("no-truncate", false, "ignore truncation if a UDP request truncates (default= retry with TCP)")
@ -130,15 +130,6 @@ func ParseCLI(version string) (Options, error) {
},
}
// Set timeout to 0.5 seconds if set below 0.5
if float32(opts.Request.Timeout) < (0.5 * float32(time.Second)) {
opts.Request.Timeout = (time.Second / 2)
}
if opts.Request.Retries < 0 {
opts.Request.Timeout = 0
}
opts.Logger.Info("POSIX flags parsed")
opts.Logger.Debug(fmt.Sprintf("%+v", opts))
@ -166,5 +157,14 @@ func ParseCLI(version string) (Options, error) {
}
opts.Logger.Info("Port set to", opts.Port)
// Set timeout to 0.5 seconds if set below 0.5
if opts.Request.Timeout < (time.Second / 2) {
opts.Request.Timeout = (time.Second / 2)
}
if opts.Request.Retries < 0 {
opts.Request.Retries = 0
}
return opts, nil
}

View file

@ -5,6 +5,7 @@ package cli_test
import (
"os"
"testing"
"time"
"git.froth.zone/sam/awl/cli"
"gotest.tools/v3/assert"
@ -53,6 +54,37 @@ func TestVersion(t *testing.T) {
os.Args = old
}
func TestTimeout(t *testing.T) {
args := [][]string{
{"awl", "+timeout=0"},
{"awl", "--timeout", "0"},
}
for _, test := range args {
old := os.Args
os.Args = test
opt, err := cli.ParseCLI("TEST")
assert.NilError(t, err)
assert.Equal(t, opt.Request.Timeout, time.Second/2)
os.Args = old
}
}
func TestRetries(t *testing.T) {
args := [][]string{
{"awl", "+retry=-2"},
{"awl", "+tries=-2"},
{"awl", "--retries", "-2"},
}
for _, test := range args {
old := os.Args
os.Args = test
opt, err := cli.ParseCLI("TEST")
assert.NilError(t, err)
assert.Equal(t, opt.Request.Retries, 0)
os.Args = old
}
}
func FuzzFlags(f *testing.F) {
testcases := []string{"git.froth.zone", "", "!12345", "google.com.edu.org.fr"}
for _, tc := range testcases {

View file

@ -86,7 +86,7 @@ _Default Ports_:
Set the timeout period. Floating point numbers are accepted.++
0.5 seconds is the minimum.
*--retry* _int_, *+tries*=_int_, *+ retry*=_int_++
*--retries* _int_, *+tries*=_int_, *+ retry*=_int_++
Set the number of retries.++
Retry is one more than tries, dig style