From 521fc6a1c9f1e2e0ace16ccf1249904157b311c8 Mon Sep 17 00:00:00 2001 From: Sam Therapy Date: Sat, 17 Sep 2022 01:38:10 +0200 Subject: [PATCH] fix: remove race conditions with flags Mainly for testing but a nice to have :) Signed-off-by: Sam Therapy --- cli/cli.go | 112 ++++++++++++++++++++++++++--------------------------- 1 file changed, 56 insertions(+), 56 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index e093ece..1cf35e3 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -18,9 +18,9 @@ import ( // ParseCLI parses arguments given from the CLI and passes them into an `Options` // struct. func ParseCLI(args []string, version string) (util.Options, error) { - flag.CommandLine = flag.NewFlagSet(args[0], flag.ContinueOnError) + flagSet := flag.NewFlagSet(args[0], flag.ContinueOnError) - flag.Usage = func() { + flagSet.Usage = func() { fmt.Println(`awl - drill, writ small Usage: awl name [@server] [record] @@ -31,80 +31,80 @@ func ParseCLI(args []string, version string) (util.Options, error) { Dig-like +[no]commands are also supported, see dig(1) or dig -h Options:`) - flag.PrintDefaults() + flagSet.PrintDefaults() } // CLI flags // // Remember, when adding a flag edit the manpage and the completions :) var ( - port = flag.Int("port", 0, "`port` to make DNS query (default: 53 for UDP/TCP, 853 for TLS/QUIC)", flag.OptShorthand('p'), flag.OptDisablePrintDefault(true)) - query = flag.String("query", "", "domain name to `query` (default: .)", flag.OptShorthand('q')) - class = flag.String("class", "IN", "DNS `class` to query", flag.OptShorthand('c')) - qType = flag.String("qType", "", "`type` to query (default: A)", flag.OptShorthand('t')) + port = flagSet.Int("port", 0, "`port` to make DNS query (default: 53 for UDP/TCP, 853 for TLS/QUIC)", flag.OptShorthand('p'), flag.OptDisablePrintDefault(true)) + query = flagSet.String("query", "", "domain name to `query` (default: .)", flag.OptShorthand('q')) + class = flagSet.String("class", "IN", "DNS `class` to query", flag.OptShorthand('c')) + qType = flagSet.String("qType", "", "`type` to query (default: A)", flag.OptShorthand('t')) - ipv4 = flag.Bool("4", false, "force IPv4", flag.OptShorthand('4')) - ipv6 = flag.Bool("6", false, "force IPv6", flag.OptShorthand('6')) - reverse = flag.Bool("reverse", false, "do a reverse lookup", flag.OptShorthand('x')) + ipv4 = flagSet.Bool("4", false, "force IPv4", flag.OptShorthand('4')) + ipv6 = flagSet.Bool("6", false, "force IPv6", flag.OptShorthand('6')) + reverse = flagSet.Bool("reverse", false, "do a reverse lookup", flag.OptShorthand('x')) - timeout = flag.Float32("timeout", 1, "Timeout, in `seconds`") - retry = flag.Int("retries", 2, "number of `times` to retry") + timeout = flagSet.Float32("timeout", 1, "Timeout, in `seconds`") + retry = flagSet.Int("retries", 2, "number of `times` to retry") - edns = flag.Bool("no-edns", false, "disable EDNS entirely") - ednsVer = flag.Uint8("edns-ver", 0, "set EDNS version") - dnssec = flag.Bool("dnssec", false, "enable DNSSEC", flag.OptShorthand('D')) - expire = flag.Bool("expire", false, "set EDNS expire") - nsid = flag.Bool("nsid", false, "set EDNS NSID", flag.OptShorthand('n')) - cookie = flag.Bool("no-cookie", false, "disable sending EDNS cookie (default: cookie sent)") - tcpKeepAlive = flag.Bool("keep-alive", false, "send EDNS TCP keep-alive") - udpBufSize = flag.Uint16("buffer-size", 1232, "set EDNS UDP buffer size", flag.OptShorthand('b')) - mbzflag = flag.String("zflag", "0", "set EDNS z-flag `value`") - subnet = flag.String("subnet", "", "set EDNS client subnet") - padding = flag.Bool("pad", false, "set EDNS padding") + edns = flagSet.Bool("no-edns", false, "disable EDNS entirely") + ednsVer = flagSet.Uint8("edns-ver", 0, "set EDNS version") + dnssec = flagSet.Bool("dnssec", false, "enable DNSSEC", flag.OptShorthand('D')) + expire = flagSet.Bool("expire", false, "set EDNS expire") + nsid = flagSet.Bool("nsid", false, "set EDNS NSID", flag.OptShorthand('n')) + cookie = flagSet.Bool("no-cookie", false, "disable sending EDNS cookie (default: cookie sent)") + tcpKeepAlive = flagSet.Bool("keep-alive", false, "send EDNS TCP keep-alive") + udpBufSize = flagSet.Uint16("buffer-size", 1232, "set EDNS UDP buffer size", flag.OptShorthand('b')) + mbzflag = flagSet.String("zflag", "0", "set EDNS z-flag `value`") + subnet = flagSet.String("subnet", "", "set EDNS client subnet") + padding = flagSet.Bool("pad", false, "set EDNS padding") - badCookie = flag.Bool("no-bad-cookie", false, "ignore BADCOOKIE EDNS responses (default: retry with correct cookie") - truncate = flag.Bool("no-truncate", false, "ignore truncation if a UDP request truncates (default: retry with TCP)") + badCookie = flagSet.Bool("no-bad-cookie", false, "ignore BADCOOKIE EDNS responses (default: retry with correct cookie") + truncate = flagSet.Bool("no-truncate", false, "ignore truncation if a UDP request truncates (default: retry with TCP)") - tcp = flag.Bool("tcp", false, "use TCP") - dnscrypt = flag.Bool("dnscrypt", false, "use DNSCrypt") - tls = flag.Bool("tls", false, "use DNS-over-TLS", flag.OptShorthand('T')) - https = flag.Bool("https", false, "use DNS-over-HTTPS", flag.OptShorthand('H')) - quic = flag.Bool("quic", false, "use DNS-over-QUIC", flag.OptShorthand('Q')) + tcp = flagSet.Bool("tcp", false, "use TCP") + dnscrypt = flagSet.Bool("dnscrypt", false, "use DNSCrypt") + tls = flagSet.Bool("tls", false, "use DNS-over-TLS", flag.OptShorthand('T')) + https = flagSet.Bool("https", false, "use DNS-over-HTTPS", flag.OptShorthand('H')) + quic = flagSet.Bool("quic", false, "use DNS-over-QUIC", flag.OptShorthand('Q')) - tlsHost = flag.String("tls-host", "", "Server name to use for TLS verification") - noVerify = flag.Bool("tls-no-verify", false, "Disable TLS cert verification") + tlsHost = flagSet.String("tls-host", "", "Server name to use for TLS verification") + noVerify = flagSet.Bool("tls-no-verify", false, "Disable TLS cert verification") - aaflag = flag.Bool("aa", false, "set/unset AA (Authoratative Answer) flag (default: not set)") - adflag = flag.Bool("ad", false, "set/unset AD (Authenticated Data) flag (default: not set)") - cdflag = flag.Bool("cd", false, "set/unset CD (Checking Disabled) flag (default: not set)") - qrflag = flag.Bool("qr", false, "set/unset QR (QueRy) flag (default: not set)") - rdflag = flag.Bool("rd", true, "set/unset RD (Recursion Desired) flag (default: set)", flag.OptDisablePrintDefault(true)) - raflag = flag.Bool("ra", false, "set/unset RA (Recursion Available) flag (default: not set)") - tcflag = flag.Bool("tc", false, "set/unset TC (TrunCated) flag (default: not set)") - zflag = flag.Bool("z", false, "set/unset Z (Zero) flag (default: not set)", flag.OptShorthand('z')) + aaflag = flagSet.Bool("aa", false, "set/unset AA (Authoratative Answer) flag (default: not set)") + adflag = flagSet.Bool("ad", false, "set/unset AD (Authenticated Data) flag (default: not set)") + cdflag = flagSet.Bool("cd", false, "set/unset CD (Checking Disabled) flag (default: not set)") + qrflag = flagSet.Bool("qr", false, "set/unset QR (QueRy) flag (default: not set)") + rdflag = flagSet.Bool("rd", true, "set/unset RD (Recursion Desired) flag (default: set)", flag.OptDisablePrintDefault(true)) + raflag = flagSet.Bool("ra", false, "set/unset RA (Recursion Available) flag (default: not set)") + tcflag = flagSet.Bool("tc", false, "set/unset TC (TrunCated) flag (default: not set)") + zflag = flagSet.Bool("z", false, "set/unset Z (Zero) flag (default: not set)", flag.OptShorthand('z')) - short = flag.Bool("short", false, "print just the results", flag.OptShorthand('s')) - json = flag.Bool("json", false, "print the result(s) as JSON", flag.OptShorthand('j')) - xml = flag.Bool("xml", false, "print the result(s) as XML", flag.OptShorthand('X')) - yaml = flag.Bool("yaml", false, "print the result(s) as yaml", flag.OptShorthand('y')) + short = flagSet.Bool("short", false, "print just the results", flag.OptShorthand('s')) + json = flagSet.Bool("json", false, "print the result(s) as JSON", flag.OptShorthand('j')) + xml = flagSet.Bool("xml", false, "print the result(s) as XML", flag.OptShorthand('X')) + yaml = flagSet.Bool("yaml", false, "print the result(s) as yaml", flag.OptShorthand('y')) - noC = flag.Bool("no-comments", false, "disable printing the comments") - noQ = flag.Bool("no-question", false, "disable printing the question section") - noOpt = flag.Bool("no-opt", false, "disable printing the OPT pseudosection") - noAns = flag.Bool("no-answer", false, "disable printing the answer section") - noAuth = flag.Bool("no-authority", false, "disable printing the authority section") - noAdd = flag.Bool("no-additional", false, "disable printing the additional section") - noStats = flag.Bool("no-statistics", false, "disable printing the statistics section") + noC = flagSet.Bool("no-comments", false, "disable printing the comments") + noQ = flagSet.Bool("no-question", false, "disable printing the question section") + noOpt = flagSet.Bool("no-opt", false, "disable printing the OPT pseudosection") + noAns = flagSet.Bool("no-answer", false, "disable printing the answer section") + noAuth = flagSet.Bool("no-authority", false, "disable printing the authority section") + noAdd = flagSet.Bool("no-additional", false, "disable printing the additional section") + noStats = flagSet.Bool("no-statistics", false, "disable printing the statistics section") - verbosity = flag.Int("verbosity", 1, "sets verbosity `level`", flag.OptShorthand('v'), flag.OptNoOptDefVal("2")) - versionFlag = flag.Bool("version", false, "print version information", flag.OptShorthand('V')) + verbosity = flagSet.Int("verbosity", 1, "sets verbosity `level`", flag.OptShorthand('v'), flag.OptNoOptDefVal("2")) + versionFlag = flagSet.Bool("version", false, "print version information", flag.OptShorthand('V')) ) // Don't sort the flags when -h is given - flag.CommandLine.SortFlags = false + flagSet.SortFlags = false // Parse the flags - if err := flag.CommandLine.Parse(args[1:]); err != nil { + if err := flagSet.Parse(args[1:]); err != nil { return util.Options{Logger: util.InitLogger(*verbosity)}, fmt.Errorf("flag: %w", err) } @@ -194,7 +194,7 @@ func ParseCLI(args []string, version string) (util.Options, error) { // Parse all the arguments that don't start with - or -- // This includes the dig-style (+) options - err = ParseMiscArgs(flag.Args(), &opts) + err = ParseMiscArgs(flagSet.Args(), &opts) if err != nil { return opts, err }