From 22b2556a8116eb93f9194efe9e5e6ffb35c2a542 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Thu, 29 May 2025 23:47:47 +0700 Subject: all: change the Scan function parameter to struct ScanOptions Using struct allow to extends the parameter later without changing the signature. --- deadlinks.go | 4 ++-- deadlinks_test.go | 5 ++++- scan_options.go | 10 ++++++++++ worker.go | 18 ++++++++++++++---- 4 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 scan_options.go diff --git a/deadlinks.go b/deadlinks.go index 4c0f062..1d31b8f 100644 --- a/deadlinks.go +++ b/deadlinks.go @@ -8,11 +8,11 @@ import ( ) // Scan the baseUrl for dead links. -func Scan(baseUrl string) (result *Result, err error) { +func Scan(opts ScanOptions) (result *Result, err error) { var logp = `Scan` var wrk *worker - wrk, err = newWorker(baseUrl) + wrk, err = newWorker(opts) if err != nil { return nil, fmt.Errorf(`%s: %s`, logp, err) } diff --git a/deadlinks_test.go b/deadlinks_test.go index fc088b5..c0bdd84 100644 --- a/deadlinks_test.go +++ b/deadlinks_test.go @@ -154,7 +154,10 @@ func TestDeadLinks_Scan(t *testing.T) { err error ) for _, tcase := range listCase { - result, err = deadlinks.Scan(tcase.scanUrl) + var scanOpts = deadlinks.ScanOptions{ + Url: tcase.scanUrl, + } + result, err = deadlinks.Scan(scanOpts) if err != nil { test.Assert(t, tcase.scanUrl+` error`, tcase.expError, err.Error()) diff --git a/scan_options.go b/scan_options.go new file mode 100644 index 0000000..bc5484e --- /dev/null +++ b/scan_options.go @@ -0,0 +1,10 @@ +// SPDX-FileCopyrightText: 2025 M. Shulhan +// SPDX-License-Identifier: GPL-3.0-only + +package deadlinks + +// ScanOptions define the options for scan command or Scan function. +type ScanOptions struct { + Url string + IsVerbose bool +} diff --git a/worker.go b/worker.go index 23228db..c6e344d 100644 --- a/worker.go +++ b/worker.go @@ -37,6 +37,8 @@ type worker struct { // The URL to scan. scanUrl *url.URL + opts ScanOptions + // wg sync the goroutine scanner. wg sync.WaitGroup @@ -44,17 +46,18 @@ type worker struct { seenLinkMtx sync.Mutex } -func newWorker(scanUrl string) (wrk *worker, err error) { +func newWorker(opts ScanOptions) (wrk *worker, err error) { wrk = &worker{ + opts: opts, seenLink: map[string]int{}, - linkq: make(chan linkQueue, 1000), + linkq: make(chan linkQueue, 10000), errq: make(chan error, 1), result: newResult(), } - wrk.scanUrl, err = url.Parse(scanUrl) + wrk.scanUrl, err = url.Parse(opts.Url) if err != nil { - return nil, fmt.Errorf(`invalid URL %q`, scanUrl) + return nil, fmt.Errorf(`invalid URL %q`, opts.Url) } wrk.scanUrl.Path = strings.TrimSuffix(wrk.scanUrl.Path, `/`) @@ -107,12 +110,19 @@ func (wrk *worker) scan(linkq linkQueue) { if statusCode >= http.StatusBadRequest { wrk.markDead(linkq, statusCode) } + if wrk.opts.IsVerbose { + fmt.Printf("scan: %s %d\n", linkq.url, statusCode) + } return } wrk.seenLinkMtx.Lock() wrk.seenLink[linkq.url] = http.StatusProcessing wrk.seenLinkMtx.Unlock() + if wrk.opts.IsVerbose { + fmt.Printf("scan: %s %d\n", linkq.url, http.StatusProcessing) + } + var ( httpResp *http.Response err error -- cgit v1.3