-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
113 lines (100 loc) · 2.41 KB
/
file.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
package filehealth
import (
"context"
"fmt"
"io/fs"
"strings"
"time"
)
// File describes a file that has been scanned.
type File struct {
// Scanned file location
Root Dir
Path string
Index int
Skipped bool
// FileInfo values collected during a scan (may be empty)
Name string
Size int64
Mode fs.FileMode
ModTime time.Time
Issues []Issue
}
// String returns a string representation of f, including its index and path.
func (f File) String() string {
return fmt.Sprintf("[%d]: \"%s\"", f.Index, f.Path)
}
// Description returns a multiline string of the file's issues. It returns an
// empty string if the file has no issue.
func (f File) Description() string {
var out strings.Builder
for i, issue := range f.Issues {
if i > 0 {
out.WriteByte('\n')
}
suffix := ""
if desc := issue.Description(); desc != "" {
suffix += fmt.Sprintf(": %s", desc)
}
if r := issue.Resolution(); r != "" {
suffix += fmt.Sprintf(": (fix: %s)", r)
}
out.WriteString(fmt.Sprintf("[%d.%d] %s: \"%s\"%s", f.Index, i, issue.Summary(), f.Path, suffix))
}
return out.String()
}
// Operation executes an operation for the file.
func (f File) Operation(fn OperationFunc) error {
op := Operation{
scanned: f,
}
defer op.Close()
return fn(&op)
}
// DryOperation executes an operation for the file as a dry run.
func (f File) DryOperation(fn OperationFunc) error {
op := Operation{
scanned: f,
dry: true,
}
defer op.Close()
return fn(&op)
}
// DryRun performs a dry run of attempted fixes for each of the file's issues.
func (f File) DryRun(ctx context.Context) ([]Outcome, error) {
if len(f.Issues) == 0 {
return nil, nil
}
var results []Outcome
err := f.DryOperation(func(op *Operation) error {
for _, issue := range f.Issues {
if err := ctx.Err(); err != nil {
return err
}
if outcome := issue.Fix(ctx, op); outcome != nil {
results = append(results, outcome)
}
}
return nil
})
return results, err
}
// Fix attemtps to fix each of the issues with the given file.
func (f File) Fix(ctx context.Context) ([]Outcome, error) {
if len(f.Issues) == 0 {
return nil, nil
}
var results []Outcome
err := f.Operation(func(op *Operation) error {
for _, issue := range f.Issues {
if err := ctx.Err(); err != nil {
return err
}
if outcome := issue.Fix(ctx, op); outcome != nil {
results = append(results, outcome)
}
}
return nil
})
return results, err
}