diff options
| author | Shulhan <ms@kilabit.info> | 2025-06-12 21:13:58 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2025-06-12 21:13:58 +0700 |
| commit | a02e915388723a5d8cc3b555fb3dfec477fc2a55 (patch) | |
| tree | aa35678b263646e1edd730a16cb35a66e7b933d8 /brokenlinks.go | |
| parent | f408c77795a9dd6d4551fadd2e8352ba08915feb (diff) | |
| download | jarink-a02e915388723a5d8cc3b555fb3dfec477fc2a55.tar.xz | |
all: refactoring, move brokenlinks code to its own package
When two or more struct has the same prefix that means it is time to
move it to group it.
Also, we will group one command to one package in the future.
Diffstat (limited to 'brokenlinks.go')
| -rw-r--r-- | brokenlinks.go | 69 |
1 files changed, 0 insertions, 69 deletions
diff --git a/brokenlinks.go b/brokenlinks.go deleted file mode 100644 index 96580e5..0000000 --- a/brokenlinks.go +++ /dev/null @@ -1,69 +0,0 @@ -// SPDX-FileCopyrightText: 2025 M. Shulhan <ms@kilabit.info> -// SPDX-License-Identifier: GPL-3.0-only - -package jarink - -import ( - "fmt" - "slices" - "strings" -) - -const Version = `0.1.0` - -// StatusBadLink status for link that is not parseable by [url.Parse] or not -// reachable during GET or HEAD, either timeout or IP or domain not exist. -const StatusBadLink = 700 - -// Broken store the broken link, HTTP status code, and the error message that -// cause it. -type Broken struct { - Link string `json:"link"` - Error string `json:"error,omitempty"` - Code int `json:"code"` -} - -// BrokenlinksOptions define the options for scanning broken links. -type BrokenlinksOptions struct { - Url string - PastResultFile string - IsVerbose bool -} - -// BrokenlinksResult store the result of scanning for broken links. -type BrokenlinksResult struct { - // BrokenLinks store the page and its broken links. - BrokenLinks map[string][]Broken `json:"broken_links"` -} - -func newBrokenlinksResult() *BrokenlinksResult { - return &BrokenlinksResult{ - BrokenLinks: map[string][]Broken{}, - } -} - -func (result *BrokenlinksResult) sort() { - for _, listBroken := range result.BrokenLinks { - slices.SortFunc(listBroken, func(a, b Broken) int { - return strings.Compare(a.Link, b.Link) - }) - } -} - -// Brokenlinks scan the URL for broken links. -func Brokenlinks(opts BrokenlinksOptions) (result *BrokenlinksResult, err error) { - var logp = `brokenlinks` - var wrk *brokenlinksWorker - - wrk, err = newWorker(opts) - if err != nil { - return nil, fmt.Errorf(`%s: %s`, logp, err) - } - - result, err = wrk.run() - if err != nil { - return nil, fmt.Errorf(`%s: %s`, logp, err) - } - - return result, nil -} |
