Skip to content
This repository was archived by the owner on Sep 3, 2021. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/go-rod/rod v0.69.0
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-querystring v1.0.0 // indirect
github.com/kr/pretty v0.1.0
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/magiconair/properties v1.8.4 // indirect
github.com/microcosm-cc/bluemonday v1.0.4
Expand Down
36 changes: 36 additions & 0 deletions internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ func (c Client) Run(ctx context.Context) (bool, error) {
return false, fmt.Errorf("fail to scan the files: %w", err)
}

// entries = []service.Entry{
// {
// Path: "/Users/dbernardes/Source/Nitro/platform/engineering-documentation/deprecated/practices/account-links.md",
// Link: "https://www.cloudflare.com/adasdasdadasdas",
// },
// }

w := worker.Worker{Providers: c.providers}
entries, err = w.Process(ctx, entries)
if err != nil {
Expand Down Expand Up @@ -159,6 +166,35 @@ func (c Client) output(entries []service.Entry) bool {
}
fmt.Printf("\n\n")
}

// Printing the details of the failure.
result = false
iter = c.aggregate(entries)
for {
key, entries, ok := iter()
if !ok {
break
}
if !c.hasInvalidLink(entries) {
continue
}
if !result {
result = true
}

for _, entry := range entries {
if entry.Valid || (entry.FailReason == nil) {
continue
}
fmt.Printf(
"The link '%s' at the file '%s' failed because of:\n",
aurora.Bold(entry.Link), aurora.Bold(c.relativePath(key)),
)
entry.FailReason()
}
fmt.Printf("\n\n")
}

return result
}

Expand Down
12 changes: 9 additions & 3 deletions internal/service/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package service

// Entry represents the link present at a given file.
type Entry struct {
Path string
Link string
Valid bool
Path string
Link string
Valid bool
FailReason func()
}

// EnhancedError implements a logic to pretty print an error.
type EnhancedError interface {
PrettyPrint()
}
16 changes: 14 additions & 2 deletions internal/service/provider/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"regexp"
Expand Down Expand Up @@ -91,7 +92,7 @@ func (w Web) Valid(ctx context.Context, _, uri string) (bool, error) {

resp, err := w.client.Do(req)
if err != nil {
return false, nil
return false, &webError{base: err, requestHeader: req.Header}
}
defer resp.Body.Close()

Expand All @@ -102,7 +103,18 @@ func (w Web) Valid(ctx context.Context, _, uri string) (bool, error) {

isValid := ((resp.StatusCode >= 200) && (resp.StatusCode < 300))
if !isValid {
return false, nil
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, fmt.Errorf("failed to read the response body: %w", err)
}

return false, &webError{
base: err,
requestHeader: req.Header,
responseHeader: resp.Header,
status: resp.StatusCode,
body: string(body),
}
}

validAnchor, err := w.validAnchor(resp.Body, endpoint.Fragment)
Expand Down
23 changes: 23 additions & 0 deletions internal/service/provider/web_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package provider

import (
"net/http"

"github.com/kr/pretty"
)

type webError struct {
base error
requestHeader http.Header
responseHeader http.Header
status int
body string
}

func (err webError) Error() string {
return err.base.Error()
}

func (err webError) PrettyPrint() {
pretty.Println(err)
}
15 changes: 12 additions & 3 deletions internal/service/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"

"nitro/markdown-link-check/internal/service"

"github.com/logrusorgru/aurora"
)

// Provider represents the providers resonsible to process the entries.
Expand Down Expand Up @@ -49,8 +51,10 @@ func (w Worker) Process(ctx context.Context, entries []service.Entry) ([]service
}

var (
errors []workerErrorUnit
result []service.Entry
errors []workerErrorUnit
result []service.Entry
processed int
total = len(entries)
)

for _, entry := range entries {
Expand All @@ -60,12 +64,17 @@ func (w Worker) Process(ctx context.Context, entries []service.Entry) ([]service
}

valid, err := provider.Valid(ctx, entry.Path, entry.Link)
if err != nil {
if e, ok := err.(service.EnhancedError); err != nil && !ok {
errors = append(errors, workerErrorUnit{err: err, entry: entry})
} else {
if ok {
entry.FailReason = e.PrettyPrint
}
entry.Valid = valid
result = append(result, entry)
}
processed++
fmt.Printf("%d of %d entries processed\n", aurora.Bold(processed), aurora.Bold(total))
break
}
}
Expand Down