Repository navigation
Expand file tree
/
Copy pathreport.go
More file actions
245 lines (221 loc) · 6.53 KB
/
Copy pathreport.go
File metadata and controls
245 lines (221 loc) · 6.53 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package main
// What a finding is, and how it reaches the screen.
import (
"fmt"
"io"
"sort"
"strings"
)
type Severity int
const (
// Note is worth knowing.
Note Severity = iota
// Does is what happens while it runs and is finished when it is.
Does
// Leaves is what is still true tomorrow: the line in a shell profile,
// the service that starts itself, the new place packages come from.
// This is the half nobody reads for, because reading a script shows
// you what it does and not what it leaves.
Leaves
// Unread is the part where the code that will run is not the code in
// front of you: a download piped into a shell, an eval of something
// built at the time, a payload that has to be decoded first. Nothing
// under it can be checked by reading the file, which is what the file
// was handed over for.
Unread
// Reaches is it going for something it has no business going for,
// sending something out, or covering up what it did.
Reaches
)
func (s Severity) tag() string {
switch s {
case Reaches:
return "REACHES"
case Unread:
return "UNREAD "
case Leaves:
return "LEAVES "
case Does:
return "DOES "
}
return "NOTE "
}
type Finding struct {
Severity Severity
Title string
Lines []string
// Advice is what somebody does about it. A finding without one is a
// finding nobody acts on, and this report is meant to be acted on
// rather than admired.
Advice string
}
type Report struct {
Findings []Finding
// Gaps are what this run could not read. They print first, because
// they change how everything under them should be taken.
Gaps []string
}
func (r *Report) add(f Finding) { r.Findings = append(r.Findings, f) }
func (r *Report) gap(text string) {
for _, seen := range r.Gaps {
if seen == text {
return
}
}
r.Gaps = append(r.Gaps, text)
}
func (r *Report) worst() Severity {
worst := Note
for _, f := range r.Findings {
if f.Severity > worst {
worst = f.Severity
}
}
return worst
}
const (
indent = " "
wrap = 62
)
func (r *Report) render(out io.Writer, title string) {
fmt.Fprintf(out, "\n %s\n %s\n\n", title, strings.Repeat("=", min(len(title), wrap+13)))
for _, g := range r.Gaps {
block(out, "UNSEEN ", "Part of this could not be read", nil, g)
}
// Loudest first. Somebody who reads the first screen and stops should
// have read the part that mattered.
sorted := make([]Finding, len(r.Findings))
copy(sorted, r.Findings)
sort.SliceStable(sorted, func(i, j int) bool {
return sorted[i].Severity > sorted[j].Severity
})
for _, f := range sorted {
block(out, f.Severity.tag(), f.Title, f.Lines, f.Advice)
}
fmt.Fprintln(out, " Nothing was changed, and no secret was printed.")
fmt.Fprintln(out)
}
func block(out io.Writer, tag, title string, lines []string, advice string) {
// The title carries a file path often enough that it runs off the
// side, so it wraps under itself. A path with no spaces in it still
// goes over, and that is the right trade: breaking a path in half so
// it fits makes it uncopyable.
head := wrapText(title, wrap+10)
if len(head) == 0 {
head = []string{""}
}
fmt.Fprintf(out, " [%s] %s\n", tag, head[0])
for _, line := range head[1:] {
fmt.Fprintf(out, "%s%s\n", indent, line)
}
for _, line := range lines {
if line = strings.TrimRight(line, " "); line == "" {
// A blank separator prints as a blank line and not as
// thirteen spaces, so nothing here leaves trailing whitespace
// in something somebody is going to paste elsewhere.
fmt.Fprintln(out)
continue
}
fmt.Fprintf(out, "%s%s\n", indent, line)
}
if advice != "" {
if len(lines) > 0 {
fmt.Fprintln(out)
}
for _, line := range wrapText(advice, wrap) {
fmt.Fprintf(out, "%s%s\n", indent, line)
}
}
fmt.Fprintln(out)
}
func wrapText(s string, width int) []string {
words := strings.Fields(s)
if len(words) == 0 {
return nil
}
var lines []string
line := words[0]
for _, w := range words[1:] {
if len(line)+1+len(w) > width {
lines = append(lines, line)
line = w
continue
}
line += " " + w
}
return append(lines, line)
}
func plural(n int, one, many string) string {
if n == 1 {
return fmt.Sprintf("%d %s", n, one)
}
return fmt.Sprintf("%d %s", n, many)
}
// atMost keeps a list readable. Somebody with four hundred hosts in their
// history does not need all four hundred printed to take the point.
func atMost(lines []string, n int) []string {
if len(lines) <= n {
return lines
}
kept := make([]string, 0, n+1)
kept = append(kept, lines[:n]...)
return append(kept, fmt.Sprintf("... and %d more", len(lines)-n))
}
// columns lines a list up so the file and the thing found sit under each
// other, because a report with ragged columns gets skimmed rather than
// read. A path too long for its column drops to the next line instead of
// shoving everything sideways: the PSReadLine file is seventy characters
// on its own and one of those pushed every other row out of alignment.
const columnAt = 34
func columns(left, right string) []string {
if right == "" {
return []string{left}
}
// The right hand side wraps on its own and the padding is put back on
// afterwards. Wrapping the finished line instead eats the padding,
// because wrapping works on words and a run of spaces is not one, so
// every row long enough to wrap lost its column.
if len(left) >= columnAt {
out := []string{left}
for _, part := range wrapText(right, wrap+6) {
out = append(out, " "+part)
}
return out
}
pad := strings.Repeat(" ", columnAt)
parts := wrapText(right, wrap+14-columnAt)
if len(parts) == 0 {
return []string{left}
}
out := []string{left + strings.Repeat(" ", columnAt-len(left)) + parts[0]}
for _, part := range parts[1:] {
out = append(out, pad+part)
}
return out
}
// folded puts a long sentence under a short label and keeps the second
// line under the first word rather than back at the margin.
func folded(label, text string) []string {
wrapped := wrapText(text, wrap-len(label))
out := make([]string, 0, len(wrapped))
for i, line := range wrapped {
if i == 0 {
out = append(out, label+line)
continue
}
out = append(out, strings.Repeat(" ", len(label))+line)
}
return out
}
// group is a list of two column rows that remembers how many rows it has,
// which is not the same as how many lines it prints: a path too long for
// its column takes two lines, and counting lines would have the report
// saying twelve files when it means nine.
type group struct {
lines []string
count int
}
func (g *group) add(left, right string) {
g.lines = append(g.lines, columns(left, right)...)
g.count++
}