From faf729a6841cc8625e66f93e7cca8b0a0a9595c9 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 26 Sep 2020 20:36:02 -0700 Subject: [PATCH] Fix default values in help (#109) * Fix default values in help * Remove graceful handling of invalid argument in throttled http client * Bump version * Replace printHelp function * Remove unused constant * Ignore errcheck --- .snapshots/{TestPrintHelp => TestHelp} | 10 +++++++--- arguments.go | 12 +++++++++--- arguments_test.go | 7 ++----- command.go | 2 +- configuration.go | 9 ++++----- throttled_http_client.go | 4 ---- throttled_http_client_test.go | 7 ------- 7 files changed, 23 insertions(+), 28 deletions(-) rename .snapshots/{TestPrintHelp => TestHelp} (82%) delete mode 100644 throttled_http_client_test.go diff --git a/.snapshots/TestPrintHelp b/.snapshots/TestHelp similarity index 82% rename from .snapshots/TestPrintHelp rename to .snapshots/TestHelp index 331fe927..244fc992 100644 --- a/.snapshots/TestPrintHelp +++ b/.snapshots/TestHelp @@ -2,17 +2,21 @@ Usage: muffet.test [options] Application Options: - -b, --buffer-size= HTTP response buffer size in bytes - -c, --max-connections= Maximum number of HTTP connections + -b, --buffer-size= HTTP response buffer size in bytes (default: + 4096) + -c, --max-connections= Maximum number of HTTP connections (default: + 512) --max-connections-per-host= Maximum number of HTTP connections per host + (default: 512) -e, --exclude= Exclude URLs matched with given regular expressions --follow-robots-txt Follow robots.txt when scraping pages --follow-sitemap-xml Scrape only pages listed in sitemap.xml --header= Custom headers -f, --ignore-fragments Ignore URL fragments - -r, --max-redirections= Maximum number of redirections + -r, --max-redirections= Maximum number of redirections (default: 64) -t, --timeout= Timeout for HTTP requests in seconds + (default: 10) -v, --verbose Show successful results too --skip-tls-verification Skip TLS certificate verification --one-page-only Only check links found in the given URL diff --git a/arguments.go b/arguments.go index c537acdf..7cff3875 100644 --- a/arguments.go +++ b/arguments.go @@ -1,8 +1,8 @@ package main import ( + "bytes" "errors" - "io" "regexp" "strings" @@ -61,11 +61,17 @@ func getArguments(ss []string) (*arguments, error) { return &args, nil } -func printHelp(w io.Writer) { +func help() string { p := flags.NewParser(&arguments{}, flags.PassDoubleDash) p.Usage = "[options] " - p.WriteHelp(w) + // Parse() is run here to show default values in help. + // This seems to be a bug in go-flags. + p.Parse() // nolint:errcheck + + b := &bytes.Buffer{} + p.WriteHelp(b) + return b.String() } func compileRegexps(regexps []string) ([]*regexp.Regexp, error) { diff --git a/arguments_test.go b/arguments_test.go index 60a3973d..2ba123e4 100644 --- a/arguments_test.go +++ b/arguments_test.go @@ -1,7 +1,6 @@ package main import ( - "bytes" "testing" "github.com/bradleyjkemp/cupaloy" @@ -60,10 +59,8 @@ func TestGetArgumentsError(t *testing.T) { } } -func TestPrintHelp(t *testing.T) { - b := &bytes.Buffer{} - printHelp(b) - cupaloy.SnapshotT(t, b.String()) +func TestHelp(t *testing.T) { + cupaloy.SnapshotT(t, help()) } func TestParseHeaders(t *testing.T) { diff --git a/command.go b/command.go index 3111a605..59f6a5cf 100644 --- a/command.go +++ b/command.go @@ -34,7 +34,7 @@ func (c *command) runWithError(ss []string) (bool, error) { if err != nil { return false, err } else if args.Help { - printHelp(c.stdout) + c.print(help()) return true, nil } else if args.Version { c.print(version) diff --git a/configuration.go b/configuration.go index 61f767ec..856bf56a 100644 --- a/configuration.go +++ b/configuration.go @@ -3,9 +3,8 @@ package main import "time" const ( - version = "2.0.1" - agentName = "muffet" - concurrency = 1024 - tcpTimeout = time.Minute - defaultMaxConnections = 512 + version = "2.0.2" + agentName = "muffet" + concurrency = 1024 + tcpTimeout = time.Minute ) diff --git a/throttled_http_client.go b/throttled_http_client.go index f80d5b2a..39e63f1e 100644 --- a/throttled_http_client.go +++ b/throttled_http_client.go @@ -8,10 +8,6 @@ type throttledHTTPClient struct { } func newThrottledHTTPClient(c httpClient, maxConnections int) httpClient { - if maxConnections < 1 { - maxConnections = defaultMaxConnections - } - return &throttledHTTPClient{c, newSemaphore(maxConnections)} } diff --git a/throttled_http_client_test.go b/throttled_http_client_test.go deleted file mode 100644 index 50afee38..00000000 --- a/throttled_http_client_test.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "testing" - -func TestNewThrottledHTTPClientHandleInvalidConnectionsOptionGracefully(t *testing.T) { - newThrottledHTTPClient(nil, -1) -}