diff options
| author | Shulhan <ms@kilabit.info> | 2025-05-31 19:15:41 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2025-05-31 19:25:48 +0700 |
| commit | df380fa8fefbb04ac3f9c29198e5535364c67865 (patch) | |
| tree | 7bde414b81ecfc703956c321cb97a4da49757fc8 | |
| parent | 6727eecc430a7a6b524362b69ebfa9e79522d76a (diff) | |
| download | jarink-df380fa8fefbb04ac3f9c29198e5535364c67865.tar.xz | |
cmd/deadlinks: print the result in JSON
Using JSON as output can be parsed by other tools.
| -rw-r--r-- | cmd/deadlinks/main.go | 49 | ||||
| -rw-r--r-- | deadlinks.go | 2 |
2 files changed, 35 insertions, 16 deletions
diff --git a/cmd/deadlinks/main.go b/cmd/deadlinks/main.go index 59c06b1..d018c9d 100644 --- a/cmd/deadlinks/main.go +++ b/cmd/deadlinks/main.go @@ -4,6 +4,7 @@ package main import ( + "encoding/json" "flag" "fmt" "log" @@ -43,15 +44,12 @@ func main() { log.Fatal(err.Error()) } - var page string - var listBroken []deadlinks.Broken - for page, listBroken = range result.PageLinks { - fmt.Printf("Page: %s\n", page) - for _, broken := range listBroken { - fmt.Printf("\tDead: %s (%d)\n", broken.Link, - broken.Code) - } + var resultJson []byte + resultJson, err = json.MarshalIndent(result.PageLinks, ``, ` `) + if err != nil { + log.Fatal(err.Error()) } + fmt.Printf("%s\n", resultJson) return } @@ -71,11 +69,11 @@ the image src attribute ("<img src=..."). == Usage -scan URL [OPTIONS] +[OPTIONS] scan URL Start scanning for deadlinks on the web server pointed by URL. Once finished it will print the page and list of dead links inside - that page. + that page in JSON format. This command accept the following options, -verbose : print the page that being scanned. @@ -83,10 +81,29 @@ scan URL [OPTIONS] Example, $ deadlinks scan https://kilabit.info - Page: https://kilabit.info/some/page - Dead: https://kilabit.info/some/page/image.png (404) - Dead: https://external.com/link (500) - Page: https://kilabit.info/another/page - Dead: https://kilabit.info/another/page/image.png (404) - Dead: https://external.org/link (500)`) + { + "https://kilabit.info/some/page": [ + { + "Link": "https://kilabit.info/some/page/image.png", + "Code": 404 + }, + { + "Link": "https://external.com/link", + "Code": 500 + } + ], + "https://kilabit.info/another/page": [ + { + "Link": "https://kilabit.info/another/page/image.png", + "Code": 404 + }, + { + "Link": "https://external.org/link", + "Code": 500 + } + ] + } + +-- +deadlinks v` + deadlinks.Version) } diff --git a/deadlinks.go b/deadlinks.go index 365a6fb..2edcd0d 100644 --- a/deadlinks.go +++ b/deadlinks.go @@ -7,6 +7,8 @@ import ( "fmt" ) +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 |
