forked from raviqqe/muffet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
163 lines (129 loc) · 2.93 KB
/
command.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"time"
"github.com/logrusorgru/aurora/v3"
"github.com/temoto/robotstxt"
)
type command struct {
stdout, stderr io.Writer
terminal bool
httpClientFactory httpClientFactory
}
func newCommand(stdout, stderr io.Writer, terminal bool, f httpClientFactory) *command {
return &command{stdout, stderr, terminal, f}
}
func (c *command) Run(args []string) bool {
ok, err := c.runWithError(args)
if err != nil {
c.printError(err)
}
return ok
}
func (c *command) runWithError(ss []string) (bool, error) {
args, err := getArguments(ss)
if err != nil {
return false, err
} else if args.Help {
c.print(help())
return true, nil
} else if args.Version {
c.print(version)
return err == nil, err
}
client := newThrottledHTTPClient(
c.httpClientFactory.Create(
httpClientOptions{
MaxConnectionsPerHost: args.MaxConnectionsPerHost,
BufferSize: args.BufferSize,
MaxRedirections: args.MaxRedirections,
SkipTLSVerification: args.SkipTLSVerification,
Timeout: time.Duration(args.Timeout) * time.Second,
},
),
args.MaxConnections,
)
pp := newPageParser(newLinkFinder(args.ExcludedPatterns))
f := newLinkFetcher(
client,
pp,
linkFetcherOptions{
args.Headers,
args.IgnoreFragments,
},
)
_, p, err := f.Fetch(args.URL)
if err != nil {
return false, fmt.Errorf("failed to fetch root page: %v", err)
} else if p == nil {
return false, errors.New("root page is not HTML")
}
rd := (*robotstxt.RobotsData)(nil)
if args.FollowRobotsTxt {
rd, err = newRobotsTxtFetcher(client).Fetch(p.URL())
if err != nil {
return false, err
}
}
sm := (map[string]struct{})(nil)
if args.FollowSitemapXML {
sm, err = newSitemapFetcher(client).Fetch(p.URL())
if err != nil {
return false, err
}
}
checker := newPageChecker(
f,
newLinkValidator(p.URL().Hostname(), rd, sm),
args.OnePageOnly,
)
go checker.Check(p)
if args.JSONOutput {
return c.printResultsInJSON(checker.Results())
}
formatter := newPageResultFormatter(args.Verbose, c.terminal)
ok := true
for r := range checker.Results() {
if !r.OK() || args.Verbose {
c.print(formatter.Format(r))
}
if !r.OK() {
ok = false
}
}
return ok, nil
}
func (c *command) printResultsInJSON(rc <-chan *pageResult) (bool, error) {
rs := []*jsonPageResult{}
ok := true
for r := range rc {
if !r.OK() {
rs = append(rs, newJSONPageResult(r))
ok = false
}
}
bs, err := json.Marshal(rs)
if err != nil {
return false, err
}
c.print(string(bs))
return ok, nil
}
func (c *command) print(xs ...interface{}) {
if _, err := fmt.Fprintln(c.stdout, strings.TrimSpace(fmt.Sprint(xs...))); err != nil {
panic(err)
}
}
func (c *command) printError(xs ...interface{}) {
s := fmt.Sprint(xs...)
if c.terminal {
s = aurora.Red(s).String()
}
if _, err := fmt.Fprintln(c.stderr, s); err != nil {
panic(err)
}
}