This repository has been archived by the owner on May 11, 2022. It is now read-only.
/
printer.go
223 lines (195 loc) Β· 5.32 KB
/
printer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package availability
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"sort"
"text/template"
"github.com/fatih/color"
"github.com/kamilsk/check/errors"
)
const (
shaded = "shaded"
success = "success"
warning = "warning"
danger = "danger"
)
var base = template.Must(template.New("entry").Parse(`
{{- define "error" }}{{ with .Error }} -> ({{ . }}){{ end }}{{ end -}}
{{- define "redirect" }}{{ with .Redirect }} -> {{ . }}{{ end }}{{ end -}}
[{{ .StatusCode }}] {{ .Location }}{{ template "error" . }}{{ template "redirect" . -}}
`))
// NewPrinter returns configured printer instance.
func NewPrinter(options ...func(*Printer)) *Printer {
p := &Printer{
tpl: template.Must(base.Clone()),
decoder: func(origin string) string { return origin },
}
for _, f := range options {
f(p)
}
return p
}
// ColorizeOutput sets the ink for the printer.
func ColorizeOutput(enabled bool) func(*Printer) {
return func(p *Printer) {
if enabled {
p.ink = map[string]*color.Color{
shaded: color.New(color.FgHiBlack),
success: color.New(color.FgWhite),
warning: color.New(color.FgYellow),
danger: color.New(color.FgRed, color.Bold),
}
}
}
}
// DecodeOutput sets `net/url.PathUnescape` as a decoder.
func DecodeOutput(enabled bool) func(*Printer) {
return func(p *Printer) {
if enabled {
p.decoder = func(origin string) string {
decoded, err := url.PathUnescape(origin)
if err != nil {
return origin
}
return decoded
}
}
}
}
// HideError prevents URL's error output.
func HideError(disabled bool) func(*Printer) {
return func(p *Printer) {
if disabled {
p.tpl.New("error").Parse("{{ with .Error }}{{/* ignore */}}{{ end }}")
}
}
}
// HideRedirect prevents URL's redirect output.
func HideRedirect(disabled bool) func(*Printer) {
return func(p *Printer) {
if disabled {
p.tpl.New("redirect").Parse("{{ with .Redirect }}{{/* ignore */}}{{ end }}")
}
}
}
// OutputForPrinting sets up printer output.
func OutputForPrinting(output io.Writer) func(*Printer) {
return func(p *Printer) {
p.output = output
}
}
// Reporter defines general behavior of report providers.
type Reporter interface {
Sites() <-chan Site
}
// Printer represents a printer.
type Printer struct {
tpl *template.Template
output io.Writer
ink map[string]*color.Color
decoder func(string) string
report Reporter
}
// For prepares printer for passed report provider.
func (p *Printer) For(report Reporter) *Printer {
p.report = report
return p
}
// Print prints a report into the configured output.
// Stdout is used as a fallback if the output is not set up.
func (p *Printer) Print() error {
var blob = [1024]byte{}
w, buf := p.outOrStdout(), bytes.NewBuffer(blob[:0])
if p.report == nil {
return errors.Simple("nothing to print")
}
for site := range p.report.Sites() {
if site.Error != nil {
p.critical().Fprintf(w, "report %q has error %q\n", site.Name, site.Error)
if stack := errors.StackTrace(site.Error); stack != nil {
p.critical().Fprintf(ioutil.Discard, "stack trace: %#+v\n", stack) // for future
}
continue
}
sort.Sort(pagesByLocation(site.Pages))
for _, page := range site.Pages {
last := len(page.Links) - 1
{
buf.Reset()
p.tpl.Execute(buf, page)
}
p.typewriter(page.Link).Fprintf(w, "%s\n", p.decoder(buf.String()))
sort.Sort(linksByStatusCode(page.Links))
for i, link := range page.Links {
{
buf.Reset()
p.tpl.Execute(buf, link)
}
if i == last {
p.typewriter(&link).Fprintf(w, " ββββ%s\n", p.decoder(buf.String()))
continue
}
p.typewriter(&link).Fprintf(w, " ββββ%s\n", p.decoder(buf.String()))
}
}
if len(site.Problems) > 0 {
p.critical().Fprintf(w, "found problems on the site %q\n", site.Name)
for i, problem := range site.Problems {
p.critical().Fprintf(w, "- [%d] %s `%+v`\n", i, problem.Message, problem.Context)
}
}
}
return nil
}
func (p *Printer) critical() typewriter {
return p.typewriter(nil)
}
func (p *Printer) outOrStdout() io.Writer {
if p.output != nil {
return p.output
}
return os.Stdout
}
func (p *Printer) typewriter(link *Link) typewriter {
var (
tw typewriter
ok bool
)
switch {
case link == nil:
tw, ok = p.ink[danger]
case link.StatusCode >= 200 && link.StatusCode < 300:
if link.Internal {
tw, ok = p.ink[shaded]
break
}
tw, ok = p.ink[success]
case link.StatusCode >= 300 && link.StatusCode < 400:
tw, ok = p.ink[warning]
case link.StatusCode >= 400:
tw, ok = p.ink[danger]
}
if !ok || tw == nil {
tw = typewriterFunc(fmt.Fprintf)
}
return tw
}
type typewriter interface {
Fprintf(io.Writer, string, ...interface{}) (int, error)
}
type typewriterFunc func(io.Writer, string, ...interface{}) (int, error)
func (fn typewriterFunc) Fprintf(w io.Writer, format string, a ...interface{}) (int, error) {
return fn(w, format, a...)
}
type pagesByLocation []*Page
func (l pagesByLocation) Len() int { return len(l) }
func (l pagesByLocation) Less(i, j int) bool { return l[i].Location < l[j].Location }
func (l pagesByLocation) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
type linksByStatusCode []Link
func (l linksByStatusCode) Len() int { return len(l) }
func (l linksByStatusCode) Less(i, j int) bool { return l[i].StatusCode < l[j].StatusCode }
func (l linksByStatusCode) Swap(i, j int) { l[i], l[j] = l[j], l[i] }