-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinspect.go
More file actions
198 lines (170 loc) · 6.23 KB
/
Copy pathinspect.go
File metadata and controls
198 lines (170 loc) · 6.23 KB
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
package cmd
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"time"
inspectLib "github.com/GrayCodeAI/inspect"
hawkInspect "github.com/GrayCodeAI/hawk/internal/bridge/inspect"
"github.com/charmbracelet/lipgloss"
"github.com/spf13/cobra"
)
var (
inspectDepth int
inspectChecks string
inspectFailOn string
inspectFormat string
inspectConcurrency int
inspectTimeout time.Duration
)
var inspectCmd = &cobra.Command{
Use: "inspect <url>",
Short: "Scan a website for broken links, security issues, accessibility violations, and more",
Long: `inspect crawls a target URL and runs configurable checks including
broken links, security headers, form problems, accessibility violations,
performance concerns, and SEO issues.
Examples:
hawk inspect https://example.com
hawk inspect --depth 3 --checks links,security --format json https://example.com
hawk inspect --fail-on high --timeout 2m https://example.com`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
target := args[0]
var opts []inspectLib.Option
if inspectDepth > 0 {
opts = append(opts, inspectLib.WithDepth(inspectDepth))
}
if inspectChecks != "" {
checks := strings.Split(inspectChecks, ",")
for i := range checks {
checks[i] = strings.TrimSpace(checks[i])
}
opts = append(opts, inspectLib.WithChecks(checks...))
}
if inspectFailOn != "" {
opts = append(opts, inspectLib.WithFailOn(inspectLib.ParseSeverity(inspectFailOn)))
}
if inspectConcurrency > 0 {
opts = append(opts, inspectLib.WithConcurrency(inspectConcurrency))
}
if inspectTimeout > 0 {
opts = append(opts, inspectLib.WithTimeout(inspectTimeout))
}
bridge := hawkInspect.NewBridge(opts...)
if !bridge.Ready() {
return fmt.Errorf("inspect bridge failed to initialize")
}
ctx := context.Background()
if inspectTimeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, inspectTimeout)
defer cancel()
}
report, err := bridge.Run(ctx, target)
if err != nil {
return fmt.Errorf("inspect scan failed: %w", err)
}
switch inspectFormat {
case "json":
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
if err := enc.Encode(report); err != nil {
return err
}
case "markdown":
fmt.Print(formatInspectMarkdown(report))
default:
fmt.Print(formatInspectTerminal(report))
}
if report.Failed() {
return fmt.Errorf("inspect: one or more checks failed")
}
return nil
},
}
func init() {
inspectCmd.Flags().IntVar(&inspectDepth, "depth", 5, "maximum crawl depth")
inspectCmd.Flags().StringVar(&inspectChecks, "checks", "", "comma-separated checks to run (links,security,forms,a11y,perf,seo)")
inspectCmd.Flags().StringVar(&inspectFailOn, "fail-on", "critical", "minimum severity to cause exit code 1 (info,low,medium,high,critical)")
inspectCmd.Flags().StringVar(&inspectFormat, "format", "terminal", "output format: terminal, json, markdown")
inspectCmd.Flags().IntVar(&inspectConcurrency, "concurrency", 10, "maximum concurrent requests")
inspectCmd.Flags().DurationVar(&inspectTimeout, "timeout", 60*time.Second, "scan timeout (e.g. 30s, 2m)")
}
// formatInspectTerminal renders a human-readable report using lipgloss styles.
func formatInspectTerminal(report *inspectLib.Report) string {
var b strings.Builder
titleStyle := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("12"))
critStyle := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("9"))
highStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("9"))
medStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("11"))
lowStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("14"))
infoStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("8"))
dimStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("8"))
b.WriteString(titleStyle.Render(fmt.Sprintf("Inspect Report: %s", report.Target)))
b.WriteString("\n")
b.WriteString(dimStyle.Render(fmt.Sprintf("Scanned %d pages in %s", report.CrawledURLs, report.Duration.Round(time.Millisecond))))
b.WriteString("\n\n")
if len(report.Findings) == 0 {
b.WriteString(lipgloss.NewStyle().Foreground(lipgloss.Color("10")).Render("No issues found."))
b.WriteString("\n")
return b.String()
}
for _, f := range report.Findings {
var sevLabel string
switch f.Severity {
case inspectLib.SeverityCritical:
sevLabel = critStyle.Render("CRITICAL")
case inspectLib.SeverityHigh:
sevLabel = highStyle.Render("HIGH")
case inspectLib.SeverityMedium:
sevLabel = medStyle.Render("MEDIUM")
case inspectLib.SeverityLow:
sevLabel = lowStyle.Render("LOW")
default:
sevLabel = infoStyle.Render("INFO")
}
b.WriteString(fmt.Sprintf(" [%s] [%s] %s\n", sevLabel, f.Check, f.URL))
b.WriteString(fmt.Sprintf(" %s\n", f.Message))
if f.Fix != "" {
b.WriteString(dimStyle.Render(fmt.Sprintf(" Fix: %s", f.Fix)))
b.WriteString("\n")
}
b.WriteString("\n")
}
b.WriteString(dimStyle.Render("---"))
b.WriteString("\n")
b.WriteString(fmt.Sprintf("%d finding(s) total", report.Stats.FindingsTotal))
for sev, count := range report.Stats.BySeverity {
b.WriteString(fmt.Sprintf(" %s:%d", sev, count))
}
b.WriteString("\n")
if report.Failed() {
b.WriteString(critStyle.Render(fmt.Sprintf("FAILED (threshold: %s)", report.FailOn)))
b.WriteString("\n")
}
return b.String()
}
// formatInspectMarkdown renders the report as markdown.
func formatInspectMarkdown(report *inspectLib.Report) string {
var b strings.Builder
b.WriteString(fmt.Sprintf("# Inspect Report: %s\n\n", report.Target))
b.WriteString(fmt.Sprintf("Scanned **%d pages** in %s.\n\n", report.CrawledURLs, report.Duration.Round(time.Millisecond)))
if len(report.Findings) == 0 {
b.WriteString("**No issues found.**\n")
return b.String()
}
b.WriteString("## Findings\n\n")
b.WriteString("| Severity | Check | URL | Message |\n")
b.WriteString("|----------|-------|-----|----------|\n")
for _, f := range report.Findings {
b.WriteString(fmt.Sprintf("| %s | %s | %s | %s |\n", f.Severity, f.Check, f.URL, f.Message))
}
b.WriteString("\n")
b.WriteString(fmt.Sprintf("**%d finding(s) total.**\n", report.Stats.FindingsTotal))
if report.Failed() {
b.WriteString(fmt.Sprintf("\n**FAILED** (threshold: %s)\n", report.FailOn))
}
return b.String()
}